Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I draw on a control without handling a paint message?

To draw on any hwnd, you need to get a Graphics object from that hwnd. Once you have the Graphics object, you can then use its methods to draw. Of course, since this drawing is not done in the Paint handler, it will not be automatically refreshed when the control is redrawn for any reason.

Graphics g = Graphics.FromHwnd(this.Handle); 
SolidBrush brush = new SolidBrush(Color.Red); 
Rectangle rectangle = new Rectangle(25, 25, 100, 100); 
g.FillRectangle(brush, rectangle); 

Contributed from George Shepherd's Windows Forms FAQ



Page view counter