views:

141

answers:

1

Hello,

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!

+1  A: 

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); 
}
adatapost
Wouldn't that be a massive overhead since the Paint event is fired multiple times even when the form is not resized? I guess region has to be updated only when the size of the form changes, not on each WM_PAINT message?
WorldIntruder