1. Introduction GDI is a subsystem in Windows XP, which is mainly responsible for outputting information about the display screen and the print device, which is an application programming interface implemented by the C class. 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, GDI makes graphics hardware and applications to isolate each other. Thus makes applications that developers write equipment are very easy. 2.GDI new function (1) Gradient brush (Gradient Brushes) GDI allows users to create a paint brush along path or linear, to fill Shapes, paths, region, gradient brush, can also draw straight, curve, path, when you When a line-shaped brush is filled with Shapes, 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) Transformations & Matrix Object GDI provides matrix objects, a very powerful tool, making the pattern of rotation, translation, and zoom code becomes 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 Region SCALABLE REGONS GDI improves GDI in the region (in GDI), in GDI, Regions stores in the device coordinates, and the only operation of the REGIONS unique to graphically transform is translated to the area.
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 Blending You may notice the graphic displayed above, the red unpaged area has a portion of the crossover area with the transformation zone, which is achieved by the ALPHA Blending (mixing) supported by GDI . Using Alpha Fusion, you can specify the transparency of the fill color, the transparent color is fused to each other, the more transparent the color, the more clear the background color display, the four ellipses shown below are filled with the same color, but due to ownership Different transparens exhibit different display effects. (7) Multiple image format support. Image possess a lightweight position in the graphical interface program, GDI In addition to supporting GDI supported graphics format such as BMP, GrPhics Interchange Format, PNG Image format such as Exchangeable Image File, EXIF (Tag Image File Format), you can use these image files directly in the program without considering the compressed algorithms 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. Mode of programming mode
Device Contexts, Handles, and Graphics Objects If you have used GDI written applications, you are sure you are very familiar with the concept of device descriptive table (DC), device description table is Windows A data structure used, is used to store specific device capabilities and how to redraw some projects on the device. 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 line width 3, starting point (20, 10), end point (200, 100) GDI HDC HDC; PAINTSTRUCT PS; HPEN HPEN; ... HDC = Beginpaint (HWND, & PS) Get the device handle, start to draw HPEN = Createpen (PS_SOLID, 3, RGB (255, 0, 0)); create a red brush, width 3 SelectObject (HDC, HPEN); select device description table MoveToex (HDC, 20, 10 , NULL); Picture LINETO (HDC, 200, 100); EndPaint (HWND, & PS); End Draw 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 graphics objects-> Drawline (mypen, 20, 10, 200, 100); call graphics object Method ENDPAINT (HWND, & PS); 4 What GDI GDI is not a patent of Windows XP, which can also be used under other Windows operating systems (excluding Win3.x), including 64-bit Windows versions, according to Microsoft The official says, 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 uses GDI there are two methods, one method is implemented by hosted an application, and another method is to target C object-oriented in the non-hosting project. Class implementation. 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 (hind, 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_APPLICATION); wndClass.hCursor = LoadCursor (NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH) GetStockObject ( White_brush); wndclass.lpszMenuname = 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); Turning off GDI