Wednesday, October 31, 2007

Drawing rounded rectangles

I needed rounded-corners panels for the application I'm working on.

Some search showed there is no built-in solution, and the way to go is using GDI+, and drawing the control's border manually, meaning you need to draw 4 arcs and 4 lines:

GraphicsPath gp=new GraphicsPath();

gp.AddLine(X + radius, Y, X + width - (radius*2), Y);

gp.AddArc(X + width - (radius*2), Y, radius*2, radius*2, 270, 90);

gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius*2));

gp.AddArc(X + width - (radius*2), Y + height - (radius*2), radius*2, radius*2,0,90);

gp.AddLine(X + width - (radius*2), Y + height, X + radius, Y + height);

gp.AddArc(X, Y + height - (radius*2), radius*2, radius*2, 90, 90);

gp.AddLine(X, Y + height - (radius*2), X, Y + radius);

gp.AddArc(X, Y, radius*2, radius*2, 180, 90);

You can find the raw code in Bob Powell's GDI+ FAQ site, and a set of rounded controls, including panels, in an article by Hesham Desouky in CodeProject.

No comments: