1 #include <Windows.h>
2 #include <tchar.h>
3 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow);
4 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
5 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
6 {
7 MSG msg;
8 if (!InitWindowClass(hInstance, nCmdShow))
9 {
10 MessageBox(NULL, L"创建窗口失败!", _T("创建窗口"), NULL);
11 return 1;
12 }
13 while (GetMessage(&msg, NULL, 0, 0))
14 {
15 TranslateMessage(&msg);
16 DispatchMessage(&msg);
17 }
18
19 return(int)msg.wParam;
20 }
21
22 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
23 {
24 HDC hDC;
25 PAINTSTRUCT PtStr;
26 HBRUSH hBrush;
27 HPEN hPen;
28 static int dispMode = -1;
29 LPCTSTR str;
30 switch (message)
31 {
32 case WM_LBUTTONDOWN:
33 InvalidateRect(hWnd, NULL, TRUE);
34 break;
35 case WM_PAINT:
36 hDC = BeginPaint(hWnd, &PtStr);
37 dispMode = (dispMode + 1) % 6;
38 switch (dispMode)
39 {
40 case 0:
41 str = _T("映射方式MM_TEXT:缺省的映射方式");
42 SetMapMode(hDC, MM_TEXT); //设置映射方式为缺省方式
43 TextOut(hDC, 0, 0, str, _tcsclen(str)); //输出映射方式及映射比例
44 break;
45 case 1:
46 str = _T("映射方式MM_ISOTROPIC:窗口坐标为20×20,映射为视口尺寸为10×10,图形缩小1倍");
47 SetMapMode(hDC, MM_ISOTROPIC); //设置映射方式
48 SetWindowExtEx(hDC, 20, 20, NULL); //窗口矩形为×20
49 SetViewportExtEx(hDC, 10, 10, NULL); //映射为视口的矩形为×10
50 TextOut(hDC, 0, 0, str, _tcsclen(str));
51 break;
52 case 2:
53 str = _T("映射方式MM_ISOTROPIC:窗口坐标为10×10,映射为视口尺寸为20×20,图形放大1倍");
54 SetMapMode(hDC, MM_ISOTROPIC);
55 SetWindowExtEx(hDC, 10, 10, NULL); //窗口矩形为10×10
56 SetViewportExtEx(hDC, 20, 20, NULL); //映射为视口的矩形为20×20
57 TextOut(hDC, 0, 0, str, _tcsclen(str));
58 break;
59 case 3:
60 str = _T("映射方式MM_ANISOTROPIC:窗口坐标为10×10,映射为视口尺寸为20×10,图形横向放大1倍,纵向不变");
61 SetMapMode(hDC, MM_ANISOTROPIC);
62 SetWindowExtEx(hDC, 10, 10, NULL); //窗口矩形为10×10
63 SetViewportExtEx(hDC, 20, 10, NULL); //映射为视口的矩形为20×10
64 TextOut(hDC, 0, 0, str, _tcsclen(str));
65 break;
66 case 4:
67 str = _T("映射方式MM_ANISOTROPIC:窗口坐标为10×10,映射为视口尺寸为20×5,图形横向放大1倍,纵向缩小1倍");
68 SetMapMode(hDC, MM_ANISOTROPIC);
69 SetWindowExtEx(hDC, 10, 10, NULL); //窗口矩形为10×10
70 SetViewportExtEx(hDC, 20, 5, NULL); //映射为视口的矩形为20×5
71 TextOut(hDC, 0, 0, str, _tcsclen(str));
72 break;
73 case 5:
74 str = _T("映射方式MM_ISOTROPIC:窗口坐标为10×10,映射为视口尺寸为20×5,图形为了保持原纵横比,系统会调整映射比例");
75 SetMapMode(hDC, MM_ISOTROPIC);
76 SetWindowExtEx(hDC, 10, 10, NULL); //窗口矩形为10×10
77 SetViewportExtEx(hDC, 20, 5, NULL); //映射为视口的矩形为20×5
78 TextOut(hDC, 0, 0, str, _tcsclen(str));
79 break;
80 }
81 hPen = (HPEN)GetStockObject(BLACK_PEN); //设置画笔为系统预定义的黑色画笔
82 hBrush = (HBRUSH)GetStockObject(DKGRAY_BRUSH); //深灰色画刷
83 SelectObject(hDC, hBrush); //选择画刷
84 SelectObject(hDC, hPen); //选择画笔
85 RoundRect(hDC, 50, 120, 100, 200, 15, 15); //圆角矩形
86 hBrush = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //淡灰色画刷
87 SelectObject(hDC, hBrush); //选择画刷
88 Ellipse(hDC, 150, 50, 200, 150); //椭圆
89 hBrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH); //采用系统预定义的虚画刷
90 SelectObject(hDC, hBrush); //选择画刷
91 Pie(hDC, 250, 50, 300, 100, 250, 50, 300, 50); //饼形
92 EndPaint(hWnd, &PtStr); //结束绘画
93 break;
94 case WM_DESTROY:
95 PostQuitMessage(0); //调用PostQuitMessage发出WM_QUIT消息
96 break;
97 default:
98 return DefWindowProc(hWnd, message, wParam, lParam);//默认时采用系统消息默认处理函数
99 break;
100 }
101 return 0;
102 }
103
104 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow)
105 {
106 WNDCLASSEX wcex;
107 HWND hWnd;
108 TCHAR szWindowClass[] = L"窗口示例";
109 TCHAR szTitle[] = L"映射模式及填充示例图";
110 wcex.cbSize = sizeof(WNDCLASSEX);
111 wcex.style = 0;
112 wcex.lpfnWndProc = WndProc;
113 wcex.cbClsExtra = 0;
114 wcex.cbWndExtra = 0;
115 wcex.hInstance = hInstance;
116 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
117 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
118 wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
119 wcex.lpszMenuName = NULL;
120 wcex.lpszClassName = szWindowClass;
121 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
122 if (!RegisterClassEx(&wcex))
123 return FALSE;
124 hWnd = CreateWindow(
125 szWindowClass,
126 szTitle,
127 WS_OVERLAPPEDWINDOW,
128 CW_USEDEFAULT, CW_USEDEFAULT,
129 CW_USEDEFAULT, CW_USEDEFAULT,
130 NULL,
131 NULL,
132 hInstance,
133 NULL
134 );
135 if (!hWnd)
136 return FALSE;
137 ShowWindow(hWnd, nCmdShow);
138 UpdateWindow(hWnd);
139 return TRUE;
140 }