这个程序的特点:
使用方法:创建Win32项目,点上空项目,去掉SDL安全检查,然后新建一个cpp文件,贴里边就行了。
// GDIstatic.cpp - 使用GDI/GDI+进行静态绘制
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
#include <math.h>
HINSTANCE hInst;
HWND hMainWnd;
static float locx = 320, locy = 240;
// 窗口消息函数,在这里处理窗口消息
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_CREATE) // 窗口创建
{
return 0;
}
if (msg == WM_KEYDOWN) // 按下键
{
if (wParam == VK_LEFT)
{
locx -= 5.0f;
InvalidateRect(hWnd, NULL, FALSE);
return 0;
}
if (wParam == VK_RIGHT)
{
locx += 5.0f;
InvalidateRect(hWnd, NULL, FALSE);
return 0;
}
if (wParam == VK_UP)
{
locy -= 5.0f;
InvalidateRect(hWnd, NULL, FALSE);
return 0;
}
if (wParam == VK_DOWN)
{
locy += 5.0f;
InvalidateRect(hWnd, NULL, FALSE);
return 0;
}
}
if (msg == WM_PAINT) // 窗口需要重绘
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
if (hdc) // 进行图形绘制(这里使用GDI+)
{
using namespace Gdiplus;
Graphics g(hdc);
g.Clear(Color::White);
Pen blackpen(Color::Black, 1);
g.DrawLine(&blackpen, PointF(locx, locy - 100), PointF(locx, locy + 100));
g.DrawLine(&blackpen, PointF(locx - 100, locy), PointF(locx + 100, locy));
g.DrawEllipse(&blackpen, RectF(locx - 100, locy - 100, 200, 200));
}
EndPaint(hWnd, &ps);
return 0;
}
if (msg == WM_DESTROY) // 窗口被销毁
{
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
// 程序入口点
int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
hInst = hInstance;
#ifdef _GDIPLUS_H
ULONG_PTR gdip_token = 0;
Gdiplus::Status gdip_status = Gdiplus::GdiplusStartup(&gdip_token, &Gdiplus::GdiplusStartupInput(), NULL);
if (gdip_status != Gdiplus::Ok)
{
MessageBox(NULL, TEXT("GDI+ initialization failed."), TEXT("Error"), MB_ICONHAND);
return 0;
}
#endif
WNDCLASSEX wcex = {
sizeof wcex,
CS_VREDRAW|CS_HREDRAW,
WndProc,
0, 0,
hInstance,
LoadIcon(NULL, IDI_APPLICATION),
LoadCursor(NULL, IDC_ARROW),
NULL, // 我们将自己清空窗口,因此不需要背景画刷
NULL,
TEXT("MainWndClass"),
LoadIcon(NULL, IDI_APPLICATION),
};
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, TEXT("This program cannot be run in Windows 95/98/Me."), TEXT("Error"), MB_ICONHAND);
return 0;
}
RECT rcWindow = { 0, 0, 640, 480 };
AdjustWindowRectEx(&rcWindow, WS_OVERLAPPEDWINDOW, FALSE, 0); // 将客户区大小转换为窗口大小
hMainWnd = CreateWindowEx(
0, TEXT("MainWndClass"), TEXT("Main Window"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top,
NULL, NULL, hInstance, NULL);
if (hMainWnd == NULL)
{
MessageBox(NULL, TEXT("CreateWindowEx failed."), TEXT("Error"), MB_ICONHAND);
return 0;
}
ShowWindow(hMainWnd, nShowCmd);
UpdateWindow(hMainWnd);
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#ifdef _GDIPLUS_H
Gdiplus::GdiplusShutdown(gdip_token);
#endif
return (int)msg.wParam;
}
</math.h></gdiplus.h></windows.h>