How do I create a windows form that is an ellipse

T

Tony Johansson

Hi!

Normally when you create a form it's shape is a rectangle.
Now I want to create a windows form that is not a rectangle but an ellipse
instead
I wonder if anyone have some good example of code where this is done.

//Tony
 
A

Andy O'Neill

Tony Johansson said:
Hi!

Normally when you create a form it's shape is a rectangle.
Now I want to create a windows form that is not a rectangle but an ellipse
instead
I wonder if anyone have some good example of code where this is done.

//Tony
google shaped winform.
You create a background image with a transparent background for the bits you
don't want.
I've heard there are issues.
I think you're best learning WPF anyhow rather than winforms and it;s better
suited if your thing is irregularly shaped apps.
 
A

Alberto Poblacion

Tony Johansson said:
Normally when you create a form it's shape is a rectangle.
Now I want to create a windows form that is not a rectangle but an ellipse
instead
I wonder if anyone have some good example of code where this is done.

There are two ways to do this. The first one has already been mentioned
in another reply, and is based on adding a bitmap to the background and
setting the transparency key of the form to one of the bitmap colors.
Another way is the following:

1. Create an event handler for the form's Paint event, or override the
OnPaint method.
2. Inside this method, create an instance of the GraphicsPath class.
3. Use the various methods of the GraphicsPath, such as AddEllipse or
AddPolygon, to determine the shape of the form.
4. Assign this GraphicsPath object to the Region property of the form.

private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath gp = new GraphicsPath();

Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
rect.Inflate(-4, -4);
gp.AddEllipse(rect);

this.Region = new Region(gp);
}
 
T

Tony Johansson

Alberto Poblacion said:
There are two ways to do this. The first one has already been mentioned
in another reply, and is based on adding a bitmap to the background and
setting the transparency key of the form to one of the bitmap colors.
Another way is the following:

1. Create an event handler for the form's Paint event, or override the
OnPaint method.
2. Inside this method, create an instance of the GraphicsPath class.
3. Use the various methods of the GraphicsPath, such as AddEllipse or
AddPolygon, to determine the shape of the form.
4. Assign this GraphicsPath object to the Region property of the form.

private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath gp = new GraphicsPath();

Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
rect.Inflate(-4, -4);
gp.AddEllipse(rect);

this.Region = new Region(gp);
}

Many thanks for your answer. It works perfect!

//Tony
 
C

Chris Dunaway

There are two ways to do this. The first one has already been mentioned
in another reply, and is based on adding a bitmap to the background and
setting the transparency key of the form to one of the bitmap colors.
Another way is the following:

1. Create an event handler for the form's Paint event, or override the
OnPaint method.
2. Inside this method, create an instance of the GraphicsPath class.
3. Use the various methods of the GraphicsPath, such as AddEllipse or
AddPolygon, to determine the shape of the form.
4. Assign this GraphicsPath object to the Region property of the form.

private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath gp = new GraphicsPath();

Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
rect.Inflate(-4, -4);
gp.AddEllipse(rect);

this.Region = new Region(gp);
}

You probably don't want to do this in the paint method. Just do it
once when the form is created. I believe that the region object needs
to be disposed, so if you keep overwriting the region property in the
paint event, the old region is left orphaned until it gets GC.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top