Creating a Rounded Panel Control
The solution originates in OnPaint calling the base OnPaint method to draw the basic Panel and then ExtendedDraw and DrawBorder to complete the effect. ExtendedDraw creates the GraphicsPath that traces the Panel's original bounds and adds enclosing Arcs for the corners. DrawBorder as designed will always draw a border around the Panel. See Figure 3 for the result.
Figure 3: The rounded rectangle with a Lavender background and white border on a CornFlowerBlue form.
Because the GraphicsPath is assigned to the Panel's Region (aka clipping region), the actual boundaries of the Panel are constrained by the new region. This means if you assign a click event to the Panel and click in the missing corners, the Panel will not receive or respond to the event; that is, the Panel's actually region is constrained by the GraphicsPath, not just its visual appearance.
Custom shaped controls can add some visual interest to you applications. You need to know the orientation of the drawing reference system for GDI+ and then it's basically lines, arcs, shapes, and graphics used with a GraphicsPath object and the control's clipping region. You aren't required to use inheritance to create shaped controls, but it does make them easier to reuse.
About the Author
Paul Kimmel is a freelance writer for Developer.com and CodeGuru.com. He is the founder of Software Conceptions, Inc, founded in 1990. Paul Kimmel is architect for EDS, an HP Company. You may contact him about article questions at firstname.lastname@example.org.
Check out Paul's most recent books, LINQ Unleashed and Teach Yourself the ADO.NET Entity Framework in 24 Hours (coming Spring 2009).
Copyright © by Paul T. Kimmel. All Rights Reserved.
Page 2 of 2