Introduction to GDI+ |
|
To draw something, you need a platform on which to draw and one or a few tools to draw with. The most common platform on which to draw is probably a piece of paper. Besides such a platform, you may need a pen or a brush that would show the evolution of the drawing work on the platform. Since a pen can have or use only one color, depending on your goal, one pen may not be sufficient, in which case you would end up with quite a few of them. A device context is an ensemble of the platform you draw on and the tools you need to draw with. It also includes the dimensioning of the platform, the orientation and other variations of your drawing, the colors, and various other accessories that can complete your imagination. When using a computer, you certainly cannot position tools on the table or desktop for use as needed. To help with drawing on the Windows operating system, Microsoft created the Graphical Device Interface, abbreviated as GDI. It is a set of classes, functions, variables, and constants that group all or most of everything you need to draw on an application. The GDI is provided as a library called Gdi.dll and is already installed on your computer. GDI+ is the system used to perform drawing and other related graphics operations for the Microsoft Windows family of operating systems. Its predecessor was the Graphical Device Interface (GDI), which has therefore been replaced, namely with the new operating systems such as Windows XP and Windows Server 2003. The + in GDI+ indicates that it provides a significant improvement to GDI. It adds new features that were not available in GDI and were therefore difficult to produce. GDI+ allows you to create device-independent applications without worrying about the hardware on which the application would run. GDI+ is already installed in Microsoft Windows XP and Windows Server 2003. To use it on previous operating systems, it must be explicitly setup. GDI+ provides its functionality through three fronts:
|
The Graphics Platform |
In GDI, to draw, you have to obtain a handle to the device context. This is done using HDC then calling a function such as BeginPaint() to initialize the device context. You also have to create the tools needed to draw. For example, you have to create a pen and/or a brush. Once the tools are ready, you have to select them into the device context to make them available. After drawing, you should release the device context. To draw in GDI+, you use an object called graphic. |
The color is one the most fundamental objects that enhances
the aesthetic appearance of an object. The color is a non-spatial object that is
added to an object to modify some of its visual aspects. To support colors, the
GDI+ library provides the Color structure. |
Bits | ||||||||||
Alpha |
|
|||||||||
Red |
|
|||||||||
Green |
|
|||||||||
Blue |
|
Converted to decimal, each one of the red, green, and blue numbers would produce: 27 + 26 + 25 + 24 + 23 + 22 + 21 + 20 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 Therefore, each number can have a value that ranges from 0 to 255 in the decimal system. The alpha section is reserved for the operating system. The other three numbers are combined to produce a single value as follows: |
Color |
|
|||||||||||||||||||||||||||||||||||||||||||||||||
Value |
Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick Me.BackColor = Color.Aqua End Sub
If none of the pre-defined colors suits you, you can define your own color as a combination of red, green, and blue values. To create a color using this approach, you can declare a variable of type Color. To specify the characters of the color, the Color structure provides the FromArgb() static method overloaded in four versions as follows: |
Overloads Public Shared Function FromArgb(ByVal argb As Integer) As Color Overloads Public Shared Function FromArgb(ByVal alpha As Integer, _ ByVal baseColor As Color _ ) As Color Overloads Public Shared Function FromArgb(ByVal red As Integer, _ ByVal green As Integer, _ ByVal blue As Integer _ ) As Color Overloads Public Shared Function FromArgb(ByVal alpha As Integer, _ ByVal red As Integer, _ ByVal green As Integer, _ ByVal blue As Integer _ ) As Color
The third version, which is the most used allows you to specify three values that each ranges from 0 to 255. Here is an example: |
Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick Me.BackColor = Color.FromArgb(26, 69, 174) End Sub
This would produce:
The most basic tool you can use is the pen. The GDI+ library provides a pen through the Pen class. To obtain a pen, you can declare a pointer to Pen. The most basic piece of information you must specify about a pen is its color. To do this, you can use the following constructor: Public Sub New(ByVal color As Color) Here is an example: |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim clrBlue As Color = Color.Blue Dim penRed As Pen = New Pen(clrBlue) End Sub
If you have already created a pen, to change its color, you can assign the desired color name or color value to the Pen.Color property. The Pen class provides more details about a pen than that. For now, we can use a pen as simple as this one. |
Introduction |
The main object on which you will perform most drawings is called a graphic. In most cases, this object is not readily available when you need it: you must request it from the object on which you want to draw or you must create it. Both operations are highly easy. |
In GDI+, a graphic object is based on a class called Graphics. Therefore, before drawing, you should obtain a graphic object. Fortunately, every Windows control, that is, every object based on the Control class automatically inherits a method called CreateGraphics(), which gives you access to the graphic part of a control. The syntax of the Control.CreateGraphics() method is: Public Function CreateGraphics() As Graphics As you can see, the CreateGraphics() method returns the Graphics object of the variable you call it from. Here is an example of getting the Graphics object of a form: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click Dim graph As Graphics = Me.CreateGraphics() End Sub Another technique you can use to get the Graphics object of a control is to call the Control.FromHwnd() static method. Its syntax is: Public Shared Function FromHwnd(ByVal hwnd As IntPtr) As Graphics Remember that this method is static. The argument passed to it must be a handle to the object whose Graphics object you want to access. We saw already that every Windows control has a handle called Handle. Here is an example of using it to get the Graphics part of a form: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click Dim graph As Graphics = Graphics.FromHwnd(Me.Handle) End Sub If you are using the Paint event of a window, it provides a readily available Graphics object from its PaintEventArgs argument. You can access the Graphics object as follows: Private Sub Button1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles Button1.Paint e.Graphics . . . End Sub At this time, you should be familiar with the Color
Dialog box.
As mentioned above, before drawing, make sure you have a Graphics object, which depends on your approach to drawing. To actually perform the drawing, the Graphics class provides various methods adapted for different shapes. Each method used to draw something has a name that starts with Draw... Also, each method that is used to draw a known shape requires a Pen argument. Therefore, when drawing, your first decision will be based on the shape or type of figure you want to draw. Probably the second decision will consist on specifying the color of the border. Two other pieces of information are particularly important with regards to any figure or shape you will need to draw: the location and dimensions.
To keep track of the various drawings, the object on which you draw uses a coordinate system that has its origin (0, 0) on its top-left corner. If you are drawing on a form, this origin is positioned just under the title bar to the left: How you specify the values of the starting point of a shape or figure depends on the shape. |
|
||
Home | Copyright © 2004-2010 FunctionX, Inc. | |
|