Introduction to GDI+ |
|
|
|
The Graphics Platform |
In GDI, to draw, you have to obtain a handle to the device context. This is done by declaring a variable or a pointer to 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, it is suggested that you release the device context. To draw in GDI+, you use an object called a graphic or a graphics object.
|
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. A color is created as a combination of four 8-bit values. The first value is referred to as alpha but it is mostly used internally. The second is called red. The third is called green. The fourth is called blue: |
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 |
Converted to decimal, this number has a value of 255 * 255 * 255 = 16581375. This means that we can have approximately 16 million colors available. The question that comes to mind is how we use these colors, to produce what effect. You computer monitor has a surface that resembles a series of tinny horizontal and vertical lines. The intersection of a one horizontal line and a vertical line is called a pixel. This pixel holds, carries, or displays one color. |
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { this->BackColor = Color::Turquoise; }
If none of the pre-defined colors suits you, you can define your own 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: |
public: static Color FromArgb(int argb); public: static Color FromArgb(int alpha, Color baseColor); public: static Color FromArgb(int red, int green, int blue); public: static Color FromArgb(int alpha, int red, int green, int blue);
The third version, which is the most used allows you to specify three value that each ranges from 0 to 255. Here is an example: |
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { this->BackColor = Color::FromArgb(26, 69, 174); }
This would produce: Whether a color was initialized with one of the Color pre-defined color properties or using the FromArgb() methods, if you want to retrieve the red, green, and blue values of a color, you can use the R, the G, or the B properties to extract the value of each. Each one of these properties is of a byte type. Alternatively, you can call the Color::ToArgb() method. Its syntax is: public: int ToArgb(); This method returns an integer. |
While the Graphics class provides the platform to draw on, you need a tool to draw with. 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: Pen(Color color); Here is an example: |
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { Color clrBlue = Color::Blue; Pen *penRed = new Pen(clrBlue); }
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. |
Getting a Graphic Object |
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: Graphics *CreateGraphics(); 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: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { Graphics *graph = this->CreateGraphics(); }
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: static Graphics *FromHwnd(IntPtr hwnd);
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: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { Graphics *graph = Graphics::FromHwnd(this->Handle); }
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: System::Void Form1_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { e->Graphics . . . }
At this time, you should be familiar with the ColorDialog
box control.
The Process of Drawing |
Getting a Device Context |
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 from 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. |
The Starting Point of a Shape or Figure |
|
||
Home | Copyright © 2004-2012, FunctionX | |
|