字體及位置示例,字體位置示例
1 #include <Windows.h>
2 #include <tchar.h>
3 #include <math.h>
4 #define PI 3.1415926
5 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow);
6 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
7 HFONT CreateMyFont(TCHAR * fontName, int height, int lean);
8 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
9 {
10 MSG msg;
11 if (!InitWindowClass(hInstance, nCmdShow))
12 {
13 MessageBox(NULL, L"創建窗口失敗!", _T("創建窗口"), NULL);
14 return 1;
15 }
16 while (GetMessage(&msg, NULL, 0, 0))
17 {
18 TranslateMessage(&msg);
19 DispatchMessage(&msg);
20 }
21 return(int)msg.wParam;
22 }
23 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
24 {
25 HDC hDC;
26 PAINTSTRUCT ps;
27 HFONT font;
28 HPEN hPen;
29 LPWSTR title = L"登高 唐·杜甫", poem[8] = { L"風急天高猿嘯哀", L"渚清沙白鳥飛回", L"無邊落木蕭蕭下", L"不盡長江滾滾來", L"萬裡悲秋常作客", L"百年多病獨登台", L"艱難苦恨繁霜鬓", L"潦倒新停濁酒杯" };
30 int r, r0, i, j = -1, fontSize, fontSize0, color;
31 RECT clientDimension;
32 POINT begin, end, org;
33 double sita;
34 switch (message)
35 {
36 case WM_SIZE:
37 InvalidateRect(hWnd, NULL, true);
38 break;
39 case WM_PAINT:
40 hDC = BeginPaint(hWnd, &ps);
41 hPen = CreatePen(PS_DASH, 1, RGB(127, 127, 127));
42 SelectObject(hDC, hPen);
43 GetClientRect(hWnd, &clientDimension);
44 if ((clientDimension.right - clientDimension.left) < 400 || (clientDimension.bottom - clientDimension.top) < 300)
45 {
46 MessageBox(hWnd, L"屏幕尺寸大小,無法繪圖!", L"錯誤信息", 0);
47 break;
48 }
49 r = (clientDimension.bottom - clientDimension.top) * 8 / 10;
50 org.x = (clientDimension.right - clientDimension.left) / 2;
51 org.y = (clientDimension.bottom - clientDimension.top) * 9 / 10;
52 Arc(hDC, org.x - r, org.y - r, org.x + r, org.y + r, org.x + (int)(r*sin(PI / 3)), org.y - (int)(r*cos(PI / 3)), org.x - (int)(r*sin(2 * PI / 3)), org.y + (int)(r*cos(2 * PI / 3)));
53 for (sita = PI / 6; sita <= PI * 5 / 6; sita += PI * 2 / 27)
54 {
55 begin.x = org.x - (int)(r*cos(sita));
56 begin.y = org.y - (int)(r*sin(sita));
57 MoveToEx(hDC, begin.x, begin.y, NULL);
58 end.x = org.x;
59 end.y = org.y;
60 LineTo(hDC, end.x, end.y);
61 }
62 r0 = r * 2 / 5;
63 Arc(hDC, org.x - r0, org.y - r0, org.x + r0, org.y + r0, org.x + (int)(r0*sin(PI / 3)), org.y - (int)(r0*cos(PI / 3)), org.x - (int)(r0*sin(2 * PI / 3)), org.y + (int)(r0*cos(2 * PI / 3)));
64 sita = PI / 6 + PI * 4 / 15 / 5;
65 fontSize0 = fontSize = (r - r0) / 7;
66 r0 = r - 20;
67 for (i = 0; i < 7; i++)
68 {
69 LPCWSTR outInfo = &title[i];
70 fontSize -= 3;
71 font = CreateMyFont(L"楷體_GB2312", fontSize - 5, -(sita + PI / 15) * 1800 / PI);
72 SelectObject(hDC, font);
73 begin.x = org.x + (int)(r0*cos(sita));
74 begin.y = org.y - (int)(r0*sin(sita));
75 TextOut(hDC, begin.x, begin.y, outInfo, 1);
76 r0 -= fontSize;
77 DeleteObject(font);
78 }
79 for (sita = PI / 6 + PI * 4 / 27 - PI / 40; sita < PI * 5 / 6; sita += PI * 2 / 27)
80 {
81 fontSize = fontSize0;
82 r0 = r - 20;
83 j++;
84 color = 0;
85 for (i = 0; i < 7; i++)
86 {
87 color += 255 / 7;
88 SetTextColor(hDC, RGB(255 - color, 0, color));
89 LPCWSTR outInfo = &poem[j][i];
90 fontSize -= 3;
91 font = CreateMyFont(L"華文行楷", fontSize, (int)(((sita - PI / 2) * 1800 / PI)) % 3600);
92 SelectObject(hDC, font);
93 begin.x = org.x + (int)(r0*cos(sita));
94 begin.y = org.y - (int)(r0*sin(sita));
95 TextOut(hDC, begin.x, begin.y, outInfo, 1);
96 r0 -= fontSize;
97 DeleteObject(font);
98 Sleep(10);
99 }
100 }
101 EndPaint(hWnd, &ps);
102 break;
103 case WM_DESTROY:
104 PostQuitMessage(0);
105 break;
106 default:
107 return DefWindowProc(hWnd, message, wParam, lParam);
108 break;
109 }
110 return 0;
111 }
112 HFONT CreateMyFont(TCHAR * fontName, int height, int lean)
113 {
114 return CreateFont(
115 height,
116 0,
117 lean,
118 0,
119 FW_HEAVY,
120 0,
121 0,
122 0,
123 GB2312_CHARSET,
124 OUT_DEFAULT_PRECIS,
125 CLIP_DEFAULT_PRECIS,
126 DEFAULT_QUALITY,
127 DEFAULT_PITCH | FF_DONTCARE,
128 fontName
129 );
130 }
131 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow)
132 {
133 WNDCLASSEX wcex;
134 HWND hWnd;
135 TCHAR szWindowClass[] = L"窗口示例";
136 TCHAR szTitle[] = L"字體及位置示例";
137 wcex.cbSize = sizeof(WNDCLASSEX);
138 wcex.style = 0;
139 wcex.lpfnWndProc = WndProc;
140 wcex.cbClsExtra = 0;
141 wcex.cbWndExtra = 0;
142 wcex.hInstance = hInstance;
143 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
144 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
145 wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
146 wcex.lpszMenuName = NULL;
147 wcex.lpszClassName = szWindowClass;
148 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
149 if (!RegisterClassEx(&wcex))
150 return FALSE;
151 hWnd = CreateWindow(
152 szWindowClass,
153 szTitle,
154 WS_OVERLAPPEDWINDOW,
155 CW_USEDEFAULT, CW_USEDEFAULT,
156 CW_USEDEFAULT, CW_USEDEFAULT,
157 NULL,
158 NULL,
159 hInstance,
160 NULL
161 );
162 if (!hWnd)
163 return FALSE;
164 ShowWindow(hWnd, nCmdShow);
165 UpdateWindow(hWnd);
166 return TRUE;
167 }