Use GDI + in non-managed C ++

xiaoxiao2021-03-06  82

After using the GDI write process in C #, I like this new graphics device interface. However, you can only run my program on a computer with a .NET framework, which makes me feel uncomfortable. When I found "real" GDI code is in a single, there is no hosted GDIPLUS.DLL DLL implementation, I am like finding new mainland, I am happy, I can run in "real" computer to use GDI The program, I want this speed, it will be more faster than in C #, do it, do it, open MSDN, step by step.

Microsoft said that GDI can be used in all Windows-based applications, including 64-bit Windows versions (excluding Win3.x). You only need to copy GDIPLUS.DLL to the system directory of Windows, you can use the application that requires GDI support. Used in unmanaged C , you only need to include gdiplus.h header files, and then contain the GDIPLUS.LIB library file in the connection settings.

Ok, let's take a look at how to use it in C with the simplest task - draw lines and letters.

First, picture line

With GDI line, you need these objects: Graphics, Pen, Color. Graphics provides a Graphics :: Drawline method, PEN-saved properties such as color, width, and so on. Use the address of the PEN object as a parameter for the Graphics :: Drawline method. Or look at this simple SDK program. What we need to pay attention is that in the WinMain function, we need to call GDIPLUSSTARTUP and GDIPLUSSHUTDOWN.

#define unicode

#include

#include

Using namespace gdiplus;

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 = 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);

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

Second, the writing of GDI writing only needs to replace the onpain function to the following. Void OnPaint (HDC HDC)

{

Graphics Graphics (HDC);

Solidbrush Brush (Color (255, 0, 0, 255);

Fontfamily Fontfamily (l "Song");

Font Font (& FontFamily, 24, Fontstyleregular, Unitpixel);

Pointf PointF (10.0F, 20.0F);

Graphics.drawstring (l "Hello! GDI ", -1, & font, pointf, & brush;} Note, because the control Wchar * type is used because the DrawString function is required, so use L "sting" form. Haha, write it like this, you will take a brick!

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

New Post(0)