📄 demo8_1.c
字号:
1 /****************************************************************/
2 /* Demo8_1 --- Print Test */
3 /****************************************************************/
4
5 #include <windows.h>
6 #include "demo8_1.h"
7
8
9 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
10 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
11
12 void DrawGraph(HDC, BOOL);
13 void DrawPencil(HDC);
14 void DrawLine(HDC, BOOL);
15
16 void PrintGraph(HWND);
17
18
19 int ToolID = IDM_PENCIL;
20 POINT OrgPoint;
21 POINT PrePoint;
22 POINT CurPoint;
23
24 int CX, CY;
25
26 /****************************************************************/
27 /* WinMain() */
28 /****************************************************************/
29
30 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
31 LPSTR lpszCmdLine, int nCmdShow)
32 {
33 WNDCLASS wclass;
34 MSG msg;
35 HWND hWnd;
36 char szName[] = "Demo8_1";
37
38 if (!hPrevInstance)
39 {
40 wclass.style = CS_HREDRAW | CS_VREDRAW;
41 wclass.lpfnWndProc = MainWndProc;
42 wclass.cbClsExtra = 0;
43 wclass.cbWndExtra = 0;
44 wclass.hInstance = hInstance;
45 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
46 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
47 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
48 wclass.lpszMenuName = szName;
49 wclass.lpszClassName = szName;
50
51 if (!RegisterClass (&wclass))
52 return (FALSE);
53 }
54
55 hWnd = CreateWindow(
56 szName,
57 "Print Test" ,
58 WS_OVERLAPPEDWINDOW,
59 CW_USEDEFAULT,
60 CW_USEDEFAULT,
61 CW_USEDEFAULT,
62 CW_USEDEFAULT,
63 NULL,
64 NULL,
65 hInstance,
66 NULL );
67
68 if (!hWnd)
69 return (FALSE);
70
71 ShowWindow(hWnd, nCmdShow);
72 UpdateWindow(hWnd);
73
74 while (GetMessage(&msg, NULL, NULL,NULL))
75 {
76 TranslateMessage(&msg);
77 DispatchMessage(&msg);
78 }
79 return (msg.wParam);
80 }
81
82
83
84 /****************************************************************/
85 /* MainWndProc() */
86 /****************************************************************/
87
88 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
89 WORD wParam, LONG lParam)
90 {
91 HDC hDC;
92 HMENU hMenu;
93 static BOOL bLBDown;
94
95 switch (message)
96 {
97 case WM_CREATE :
98 hMenu = GetMenu(hWnd);
99 CheckMenuItem(hMenu, IDM_PENCIL,
100 MF_CHECKED);
101 return (0);
102
103 case WM_COMMAND :
104 hMenu = GetMenu(hWnd);
105 switch (wParam)
106 {
107 case IDM_PENCIL :
108 case IDM_LINE :
109
110 if (ToolID == wParam)
111 return (0);
112
113 CheckMenuItem(hMenu, ToolID,
114 MF_UNCHECKED);
115 ToolID = wParam;
116 CheckMenuItem(hMenu, ToolID,
117 MF_CHECKED);
118 break;
119
120 case IDM_CLEAR :
121 InvalidateRect(hWnd, NULL, TRUE);
122 break;
123
124 case IDM_PRINT :
125 PrintGraph(hWnd);
126 break;
127
128 case IDM_QUIT :
129 DestroyWindow(hWnd);
130 break;
131 }
132 return (0);
133
134 case WM_SIZE :
135 CX = LOWORD(lParam);
136 CY = HIWORD(lParam);
137 return (0);
138
139 case WM_LBUTTONDOWN :
140 SetCapture(hWnd);
141 bLBDown = TRUE;
142
143 OrgPoint = MAKEPOINT(lParam);
144 CurPoint = PrePoint = OrgPoint;
145
146 return (0);
147
148 case WM_LBUTTONUP :
149 bLBDown = FALSE;
150 ReleaseCapture();
151
152 hDC = GetDC(hWnd);
153 DrawGraph(hDC, TRUE);
154 ReleaseDC(hWnd, hDC);
155
156 return (0);
157
158 case WM_MOUSEMOVE :
159 if (bLBDown)
160 {
161 PrePoint = CurPoint;
162 CurPoint = MAKEPOINT(lParam);
163
164 hDC = GetDC(hWnd);
165 DrawGraph(hDC, FALSE);
166 ReleaseDC(hWnd, hDC);
167 }
168 return (0);
169
170 case WM_DESTROY :
171 PostQuitMessage(0);
172 return (0);
173
174 default :
175 return(DefWindowProc(hWnd, message, wParam, lParam));
176 }
177 }
178
179
180
181 void DrawGraph(HDC hDC, BOOL bSure)
182 {
183 switch (ToolID)
184 {
185 case IDM_PENCIL :
186 DrawPencil(hDC);
187 break;
188
189 case IDM_LINE :
190 DrawLine(hDC, bSure);
191 break;
192 }
193 }
194
195
196
197 void DrawPencil(HDC hDC)
198 {
199 MoveTo(hDC, PrePoint.x, PrePoint.y);
200 LineTo(hDC, CurPoint.x, CurPoint.y);
201 }
202
203
204
205 void DrawLine(HDC hDC, BOOL bSure)
206 {
207 int nDrawMode;
208
209 if (! bSure)
210 {
211 nDrawMode = SetROP2(hDC, R2_NOT);
212
213 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
214 LineTo(hDC, PrePoint.x, PrePoint.y);
215
216 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
217 LineTo(hDC, CurPoint.x, CurPoint.y);
218
219 SetROP2(hDC, nDrawMode);
220 }
221 else
222 {
223 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
224 LineTo(hDC, CurPoint.x, CurPoint.y);
225 }
226 }
227
228
229
230 HDC CreateDC_Printer()
231 {
232 HDC hPrnDC;
233 char szProfile[70];
234 char *szDriver, *szDevice, *szOutput;
235
236 GetProfileString("windows", "device", "", szProfile, 70);
237
238 szDevice = (char *) strtok(szProfile, ",");
239 szDriver = (char *) strtok(NULL, ",");
240 szOutput = (char *) strtok(NULL, ",");
241
242 if (szDevice && szDriver && szOutput)
243 {
244 hPrnDC = CreateDC(szDriver, szDevice, szOutput, NULL);
245 return (hPrnDC);
246 }
247
248 return (NULL);
249 }
250
251
252
253 void PrintGraph(HWND hWnd)
254 {
255 HDC hDC, hMemDC;
256 HDC hPrnDC;
257 HBITMAP hBitmap;
258 BOOL bPrinted = TRUE;
259 char szName[] = "Demo8_1 -- Print test";
260
261 hPrnDC = CreateDC_Printer();
262 if (hPrnDC == NULL)
263 {
264 MessageBox(hWnd, "Printer Error", NULL,
265 MB_OK | MB_ICONHAND);
266 return ;
267 }
268
269 hDC = GetDC(hWnd);
270 hMemDC = CreateCompatibleDC(hDC);
271 hBitmap = CreateCompatibleBitmap(hDC, CX, CY);
272
273 SelectObject(hMemDC, hBitmap);
274 BitBlt(hMemDC, 0, 0, CX, CY, hDC, 0, 0, SRCCOPY);
275 ReleaseDC(hWnd, hDC);
276
277 if (Escape(hPrnDC, STARTDOC,
278 strlen(szName), szName, NULL) > 0)
279 {
280 BitBlt(hPrnDC, 0, 0, CX, CY, hMemDC, 0, 0, SRCCOPY);
281
282 if (Escape(hPrnDC, NEWFRAME, 0, NULL, NULL) > 0)
283 Escape(hPrnDC, ENDDOC, 0, NULL, NULL);
284 else
285 bPrinted = FALSE;
286 }
287 else
288 bPrinted = FALSE;
289
290 if (! bPrinted)
291 MessageBox(hWnd, "Print Error", NULL,
292 MB_OK | MB_ICONHAND);
293
294 DeleteDC(hPrnDC);
295 DeleteDC(hMemDC);
296 DeleteObject(hBitmap);
297 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -