Use GDI + programming

xiaoxiao2021-03-06  15

1 Introduction

GDI is a subsystem in Windows XP, which is primarily responsible for outputting information about the display screen and the print device, which is a set of applications programming interfaces implemented by C classes. As the name suggests, GDI is the successor of previous version of GDI. For compatibility, Windows XP still supports previous version of GDI, but when developing new applications, the developers should use GDI to meet graphic output, because GDI previous The GDI in the Windows version has been optimized and added a lot of new features.

GDI as a graphical device interface allows the application developer to output the screen and the printer information, there is no need to consider the details of the specific display device, and they only need to call some methods of the GDI library output to complete the graphics operation, the real drawing work These methods are handed over to specific device drivers, and GDI makes graphics hardware and applications to isolate each other. Thus makes developers write equipment-independent applications very easy.

2.GDI new function

(1) Gradient Brushes

GDI allows the user to create a brush that is gradually graded along a path or a straight line, to fill Shapes, paths, region (Regions), gradient brush can also draw straight lines, curves, path, when you use a line shape When a brush is filled, the color can gradually change along the shape.

(2) Cardinal SPlines

GDI supports the base spline function, while GDI does not support. The base spline is a large curve that is connected in a set of single curves in order to connect. The spline is specified by a range of points and passes through each specified point. Since the base spline is smooth through every point in the group (no sharp corners), it is more accurate than the path created with a linear connection. Below is a graphic created by two methods, one uses a base spline, one uses a straight line.

(3) Persistent Path Objects Persistent Path Objects

In GDI, the path belongs to the device description table (DC), and the path will be destroyed after the painting. In GDI , the drawing works by the Graphics object, you can create a few path objects separated from Graphics, and the path object is not broken when the drawing is operated so that you can use the same path object multiple times.

(4) Transformation and matrix object Transformations & Matrix Object

GDI provides a matrix object, a very powerful tool, making it easy to write a graphic rotation, translation, and zoom code become very easy. A matrix object is always associated with a graphic transformation. For example, the path object (PATH) has a transform method, and one of its parameters can accept the address of the matrix object, and it can be drawn according to the transformation matrix. . The following graphic is an example before and after a graphical transformation, and the transformation is turned until the zoom is scaled.

(5) Scalable area Scalable Regions

GDI improves GDI in the region (Regions), in GDI, Regions stores in device coordinates, and the only operation of REGONS unique to graphically transform is to translate the region. GDI uses the World Coordinate Storage Area (Regions), allowing any graphic transformation to the region (such as scaling as shown), graphical transformation to transform matrix storage, the following example is an example (zoom, rotation, translation) before and after a region transform (zoom, rotation, translation )

(6) Alpha beding (mixed)

You may notice the graphics displayed above, a portion of the red unlailized area with the transformation area crossover, the fantasy effect of this part is implemented by GDI supported alpha beding (mix), using Alpha Fusion, you can specify a population The transparency of the color, the transparent color is integrated with the background color, the more transparent filling color, the more clear the background color display, the four ellipses shown below are filled with the same color, but due to different transparency, different display effects . (7) A variety of image formats support.

The image has a lightweight position in the graphical interface program, and GDI has supported JPEG (Joint Photographic Experts Group, GIF (Exchange Image File) in addition to GDI-supported graphics formats such as BMP. NetWork Graphics, Tiff (Tag Image File Format) and other image formats, you can use these image files directly in the program without considering the compressed algorithms they used.

(8) Others.

GDI will also support other technologies, such as reloading, color correction, metadata, graphic containers, perhaps these features we will in future WindowsXP or Microsoft Visualstudio. See it in Net.

3. Change Device Contexts, Handles, and Graphics Objects (graphics object)

If you have used GDI to write an application, you are sure to be very familiar with the concept of device description form (DC), and the device description is a data structure used by Windows, which is used to store specific device capabilities and how to redraw on the device. Some projects related to attribute information. Moreover, the device description table of the video device is also related to a particular window. First, you have to get a device descriptor table, and then pass this handle as a parameter to the GDI graphics draw function when graphically drawn. Of course, you can also pass it to a function of obtaining or setting the device description table.

With GDI functions, you don't have to use handles or device descriptions. Instead, you can simply create a graphics (Graphics), then call it in the object-oriented programming manner, such as MyGraphicsObject.drawline (parameters). Graphics object is the core of GDI , just as the device description table is the core of GDI, the device descriptor table (DC) and graphics objects play a similar role in different environments, but both There is a difference in this quality. The former uses handle-based programming methods and the latter uses object-oriented programming methods.

Like the graphic object, the device object, related to the screen's display window, which contains attribute information related to the project (such as smoothness), but the graphic object does not like the PEN (Brush), Path (path) like GDI. Image (image), font (font), etc. stir together. In GDI, all drawings related to the drawing must be selected in the specified device descriptor (using the SelectObject function) to be used by the device descriptor table specified. In GDI , you only need to pass these drawing objects as a parameter to the graphic object Graphics method call, and the drawing tool used by each graphic object is related to the parameters used by it, it can be used by parameters. Pen and brush drawings instead of connecting with a specific pen and paintings. The following code achieves the red line drawing with two methods, of which the line width 3, the starting point (20, 10), the end point (200, 100)

GDI

HDC HDC;

Paintstruct PS;

HPEN HPEN

...

HDC = BeginPaint (HWND, & PS); get the device handle and start drawing

HPEN = Createpen (PS_SOLID, 3, RGB (255, 0, 0)); Create a red brush, width 3

SelectObject (HDC, HPEN); Selected Device Description Table

MoveToex (HDC, 20, 10, NULL); Picture

LINETO (HDC, 200, 100);

Endpaint (hwnd, & ps); end drawing

GDI

HDC HDC;

Paintstruct PS;

Pen * mypen;

Graphics * MyGraphics;

...

HDC = BeginPaint (HWND, & PS);

Mypen = new pen (0xffff0000, 3); create a pen, width 3, red

MyGraphics = New Graphics (HDC); create graphic objects with device handle

MYGRAPHICS-> Drawline (Mypen, 20, 10, 200, 100); Method for calling graphic object line

Endpaint (hwnd, & ps);

How to use GDI

GDI is not a patent of Windows XP, which can also be used in other Windows operating systems (excluding Win3.x), including 64-bit Windows versions, in Microsoft's official, GDI supports all Windows-based applications. Just copy GDIPLUS.DLL to the Windows system directory to use applications that require GDI support. Microsoft Visual C # fully supports GDI , Microsoft Visual C . Net

There are two ways to use GDI , one method is implemented by hostering applications, and another method is achieved in the non-hosting project to be implemented by calling C object-oriented class.

Below is a Window program based on Win32 SDK. Use the gdiplus.h header file to include the GDIPLUS.LIB library file.

#define unicode

#include

#include

Using namespace gdiplus;

Void OnPaint (HWND HWND)

{

HDC HDC;

Paintstruct PS;

HDC = Beginpaint (HWND, & PS); Graphics Graphics (HDC);

PEN PEN (Color (255, 0, 0, 255);

Graphics.drawline (& Pen, 0, 0, 200, 100);

Endpaint (hwnd, & ps);

} // onpaint

Lresult Callback WndProc (HWND, UINT, WPARAM, LPARAM);

Int WinApi Winmain (Hinstance Hinstance, Hinstance,

PSTR SZCMDLINE, INT ICMDSHOW)

{

Hwnd hwnd;

MSG msg;

WNDCLASS WNDCLASS;

GDIPLUSSTARTUPINPUT GDIPLUSSTARTUPINPUT;

Ulong_ptr gdiplustoken;

// GDI initialization

GDIPLUSSTARTUP (& gdiplustoken, & gdiplusstartupinput, null);

WNDCLASS.Style = CS_HREDRAW | CS_VREDRAW;

WNDCLASS.LPFNWNDPROC = WNDPROC;

WNDCLASS.CBCLSEXTRA = 0;

Wndclass.cbWndextra = 0;

WNDCLASS.HINSTANCE = HINSTANCE;

WNDCLASS.HICON = LOADICON (NULL, IDI_APPLIC);

WNDCLASS.HCURSOR = loadingcursor (null, idc_arrow);

WNDCLASS.HBRBACKGROUND = (Hbrush) getStockObject (White_brush);

WNDCLASS.LPSZMENUNUNAME = NULL;

WNDCLASS.LPSZCLASSNAME = Text ("gettingstarted");

RegisterClass (& Wndclass);

HWND = CREATEWINDOW

TEXT ("gettingstarted"), // window class name

Text ("getting started"), // WINDOW CAPTION

WS_OVERLAPPEDWINDOW, // WINDOW STYLE

CW_USEDEFAULT, // Initial X position

CW_USEDEFAULT, // Initial Y position

CW_USEDEFAULT, // Initial X size

CW_USEDEFAULT, // Initial y size

Null, // Parent Window Handle

Null, // WINDOW MENU HANDLE

Hinstance, // Program Instance Handle

NULL); // CREATION Parameters

ShowWindow (hwnd, icmdshow);

UpdateWindow (HWND);

While (GetMessage (& MSG, NULL, 0, 0))

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

GDIPLUSSHUTDOWN (GDIPLUSTOKEN); Close GDI

Return msg.wparam;

} // WinMain

LResult Callback WndProc (HWND HWND, UINT MESSAGE,

WPARAM WPARAM, LPARAM LPARAM) {

Switch (Message)

{

Case WM_Paint:

OnPaint (hwnd);

Return 0;

Case WM_DESTROY:

PostquitMessage (0);

Return 0;

DEFAULT:

Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);

}

} // WndProc

In summary, we can see that the graphic programming using GDI is easier, more powerful, but GDI is not only part of Microsoft Visual Studio.net, but also an important part of WindowXP, the future is GDI world, GDI will gradually exit The historical stage, this is a general trend and cannot reversible. It is your wise choice to lock the GDI as soon as possible.

.a1 {font-size: 1}

转载请注明原文地址:https://www.9cbs.com/read-46581.html

New Post(0)