Reprinted please indicate the source
Author: Pony
1. Introduction
In fact, I am not an expert on GDI +, but I have applied some of it in a few small projects. It is an entry point. I just recently made a summary of what I have mastered.
If you have used GDI before, congratulations, because it is easy to switch to GDI +. if you have never used GDI, congratulations, because you can directly learn GDI + without having to experience the pain of learning GDI. compared with GDI, GDI + is more abstract and upper-layer. It hides more underlying things and the overall architecture is clearer and clearer.
In the form of c ++ class, GDI + provides external APIs. programmers can call these Apis directly to complete related functions. there are 40 related classes, 50 enumerated constants and 6 struct. its limitation is that it can only run on windows XP and windows server2003 systems.
To put it simply, there are several differences between GDI + and GDI.
1 A very important improvement of GDI + is its support for multiple image formats. It currently supports the following formats of images:
BMP
Graphics Interchange Format (GIF)
JPEG
Exif
PNG
TIFF
ICON
WMF
EMF
2 The concept of DC (device environment content) has become vague and is basically transparent to programmers.
Anyone who uses GDI knows that the DC doesn't understand it, but it doesn't work. This DC stores the properties and function information of the display device. Whenever you want to drawing, you must first obtain the DC handle, then, pass the handle as a parameter to the API of GDI. In addition, you need to consider complicated operations such as selecting a device (selectObjec) and releasing a device.
The processing of the GDI + peer interface is much simpler and clearer. It has a Graphics class. A Graphics object is similar to a DC role, and its relationships with pen, brush, and so on become independent, there is no need to "select and select". In GDI +, things like pen and brush are independent objects, you only need to pass their instances as parameters to the Graphics interface (as shown in the following example ).
3. Added support for gradient colors.
The original gradient effect in GDI is complicated. The following example details the gradient in GDI +.
Application 2
Preparations
When using GDI +, you must first include the relevant header file. There is only one header file, as shown below:
# Include <gdiplus. h>
The gdiplus. h file is included in the include directory of vs2005, so in vs2005, you can directly include it in the form of "<>.
You also need to use a lib, Which is Gdiplus. lib.
# Pragma comment (lib, "Gdiplus. lib ")
You can also add them directly in "project properties-linker-input". The effect is the same.
In order not to confuse the relevant classes, constants, and struct of the GDI + program with other applications, the GDI + program has its own namespace, Gdiplus. When you want to use APIs related to the GDI + program, the following statement is required:
Using namespace Gdiplus;
Finally, GDI + has an Application Switch, GdiplusStartup and GdiplusShutdown, one on and one off. Before using the GDI + function, call GdiplusStartup first. When the program ends, call GdiplusShutdown to close it. for example, in the mfc-based dialog box application, I usually do this. Assume that my project is MyGDIPlusTest, first in MyGDIPlusTest. open GDI + in the InitInstance function of cpp, as shown below:
.....
CWinApp: InitInstance ();
GdiplusStartup (& m_gdiPlusToken, & m_gdiPlusStartupInput, NULL );
.....
Then, reload the ExitInstance function and disable the GDI +
GdiplusShutdown (m_gdiPlusToken); // close when the program exits
Return CWinApp: ExitInstance ();
Check the parameters of the two functions by yourself.
Example
1. First look at an example in msdn.
View plaincopy to clipboardprint? # Include <stdafx. h>
# Include <windows. h>
# Include <objidl. h>
# Include <gdiplus. h>
Using namespace Gdiplus;
# Pragma comment (lib, "Gdiplus. lib ")
VOID OnPaint (HDC hdc)
{
Graphics graphics (hdc );
Pen pen (Color (255, 0, 0,255 ));
Graphics. DrawLine (& pen, 0, 0,200,100 );
}
Lresult callback WndProc (HWND, UINT, WPARAM, LPARAM );
Int winapi WinMain (HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Initialize GDI +.
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 );
Return msg. wParam;
} // WinMain
Lresult callback WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
Switch (message)
{
Case WM_PAINT:
Hdc = BeginPaint (hWnd, & ps );
OnPaint (hdc );
EndPaint (hWnd, & ps );
Return 0;
Case WM_DESTROY:
PostQuitMessage (0 );
Return 0;
Default:
Return DefWindowProc (hWnd, message, wParam, lParam );
}
} // WndProc
# Include <stdafx. h>
# Include <windows. h>
# Include <objidl. h>
# Include <gdiplus. h>
Using namespace Gdiplus;
# Pragma comment (lib, "Gdiplus. lib ")
VOID OnPaint (HDC hdc)
{
Graphics graphics (hdc );
Pen pen (Color (255, 0, 0,255 ));
Graphics. DrawLine (& pen, 0, 0,200,100 );
}
Lresult callback WndProc (HWND, UINT, WPARAM, LPARAM );
Int winapi WinMain (HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
// Initialize GDI +.
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 );
Return msg. wParam;
} // WinMain
Lresult callback WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
Switch (message)
{
Case WM_PAINT:
Hdc = BeginPaint (hWnd, & ps );
OnPaint (hdc );
EndPaint (hWnd, & ps );
Return 0;
Case WM_DESTROY:
PostQuitMessage (0 );
Return 0;
Default:
Return DefWindowProc (hWnd, message, wParam, lParam );
}
} // WndProc
This example is line-drawn. If it is a print string, only the content in OnPaint is modified as follows:
View plaincopy to clipboardprint? VOID OnPaint (HDC hdc)
{
Graphics graphics (hdc );
SolidBrush brush (Color (255, 0, 0,255 ));
FontFamily fontFamily (L "Times New Roman ");
Font font (& fontFamily, 24, FontStyleRegular, UnitPixel );
PointF pointF (10.0f, 20366f );
Graphics. DrawString (L "Hello World! ",-1, & font, pointF, & brush );
}
VOID OnPaint (HDC hdc)
{
Graphics graphics (hdc );
SolidBrush brush (Color (255, 0, 0,255 ));
FontFamily fontFamily (L "Times New Roman ");
Font font (& fontFamily, 24, FontStyleRegular, UnitPixel );
PointF pointF (10.0f, 20366f );
Graphics. DrawString (L "Hello World! ",-1, & font, pointF, & brush );
}
2. A simple example of self-written Image Display
Click the mfc-based dialog box to display an image. Only key code is displayed.
View plaincopy to clipboardprint? Void CGDI_Plus_TestDlg: OnBnClickedButtonPen ()
{
// TODO: Add your control notification handler code here
HDC hdc = this-> GetDC ()-> m_hDC;
Graphics graphics (hdc );
Image image (L "c: \ image \ ip.jpg ");
Graphics. DrawImage (& image, 200,200 );
}
Void CGDI_Plus_TestDlg: OnBnClickedButtonPen ()
{
// TODO: Add your control notification handler code here
HDC hdc = this-> GetDC ()-> m_hDC;
Graphics graphics (hdc );
Image image (L "c: \ image \ ip.jpg ");
Graphics. DrawImage (& image, 200,200 );
}
The following points should be noted,
First, Image only supports instance initialization in the form of path and stream, but does not support resource handle. If you want to use the form of resource handle, you can first read the resource data to the stream, I will not talk about it here.
Second, there are many overload functions for DrawImage, which provide almost all the functions you can think of. Here I use the following overload
Status DrawImage (Image * image, INT x, INT y );
That is, display the loaded image at the specified position.
3 gradient effect
As mentioned above, GDI + supports gradient. There are two types: linear gradient and path gradient. The first type is discussed here.
Linear gradient is further divided
Horizontal Linear Gradients,
Customizing Linear Gradients,
Diagonal Linear Gradients
Three. These are better understood. The following sample code comes from msdn and I have made some modifications.
View plaincopy to clipboardprint? Void CGDI_Plus_TestDlg: OnBnClickedButtonPen ()
{
// TODO: Add your control notification handler code here
HDC hdc = this-> GetDC ()-> m_hDC;
Graphics graphics (hdc );
LinearGradientBrush linearBrush (Point (0, 10), Point (200, 10 ),
Color (255,255, 0,255), Color ));
Pen pen (& linearBrush, 4 );
Graphics. DrawLine (& pen, 100,100,200,100 );
Graphics. FillEllipse (& linearBrush, 100,150,200,100 );
Graphics. FillRectangle (& linearBrush, 100,255,500, 30 );
}
Void CGDI_Plus_TestDlg: OnBnClickedButtonPen ()
{
// TODO: Add your control notification handler code here
HDC hdc = this-> GetDC ()-> m_hDC;
Graphics graphics (hdc );
LinearGradientBrush linearBrush (Point (0, 10), Point (200, 10 ),
Color (255,255, 0,255), Color ));
Pen pen (& linearBrush, 4 );
Graphics. DrawLine (& pen, 100,100,200,100 );
Graphics. FillEllipse (& linearBrush, 100,150,200,100 );
Graphics. FillRectangle (& linearBrush, 100,255,500, 30 );
}
The program running effect is as follows:
Appendix: GDI + and GDI hybrid programming
GDI + provides a mechanism that can be used together with GDI. It mainly uses ReleaseHDC and GetHDC in Graphics. The following code example is from msdn:
View plaincopy to clipboardprint? VOID Example_GetReleaseHDC (Graphics * g)
{
Pen pen (Color (255, 0, 0,255 ));
G-> DrawEllipse (& pen, 10, 10,100, 50); // GDI +
HDC hdc = g-> GetHDC ();
// Make gdi cils, but don't call any methods
// On g until after the call to ReleaseHDC.
Rectangle (hdc, 120, 10,220, 60); // GDI
G-> ReleaseHDC (hdc );
// OK to call methods on g again.
G-> DrawLine (& pen, 240, 10,340, 60 );
}