Accessing the Graphics Object

There are several ways an application can use the code from this chapter. It can execute code using the OnPaint method or Form_Paint event, or it can use code with a button or menu click event handler. If an application executes code with Form_Paint or OnPaint, you will need to include the following line at the beginning of the method.

 

Graphics g = e.Graphics;

 

If an application executes code from a button or menu click event handler or elsewhere, you will need to create a Graphics object using CreateGraphics or another method (see Chapter 3 for details) and call the Dispose method to dispose of objects when you're finished with them. The following code snippet gives an example:

 

Graphics g = this.CreateGraphics(); // YOUR CODE HERE // Dispose of GDI+ objects g.Dispose();

 

Note

To test code from this chapter, we will create a Windows application with code written on the menu item click event handlers.

Категории