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 ObjectBecause all drawing requires a Graphics object, the first step in any drawing code is obtaining an instance. There are three ways to do so:
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 PensPens 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);
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 BrushesTo 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 TextWriting text is slightly more complex than drawing a hollow or filled shape: The DrawString method of the Graphics object takes the following parameters:
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. |