Using GDI

Using GDI+

GDI+, the latest version of the Windows graphics design interface library, enables you to add eye-popping graphics to your applications with very little code. Often a GDI+ application can draw an image from scratch more quickly than it would draw a saved bitmap or JPEG of that image.

You can use the techniques in this section to draw an image on the background of a form, to build a specialized control, or to produce other kinds of graphical output such as a chart. Drawing in GDI+ involves three kinds of objects: pens, brushes, and text. All three draw on a surface represented by an instance of the Graphics class.

Getting a Graphics Object

Because all drawing requires a Graphics object, the first step in any drawing code is obtaining an instance. There are three ways to do so:

  • If your drawing code is in a handler for the Paint event, perhaps for the background of a form or control, the second argument of the handler method is a PaintEventArgs pointer; it has a Graphics property your code can access.

  • If your drawing code is in some other member function of a form or control, you can call its CreateGraphics method to reach the Graphics object associated with the form or control.

  • To draw into an Image object (perhaps because your code is generating a JPEG), use the static Graphics::FromImage() method, passing in a reference to the Image object.

Once you have a Graphics object, you draw on it by calling methods of the Graphics class, and passing in drawing objects, such as pens or brushes, and coordinates that position the shape or line on the drawing surface.

Drawing with Pens

Pens are used to draw lines. A pen has a width, a color, a style (for example solid or dashed), and end styles (called caps), such as arrows. To create a red pen two pixels wide, you pass the color and width to the constructor:

 Drawing::Pen* p = new Drawing:: Pen(Drawing::Color::Red, 2); 


The Color structure has a number of colors defined as constants. You can see these in the online help, but many people won't be sure exactly what color AliceBlue or PapayaWhip really is. To see the colors with their names, use the same designer you use to build your WinForms application or control. Select a button, or the form background, and click next to the BackColor property in the Properties window. On the mini dialog box that appears, select the Web tab, which presents a huge number of colors along with their names. Make a note of the ones you likethe same names are used for the constants in the Color structure.

If there's a color you want to use that doesn't seem to exist as a predefined color constant, or if you want a color that's partially transparent, you can use the FromArgb() method of the Color class: It uses the opaqueness (a number from 0 to 255 where 0 is completely transparent) and the red, green, and blue values to create exactly the color you desire .

With a Graphics object, you call its methods, passing in this pen. For example, to draw a line

 g->DrawLine(p, 10, 10, 50, 50); 

The four numbers in this function call are the x and y coordinates of the starting point, followed by the x and y coordinates of the end point. You can also construct Point objects and pass them to DrawLine() .

To draw a hollow shape, use DrawRectangle , DrawEllipse , or DrawPolygon . (A square is just a special case of a rectangle, and a circle is a special case of an ellipse.) You can alsodraw Bezier curves and other complex shapes .

Painting with Brushes

To fill a shape or area, you need a brush. A solid brush fills a shape with a single color, but there are also patterned brushes such as HatchBrush and TextureBrush , and you can fill a shape with a blend of colors using a single brushif it's a LinearGradientBrush or PathGradientBrush . ( SolidBrush and TextureBrush are in the Drawing namespace; the other brushes are in the Drawing::Drawing2D namespace.) The constructor for a LinearGradientBrush takes two points and two colors: The brush will fade from one color to the other through the rectangle.

Once you have a brush, you use methods of the Graphics instance to draw shapes on it: FillRectangle() , FillEllipse() , and FillPolygon , for example. If you want a different border around the shape, draw a hollow shape with a pen that's just slightly larger than the filled shape.

Here's an example of code that draws a rectangle on the surface of a form when a button on that form is clicked:

 private: System::Void Draw_Click(System::Object *  sender,                                  System::EventArgs *  e) {    Drawing::Drawing2D::LinearGradientBrush* b =        new Drawing::Drawing2D::LinearGradientBrush(Point(0,00), Point(100,100),                                      Drawing::Color::Red,Drawing::Color::Blue);    Drawing::Graphics* g = CreateGraphics();    g->FillRectangle(b, Rectangle(175,50,100,55));    b->Dispose();    g->Dispose(); } 

Add a button to the Form1 surface and change the name and text to Draw . Double-click the button to edit the handler for the click event, and add this code to the handler.

If you're familiar with the MFC approach to drawing, you'll notice here that you don't need to save the old pen or brush before you draw and then restore it. The Framework takes care of the housekeeping, and makes sure your code doesn't interfere with other drawing code elsewhere in the application. It is a good idea, however, to clean up your Graphics object, as well as your pens, brushes, and so on when you're finished with them by calling Dispose() .

Writing Text

Writing text is slightly more complex than drawing a hollow or filled shape: The DrawString method of the Graphics object takes the following parameters:

  • The string to be written

  • A Font object (which can be constructed using a face name and a size)

  • A brush with which to fill in each letter

  • The coordinates at which to start writing the string

  • Optionally, a StringFormat object that can cause the text to be written on an angle, from right to left, or clipped slight differently than the default

These lines of code draw text on a Graphics object g , which has already been obtained:

 String* text = "Red to Blue"; Drawing::Font* f = new Drawing::Font("Arial", 12); Drawing::SolidBrush* b2 = new Drawing::SolidBrush(Drawing::Color::Black); g->DrawString(text, f, b2, Point(175, 110)); f->Dispose(); b2->Dispose(); 

To see this code run, edit the Draw handler described in the previous section and add these lines just before the call to g->Dispose() . Build and run the application, enter a number in the upper text box to prevent validation errors, and click the Draw button to see the text underneath the gradient-filled rectangle.

Using GDI+ makes adding graphical touches to your applications much simpler than it has been in the past. You can create information-rich interfaces, or just impress your users by going beyond gray forms with gray buttons on them. Add some color, some shapes, and some alternative ways of communicating to the applications you build.

Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory © 2008-2017.
If you may any questions please contact us: