WorldIntruder
WorldIntruder

Reputation: 191

Issue when Applying Region to a Form

I am facing a strange behavior when I apply a non-rectangular region to a Windows Form (lets say an ellipse). The issue is that the form seems to disappear for a moment (as if the region is empty) when initially resized. It looks like a slight flicker whereas the contents of the desktop behind the form become visible for a fraction of the second. After the first resize this flicker is no longer observable.

This can be reproduced by simply creating a Windows Forms project and applying an ellipse region to the form by using the size of the form as a bounds rectangle for the ellipse (in this way you will be able to resize the form hence its borders will not be completely "eaten" by the region).

Note: I am updating the region of the Form in the OnResize event.

The code that I am using looks the following way:

 protected override void OnResize(EventArgs e)
 {
     base.OnResize(e);
     GraphicsPath path = new GraphicsPath();
     path.AddEllipse(new Rectangle(Point.Empty, this.Size));

     this.Region = new Region(path);
 }

Any ideas what might be causing this?

Quick follow-up:

I noticed that when I put the same code snippet in the OnSizeChanged event the flicker disappears or seems to happen rarely.

Thanks!

Upvotes: 1

Views: 863

Answers (2)

Justin Doyle
Justin Doyle

Reputation: 606

You've fixed a massive problem for me in the same area.

I'm using this:

    private void BorderedPanel_SizeChanged(object sender, EventArgs e)
    {
        this.Region = new Region(RoundedRectangle.CreatePlusOne(this.ClientRectangle, this.cornerRadius, this.RectangleCorners));
        Refresh();
    }

and it works without flickering. So it's worth giving a shot!

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94625

Handles Paint event

private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath path = new GraphicsPath(); 
path.AddEllipse(new Rectangle(Point.Empty, this.Size)); 
this.Region = new Region(path); 
}

Upvotes: 1

Related Questions