10.1 Obtaining a Graphics Object

 <  Day Day Up  >  

You want to render objects to a Windows Form or an individual control.


Technique

There are several ways to obtain a Graphics object. If you override the OnPaint method in your form or control, the PaintEventArgs contains a Graphics object. You can also retrieve a Graphics object at any time by calling the CreateGraphics method. This method is defined in the System.Windows.Forms.Form class as well as the classes used for Windows Form controls. When finished with the Graphics object created using the CreateGraphics method, you must destroy the object using the Dispose method. To render onto a Label control, for instance, call the CreateGraphics defined in the Label object instance:

 
 Label lbl = new Label(); Graphics g = lbl.CreateGraphics(); // do some rendering here g.Dispose(); 

You can also obtain a Graphics object from a window handle or a device context handle. This step is necessary if your code utilizes WIN32 API functions. A window handle is a unique systemwide identifier that is created for each window on the system. Likewise, a handle to a device context (HDC) is a unique identifier for a device information structure. If you obtain a Graphics object using these methods , you must destroy them when finished by calling the Dispose method:

 
 Graphics g1 = Graphics.FromHwnd( this.Handle ); Graphics g2 = Graphics.FromHdc( g1.GetHdc() ); g1.Dispose(); g2.Dispose(); 

Comments

Rendering using a Graphics object is a lot like painting on a canvas. In fact, this similarity is so innate that you'll see various artistic references to painting when working with GDI+. Drawing lines, for instance, uses a Pen object and you fill areas with colors and patterns by using a Brush . In fact, the event handler that is called when a form is about to be rendered is OnPaint .

A Graphics object hides the details of working with a device context. In GDI, you had to first retrieve a handle to the device context of the particular device you wanted to render to. The device context is a structure within system-allocated memory used by your application, the operating system, and the hardware driver for the associated device. Add to the mix the fact that you can create memory-based device contexts not particularly associated with a hardware device, and you can begin to see why the learning curve is a little high. The GDI+ Graphics object removes a large portion of the complexities inherent with WIN32-based GDI programming and also adds several features absent in GDI. That's not saying that there are portions of GDI+ that are impossible to accomplish with GDI; it's just that GDI+ contains several features that would require extensive algorithms to achieve the same results in GDI.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net