<C++ .NET WinForm, Dialog Box & Controls 7 |Main |C++ .NET Drawing & Printing 2 >



Graphical Output: Simple Drawing and Printing 1



What we have in this page?


  1. Graphics with GDI+

  2. The System::Drawing Namespaces

  3. The Graphics Class

  4. Creating Graphics Objects

  5. Drawing Objects

  6. Pen Objects

  7. Brush Objects

  8. Standard Pens and Brushes

  9. Drawing Operations


  1. Paint Events

  2. Using Color

  3. Using Fonts

  4. Handling Images

  5. Printing

  6. System.Drawing.Printing Namespace

  7. A Very Quick Reference



Modules 21 and 22 introduced you to the world of Windows Forms and showed you how to use the classes in the System::Windows::Forms namespace to build GUI applications. This module introduces the drawing mechanisms that underlie all the Windows Forms functionality. You’re going to learn how to draw on forms, how to display images, and how to print.


Graphics with GDI+


The Microsoft .NET subsystem that handles graphical output is called GDI+. Graphical Device Interface (GDI) was the original Windows graphics model introduced with Microsoft Windows 3. It remained much the same through the versions of Windows that followed, but it has been greatly improved for the .NET Framework, hence the addition of the plus sign (+) to the name. GDI+ provides a library of classes for performing simple two-dimensional graphics, such as drawing lines and simple shapes, displaying text and bitmaps, and printing. Although GDI+ is much improved compared to the original GDI, it’s still essentially a simple two-dimensional graphics library, with no advanced features such as animation or three-dimensional effects.


The System::Drawing Namespaces


The GDI+ functionality is spread across several namespaces:

  1. System::Drawing provides the basic functionality.

  2. System::Drawing::Drawing2D provides more advanced two-dimensional and vector graphics.

  3. System::Drawing::Imaging provides functionality for image processing.

  4. System::Drawing::Text provides more typographic functionality than System::Drawing.

  5. System::Drawing::Printing handles printing.

  6. System::Drawing::Design provides functionality for extending the design-time UI in Visual Studio .NET.

The System::Drawing namespace provides access to GDI+ basic graphics functionality. More advanced functionality is provided in the System::Drawing::Drawing2D,System::Drawing::Imaging, and System::Drawing::Text namespaces. The Graphics class provides methods for drawing to the display device. Classes such asRectangle and Point encapsulate GDI+ primitives. The Pen class is used to draw lines and curves, while classes derived from the abstract class Brush are used to fill the interiors of shapes.

Classes within the System::Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. In this module, we’ll mainly be looking at the features offered by the basicSystem::Drawing namespace, as well as the printing functionality provided by System::Drawing::Printing. The main classes in System::Drawing are listed in the following table.





Represents a bitmap. This class can work with several common formats, including BMP, GIF, and JPG.


Brushes are used to fill the interiors of shapes.


A collection of predefined brushes.


Structure that represents a color.


Represents a font.


Represents a family of fonts.


Represents a drawing surface.


Represents an icon.


The base for the otherImage classes, such as Bitmap and Metafile.


Used for drawing the outlines of shapes.


A collection of predefined pens.

Point, PointF

Integer and floating-point structures that represent an X, Y point.

Rectangle, RectangleF

Integer and floating-point structures that represent a rectangle.


Represents a region. Regions don’t have to be rectangular.

Size, SizeF

Integer and floating-point structures that represent a size.


A brush that fills a shape with a solid color.


Encapsulates a set of text layout information, such as alignment and line spacing.


A collection of SolidBrush objects representing the system colors.


A collection of Color objects representing the system colors.


A collection of icons, mainly representing those used in message boxes.


A collection of Pen objects representing the system colors.


A brush that uses an image to fill shapes.


Table 1





The Graphics Class


If you want to draw on a form, you need to consider a lot of issues first. For instance, your program might run on a Personal Digital Assistant (PDA) or a Wireless Application Protocol (WAP) phone, so is the display monochrome or color? How many colors can the display support? What’s the display resolution? And what about output to printers, they can be color or black and white, support different paper sizes and orientations, and have a range of resolutions.

However, GDI+ isolates you from worrying about these factors through the Graphics class, which represents an idealized drawing surface. As you’ll see shortly, the Graphics class supports a lot of drawing operations. You use these operations to draw, and the Graphics object renders them on the actual output device, effectively isolating you from the characteristics of the device. If you have come across the idea of a Windows device context (DC), you know what a Graphics object is because theGraphics class is the .NET way of encapsulating a device context. In addition to being the place where you send graphical output, a Graphics object can also tell you about the device it’s using. In this way, a Graphics object is an intermediary between you and the output device. The rest of this module will show you how to make use of the Graphics class to produce graphical output.


