GDI Basics

GDI+ Basics

GDI+ is the .NET API for writing graphics in Windows Forms. It offers many capabilities for drawing shapes and patterns for any type of 2D graphical output necessary.

To begin writing GDI+, it is helpful to have a bearing on the coordinate system being used in a client area. By default the coordinate origin (0,0) is in the upper-left corner of the client area. Vertical coordinates increase down the page and decrease up the page. Horizontal coordinates increase to the right of the page and decrease to the left of the page.

Drawing in the client area typically occurs during a Paint event, which is generated by the system whenever a window needs to be repainted. Because there is no way to know for sure when the client area of an application will need to be repainted, an application must be prepared at any time to handle a Paint event. Windows Forms programming offers a couple of ways to handle Paint events, by using an event handler or by overriding the OnPaint method of the base class. The Paint event handler follows the standard event handler pattern with a sender object and PaintEventArgs argument, but the OnPaint method only needs to be declared with a PaintEventArgs argument. For simplicity, I'll use the OnPaint method, which is demonstrated in Listing 9.1.

Listing 9.1 Handling the Paint Event (OnPaint.cs)
 protected override void OnPaint(PaintEventArgs pea) {    base.OnPaint(pea);    Graphics g = pea.Graphics;    StringFormat strFormat = new StringFormat();    strFormat.Alignment = StringAlignment.Center;    strFormat.LineAlignment = StringAlignment.Center;    g.DrawString("Welcome to C#Builder Kick Start!",       Font, Brushes.Black,  ClientRectangle, strFormat); } 

The purpose of Listing 9.1 is to show how to write a string into the center of the client area in response to a Windows Paint event. When the Paint event occurs, the Windows Forms infrastructure is notified and the virtual OnPaint method of the Control class is invoked. When overriding OnPaint, be sure to call the base class OnPaint to ensure that registered delegates to the base class Paint method are called. Because the OnPaint method in Listing 9.1 overrides OnPaint in all the classes in its hierarchy, it is called polymorphically. Remember, forms and derived types are classes and, therefore, object-oriented behavior applies.

ABOUT THE LISTING EXAMPLES IN THIS CHAPTER

The listing examples in this chapter are excerpts from the source code that belongs to this book. Since the listings are not complete, they can't be entered into the IDE and run as-is. I recommend that you obtain the source code for this book. The source code contains all C# code and project files necessary for loading each program into C#Builder and running it.

Any type of graphics drawing on a form requires either a Paint event handler or an overridden OnPaint method (see Listing 9.1). Both of these methods have a PaintEventArgs argument. A very important member of PaintEventArgs is the Graphics property, which returns a Graphics object. All drawing on a form is performed with a Graphics object. It enables drawing text, painting regions, or rendering a multitude of shapes.

In Listing 9.1, the Graphics object is used to draw the string "Welcome to C#Builder Kick Start!" to the center of the client area. The center alignment is made possible by setting the properties of an instance of the StringFormat type. The StringFormat instance is passed as the last argument to the DrawString method.

The Font and ClientRectangle are inherited properties that can be set or read from as needed. Font is the property that obtains the current font of the form. The ClientRectangle property returns a Rectangle object holding the dimensions of the form's client area, which is the area in the middle of the form minus title bar, toolbar, menu, status bar, and borders. It is instructive to peruse the documentation on DrawString and StringFormat to see what is possible with their additional overloads. The next section discusses the Brush parameter. Figure 9.1 shows the output of the program in Listing 9.1.

Figure 9.1. Demonstration of handling Paint events.

graphics/09fig01.gif



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

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