GDI + programming Basics

Source: Internet
Author: User

I. GDI
GDI is the middle layer between applications and different hardware. This structure frees programmers from directly processing different hardware and handed over the differences between hardware to GDI for processing. By isolating applications from different output device features, GDI enables Windows applications to run on any graphics output device supported by windows without any obstacles. For example, we can enable programs that work on Epson point printers to work on laser printers without changing the program. It converts the graphic output in windows to a hardware command and sends it to the hardware device. GDI is stored in the system as a file. When the system needs to output a graph, it is loaded into the memory. If it encounters a non-GDI Command when converting it into a hardware command, the system may also load the hardware driver, the driver assists GDI in converting graphical commands into hardware commands.

Ii. device Environment
Windows is a tool used to provide device independence for applications. It is an internal data structure used by windows to process output devices. The device environment is ipvs, driver, it is a link between an output device (such as a printer or a plotter). GDI is a group of C ++ classes that depict data on hardware with the assistance of a driver, it is located between the application and hardware and transmits data from one party to the other. In Visual Studio. NET, micro $ oft solves many problems in GDI and makes it easy to use. The. Net version of GDI is called GDI +.

Iii. GDI +
GDI + is the next version of GDI, which has been improved and is easier to use. One advantage of GDI is that you do not have to know any details about how data is rendered on the device. GDI + has better implemented this advantage. That is to say, GDI is a medium-and low-level API, you may also need to know the device, while GDI + is a high-level API, you do not have to know the device. For example, if you want to set the foreground and background color of a control, you only need to set the backcolor and forecolor attributes.

Iv. Changes in programming Modes
"GDI uses a stateful model, whereas GDI + uses a stateless" -- GDI is stateful, and GDI + is stateless.

1. no longer use the device environment or handle
When drawing with GDI, you must specify a device environment (DC) to associate a window or device with the handle pointer of the device environment class, all drawing operations are related to the handle. Instead of using the device environment or handle, GDI + uses graphics objects instead. Similar to the device environment, the graphics object also associates a window on the screen with it and contains the relevant properties required for drawing operations. However, only the graphics object is associated with the device environment handle, and other objects such as pen, brush, image, and font no longer use the device environment.

2. Pen, brush, Font, image, and other objects are independent of graphic objects.
  
The paint brush object can be created and maintained separately from the graphic object used to provide the drawing method. The graphics drawing method directly uses the pen object as its own parameter, thus avoiding tedious switching using SelectObject in GDI, similarly, there are brushes, paths, images, and font.

3. "current location"
  