Creating Graphics Objects


If you look at the documentation for theGraphics class, you’ll see that it doesn’t have any constructors. You don’t create a Graphics object directly using a constructor, but instead use the Form’sCreateGraphics function to obtain one.

// Create a Graphics object

Graphics^ pg = myForm->CreateGraphics();

You don’t create a Graphics object directly because a Graphics object represents an underlying Windows data structure called a device context. Whether you get a new or a cached object is not your problem because using a factory method such as CreateGraphics hides the details of how the Graphics object is actually obtained. A Graphics object uses system resources, which will be released when the object is garbage collected. Because you don’t know when that will be, it’s a good idea to free up resources when you’ve finished by calling the object’s Dispose method. Unfortunately Dispose not working when used in the VC++ .NET for this module exercises (it is OK in VB .NET and C#). We need to use delete instead.

// Release the resources held by the

// Graphics object – for VB .NET and C#



// For VC++

delete pg;

It’s good programming practice to hold on to Graphics objects for as short a time as possible because the supply of graphics resources on some systems might be limited. Therefore, you should callDispose (or delete) as soon as you’ve finished with the object.


Drawing Objects


Basic drawing operations are performed using Pen and Brush objects. Pen objects are used to draw lines and the outlines of shapes, and Brush objects are used to fill the interior of shapes.


Pen Objects


Simple Pen objects are created by specifying a line width in pixels and a color.

// Create a black pen with default width of one pixel

Pen^ pen1 = gcnew Pen(Color::Black);


// Create a red pen two pixels wide

Pen^ pen2 = gcnew Pen(Color::Red, 2.0);

The Color arguments are specified using static members of the Color structure. We’ll discuss Color in more detail later in the module. If you want to create more advanced Pen objects, you can specify a Brush instead of a Color. Specifying Brush gives you the option of filling the lines with images or patterns.


Brush Objects


Brushes come in several varieties, all of which inherit from the Brush base class. The simplest is the SolidBrush, which fills an area with a solid color.

// Create a blue brush

SolidBrush^ br1 = gcnew SolidBrush(Color::Blue);

The TextureBrush class uses an image to fill shapes. If the image is smaller than the shape, it can be tiled.

// Create a TextureBrush which will tile

TextureBrush^ br2 = gcnew TextureBrush(gcnew Bitmap("brush.bmp"), WrapMode::Tiled);

Standard Pens and Brushes


The Pens class has properties that define more than 140 pens representing the full range of system-defined colors. Each pen has a width of one pixel. The Brushes class does exactly the same for brushes.

// Create a one-pixel wide red pen

Pen^ redPen = Pens::Red;


// Create a standard blue brush

Brush^ blueBrush = Brushes::Blue;

The SystemPens and SystemBrushes classes are very similar, but their properties reflect the standard system colors that are used to draw UI components.

// Create a pen the color of window frames

Pen^ redPen = SystemPens::WindowFrame;


// Create a brush which has the standard color of 3D controls

Brush^ blueBrush = SystemBrushes::Control;

The values associated with these pens and brushes might change if the user changes the desktop color scheme throughControl Panel.


Drawing Operations


Now let’s put together what you’ve learned so far and do some drawing on a form. The Graphics class has a lot of drawing methods, the most common of which are summarized in the following table.










Fills the entire drawing area with a color


Draws an arc representing a portion of an ellipse


Draws a Bezier curve

DrawClosedCurve, FillClosedCurve

Draws or fills a closed curve defined by an array of points


Draws a curve

DrawEllipse, FillEllipse

Draws or fills an ellipse defined by a bounding rectangle


Draws an icon


Draws an image


Draws a line


Draws a set of lines connecting an array of points

DrawPie, FillPie

Draws or fills a pie segment defined by an ellipse and two radial lines

DrawPolygon, FillPolygon

Draws or fills a polygon defined by an array of points

DrawRectangle, FillRectangle

Draws or fills a rectangle


Draws a text string


Fills a region defined by a Region object

and many more...



Table 2


Notice how there are separate draw and fill calls. If you want to draw a rectangle outlined in black and filled with red, you’ll have to call both DrawRectangle and FillRectangle. The following exercise will show you how to draw some simple shapes on a form.



Part 1 |Part 2 |Part 3 |Part 4 |Part 5 |Part 6



<C++ .NET WinForm, Dialog Box & Controls 7 |Main |C++ .NET Drawing & Printing 2 >