Paint Static Content

Problem

You want to draw custom elements on a form and make sure they are not erased when the form is minimized or obscured.

Solution

Place all your drawing code in an event handler for the Control.Paint or Form.Paint events.

Discussion

When any part of a form disappears from view, Windows automatically discards all of its graphical information. When the form reappears, Windows fires the Paint event to instruct the form to redraw itself. Thus, any custom painting logic should always be coded in a Paint event handler so that the window is refreshed accurately. To make matters even easier, the Paint event always provides a PaintEventArgs parameter. This PaintEventArgs references a Graphics object that represents the drawing surface ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.