C ++ win32 development of GDIPlus textures

Source: Internet
Author: User

// Add Gdiplus. lib

// Use three images to name 2.png 3.png 9.png respectively.

// Use VC or vs to create an empty project win32, add the file winmain. cpp, and add the following content to the file

# Include <windows. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <gdiplus. h>

Bool m_bTest = true;

WCHAR strImgName [100];

Int m_number = 0;

/* GDI + startup token */
ULONG_PTR gdiplusStartupToken;

PAINTSTRUCT ps; // point structure

Int m_waveData [300];

// Message callback function, including interface messages
Lresult callback MyWindowProc (
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);

// Main function entry
Int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
Int nCmdShow)
{

/* Initialize GDIpuls */
Gdiplus: GdiplusStartupInput gdiInput;
Gdiplus: GdiplusStartup (& gdiplusStartupToken, & gdiInput, NULL );

// Initialize the image name

Wcscpy (strImgName, L "2.png ");

WNDCLASS wndclass; // form class
Wndclass. cbClsExtra = 0;
Wndclass. cbWndExtra = 0;
Wndclass. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH );
Wndclass. hCursor = LoadCursor (0, IDC_HAND );
Wndclass. hIcon = LoadIcon (0, IDI_INFORMATION );
Wndclass. hInstance = hInstance;
Wndclass. lpfnWndProc = MyWindowProc;
Wndclass. lpszClassName = TEXT ("hg ");
Wndclass. lpszMenuName = NULL;
Wndclass. style = CS_HREDRAW | CS_VREDRAW; // specify the form style
// Registration type
If (! RegisterClass (& wndclass ))
{
MessageBox (0, TEXT ("reg err"), TEXT ("reg err"), 0 );
}
// Create a form
HWND hwnd = CreateWindow (TEXT ("hg"), TEXT ("Helloworldhhh"), WS_OVERLAPPEDWINDOW, 0, 0, 1024,768, NULL, NULL, hInstance, NULL );
If (! Hwnd)
{
MessageBox (hwnd, TEXT ("create windows err"), TEXT ("create windows err"), 0 );
}
// Display form
ShowWindow (hwnd, SW_SHOW );
UpdateWindow (hwnd );

MSG msg;
// Message Processing
While (GetMessage (& msg, 0, 0, 0 ))
{
TranslateMessage (& msg );
DispatchMessage (& msg );
}

Return 0;
}

Lresult callback MyWindowProc (
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
Int wmId, wmEvent;

Switch (uMsg)
{
Case WM_CLOSE:
{
Gdiplus: GdiplusShutdown (gdiplusStartupToken );
PostQuitMessage (0 );
}
Break;
Case WM_DESTROY:
PostQuitMessage (0 );
Break;
Case WM_RBUTTONDBLCLK: // right-click
// MessageBox (NULL, "dd", "ss", MB_ OK );
// DestroyWindow (hwnd );
Break;
Case WM_LBUTTONDOWN: // Click left to switch the display image
If (m_bTest)
{
Wcscpy (strImgName, L "3.png ");
M_bTest = false;
} Else
{
Wcscpy (strImgName, L "2.png ");
M_bTest = true;
}

RedrawWindow (hwnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW );

Break;
Case WM_KEYDOWN: // Keyboard Message
{
Switch (wParam)
{
Case VK_LEFT: // left-click
{
Wcscpy (strImgName, L "3.png ");
}
Break;
Case VK_RIGHT: // right-click
{
Wcscpy (strImgName, L "2.png ");
}
Break;
Case VK_ESCAPE:
{
Gdiplus: GdiplusShutdown (gdiplusStartupToken );
PostQuitMessage (0 );
}
Break;
}
// When there is an interface modification, update and redraw. Remember not to redraw the background.
RedrawWindow (hwnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW );
}
Break;
Case WM_PAINT:
{

HDC hdc; // DC handle
HPEN hPen; // pen handle
HPEN hPenOld; // The handle used to save the original pen.
Hdc = BeginPaint (hwnd, & ps); // obtain the DC handle and start painting. hWnd is the window handle.

Using namespace Gdiplus; // Add to namespace

RECT rc;
GetClientRect (hwnd, & rc );
Bitmap bmp (int (rc. right), int (rc. bottom ));

Graphics bmp Graphics (& bmp );
BMP graphics. SetSmoothingMode (SmoothingModeAntiAlias );

/* Drawing on bitmap */
// SolidBrush bkBrush (Color (0, 0 ));
// BMP graphics. FillRectangle (& bkBrush, 0, 0, rc. right, rc. bottom );

Gdiplus: Image image2 (L "9.png ");

Gdiplus: Image image (strImgName );

Gdiplus: Graphics graphics (hdc );

BMP graphics. DrawImage (& image2, 600,400 );
BMP graphics. DrawImage (& image, 100,100,300,100 );

/* Important! Create a CacheBitmap object for quick drawing */

BMP graphics. DrawImage (& image2, 600,400 );
BMP graphics. DrawImage (& image, 100,100,300,100 );

Gdiplus: FontFamily fontFamily (L "Times New Roman"); // create a font family object

Gdiplus: Font font (& fontfamily, 20); // create the Times New Roman font of the 5th character size

Gdiplus: solidbrush brush (gdiplus: color (255,128, 50); // create a green solid brush (for string writing)

For (INT I = 0; I <4; I ++)
{
BMP graphics. drawstring (strimgname,-1, & font, gdiplus: pointf (400,110 * I + 20), & brush );
}

Cachedbitmap cachedbmp (& BMP, & graphics );

// Submit at the same time to avoid flickering screen

Graphics. DrawCachedBitmap (& cachedBmp, 0, 0 );

Gdiplus: Pen myPen (Gdiplus: Color (255,128, 50 ));

// For (int I = 0; I <300; I ++)
//{
// Graphics. DrawLine (& myPen, 300,200 );
//}

HPen = CreatePen (PS_SOLID, 3, RGB (255, 0, 0); // create a red paint brush with a width of 3
HPenOld = (HPEN) SelectObject (hdc, hPen); // select DC
Movetoex (HDC, 20, 10, null); // The last parameter is the structure pointer of the old current vertex returned.
// Lineto (HDC, 200,100); // draw a line

Deleteobject (Hpen); // Delete the pen you have created

Endpaint (hwnd, & PS); // draw end

}
Break;
// Case wm_erasebkgnd:
// Break;
Default:
Return defwindowproc (hwnd, umsg, wparam, lparam );
}
Return 0;
}

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.