完整代码实现:
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HPEN hP; //画笔句柄
HBRUSH hB; //画刷句柄
HFONT hF_black{}, hF_big{}; //定义两种字体句柄
TEXTMETRIC tm; //定义一个TEXTMETRIC结构,用以记录字体信息
SIZE size{};
static RECT rect = { 0,300,0,350 };
LPCSTR a[28];
a[6] = "故"; a[0] = "人"; a[1] = "西"; a[2] = "辞"; a[3] = "黄"; a[4] = "鹤"; a[5] = "楼";
a[13] = "烟"; a[7] = "花"; a[8] = "三"; a[9] = "月"; a[10] = "下"; a[11] = "扬"; a[12] = "州";
a[20] = "孤"; a[14] = "帆"; a[15] = "远"; a[16] = "影"; a[17] = "碧"; a[18] = "空"; a[19] = "尽";
a[27] = "唯"; a[21] = "见"; a[22] = "长"; a[23] = "江"; a[24] = "天"; a[25] = "际"; a[26] = "流";
int X = 0, Y = 0;
static long nXChar, nCaps, nYChar;
int pointx, pointy, j;
POINT point1[3] = { {150,50},{100,137},{50,50} };
POINT point2[5] = { {100,212},{70,227},{70,250},{130,250},{130,227} };
//定义一个SIZE类型的结构
switch (message)
{
case WM_TIMER:
if (wParam == 9999) //定时刷新
InvalidateRect(hwnd, NULL, true);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps); //取得设备环境句柄
SetMapMode(hdc, MM_TEXT); //设置映射模式
//绘制图形
hP = CreatePen(PS_SOLID, 0, COLORREF RGB(255, 0, 0));//红色画笔
SelectObject(hdc, hP);
Polygon(hdc, point1, 3);//画白色三角形
Arc(hdc, 63, 137, 138, 212, 100, 137, 100, 137);//画白色椭圆
Polygon(hdc, point2, 5);//画白色多边形
hB = CreateSolidBrush(COLORREF RGB(0, 0, 0));//黑色画刷
SelectObject(hdc, hB);
RoundRect(hdc, 213, 100, 287, 137, 20, 20);//黑色圆角矩形
Pie(hdc, 213, 137, 288, 212, 240, 137, 260, 137);//黑色饼图
Rectangle(hdc, 213, 212, 287, 250);//黑色矩形
//绘制古诗
SetTextColor(hdc, RGB(0, 0, 0));
SetBkColor(hdc, RGB(255, 255, 255));
GetTextMetrics(hdc, &tm); //获取字体信息
nXChar = tm.tmAveCharWidth; //获取字符宽度
nYChar = tm.tmHeight + tm.tmExternalLeading; //字符高度
nCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * nXChar / 2;
for (j = 0; j < 28; j++) //输出文本
{
pointx = 800 - j / 7 * 50;
pointy = 50 + ((j + 1) % 7) * (nYChar + nCaps);
TextOut(hdc, pointx, pointy, a[j], 2);
}
if (rect.right >= size.cx) rect.right = 0;
DeleteObject(hP);
DeleteObject(hB);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
TCHAR lpszClassName[] = "My_Drawing";
TCHAR lpszTitle[] = "My_Drawing";
wndclass.style = 0;
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 = lpszClassName;
if (!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hwnd = CreateWindow(lpszClassName, lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}