There is always a special location called "current location" in the GDI plot operation (picturesque line. Each draw line starts from the current position. After the draw line operation ends, the end point of the straight line becomes the current position. The reason for setting the current position is to improve the efficiency of the draw line operation, because in some cases, a straight line is always connected to another line, And the beginning and end are connected. With the automatic update of the current position, it is possible to avoid two points of coordinates every time the line is drawn. Despite its necessity, there are always many situations where a straight line is drawn separately, therefore, GDI + cancels this "current location" to avoid drawing errors when the "current location" cannot be determined. Instead, it directly specifies the coordinates of the start and end points of a straight line in drawline.
  

4. Drawing and filling
GDI always uses the same drawing function for Shape Contour drawing and filling, such as rectangle. A paint brush is required for contour painting, while a painting brush is required for filling an area. That is to say, whether or not we need to fill in the drawn shape, we need to specify a painting brush, otherwise, GDI uses the default painting brush for filling. This method does bring us a lot of inconvenience. Currently, GDI + uses different methods to separate Shape Contour painting and filling operations, such as drawrectangle and fillrectangle, to draw and fill a rectangle respectively.

5. Regional Operations
GDI provides many regional creation functions, such as createrectrgn, createellpticrgn, createroundrectrgn, createpolygonrgn, and createpolypolygonrgn. It is true that these functions bring us a lot of convenience. However, in GDI +, in order to facilitate the introduction of the region into the matrix transformation operation, GDI + simplifies the general area creation method, and takes over the more complex area creation by path. Because the path object is separated from the device environment, it can be specified directly in the region constructor.

V. New Features of GDI +
Compared with GDI, GDI + adds the following new features:

1. gradient Paint Brush
In the past, the method used by GDI to implement the color gradient area was achieved by filling a cropping area with lines of different colors. Currently, GDI + has extended the GDI function. It provides linear and path gradient painting brushes to fill a graph, path and region, and can even be used to draw straight lines and curves. The path here can be regarded as the trajectory produced by various plotting functions.

2. Spline
  
The most practical significance of a curve is a spline. Splines are generated and developed based on production practices. When drawing a dashboard, the designer of the dashboard first accurately "points" to the dashboard Based on the given data. Then, a tool called a "spline" (an elastic organic glass bar or wood bar) is used to force the iron to pass through these value points and adjust the iron as appropriate, let the shape of the spline change until a proper shape is obtained, and draw the desired curve along the spline. If we regard the spline as an elastic fine beam, the iron can be regarded as the concentrated force acting on some points on the beam. Graphics: The drawcurve function of GDI + has such a parameter to adjust the concentration force. In addition to spline curves, GDI + also supports the original guide curve.

3. independent path objects
In GDI, a path belongs to a device environment (context). That is, once the device environment pointer exceeds its validity period, the path will be deleted. While GDI + uses graphics objects for drawing operations, and separates path operations from graphics objects to provide a graphicspath class for users. This means that we do not have to worry that the path object will be affected by graphics object operations, so that we can use the same path object for Multiple Path painting operations.

4. Matrix and matrix transformation
In the process of graphic processing, it is often necessary to transform its geometric information to generate complex new graphics. matrix is the most common method for geometric transformation of such graphics. To meet people's needs for graphic transformation, GDI + provides a powerful matrix class to implement transformation operations such as Matrix Rotation, miscut, translation, and proportion, in addition, GDI + supports graphics and region matrix transformations.

5. Alpha-channel Merging
In image processing, Alpha is used to measure the transparency of a pixel or image. In a non-compressed 32-bit RGB image, each pixel consists of four parts: an alpha channel and three color components (R, G, and B ). When the Alpha value is 0, the pixel is completely transparent, and when the Alpha value is 255, the pixel is completely opaque.
The Alpha color mixing is used to mix the color of the source pixel and the background pixel. The final color is determined by the RGB color component and Alpha value. The relationship between them can be expressed by the following formula:
Display color = source pixel color x alpha/255 + background color x (255-alpha)/255
The color class of GDI + defines the argb color data type, so that you can adjust the Alpha value to change the actual effect after the line, image, and background color are mixed.

6. Multi-image format support
GDI + provides the function of opening and storing various images. Using GDI +, we can directly store a BMP file as a jpg or other format image file.
In addition to the new features, GDI + also supports features such as re-coloring, color correction, sample removal, metadata, and graphics containers.

Vi. Using the GDI + method in vc.net  
To use GDI + in Visual C ++. net, follow these steps:

(1) Add the gdiplus. h file and the additional class library gdiplus. Lib in the application. Gdiplus. h include the file added to the stdafx of the application. h file, while gdiplus. LIB can be added in two ways: the first is directly in stdafx. add the following statement to the H file:

#pragma comment( lib, "gdiplus.lib" ) 

Another method is to select the "Project-> Properties" menu command and select the "linker-> input" option on the left in the pop-up dialog box, in the "add dependency" box on the right, type gdiplus. lib,

(2) Add a member variable to the application class of the application project, as shown in the following code:

ULONG_PTR m_gdiplusToken;

Ulong_ptr is a DWORD data type. This member variable is used to save the GDI + ID in the application after the GDI + is initialized, so that after the application exits, reference This identifier to call gdiplus: gdiplusshutdown to disable GDI +.

(3) Add the overload of exitinstance to the application class and add the following code to close the GDI +:

Int cgdiplusapp: exitinstance ()
{
Gdiplus: gdiplusshutdown (m_gdiplustoken );
Return cwinapp: exitinstance ();
}

(4) Add the initialization code of GDI + to the initinstance function of the application class:

Bool cgdiplusapp: initinstance ()
{
Cwinapp: initinstance ();
Gdiplus: gdiplusstartupinput;
Gdiplus: gdiplusstartup (& m_gdiplustoken, & gdiplusstartupinput, null );
...
}

(5) Add the drawing code of GDI + to the window or view class to be drawn:

Void cgdiplusview: ondraw (CDC * PDC) {graphics (PDC-> m_hdc); graphicspath path; // construct a Path. addellipse (50, 50,200,100); // use the path to construct a paint brush pathgradientbrush pthgrbrush (& Path); // set the path center color to Blue pthgrbrush. setcentercolor (color (255, 0, 0,255); // set the color around the path to blue bar, but the Alpha value is 0 Color Colors [] = {color (0, 0, 0, (0,255)}; int COUNT = 1; pthgrbrush. setsurroundcolors (colors, & COUNT); graphics. fillrectangle (& pthgrbrush, 50, 50,200,100); lineargradientbrush lingrbrush (point (300, 50), point (500,150), color (255,255, 0, 0 ), // red color (255, 0, 0,255); // blue graphics. fillrectangle (& lingrbrush, 300, 50,200,100 );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.