DEMO8_2.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:11k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo8_2 --- Abort Procedure */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "demo8_2.h"
- 7
- 8
- 9 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 10 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 11
- 12 BOOL FAR PASCAL AbortProc(HWND, short);
- 13 BOOL FAR PASCAL PrnDlgProc(HWND, WORD, WORD, LONG);
- 14
- 15 void DrawGraph(HDC, BOOL);
- 16 void DrawPencil(HDC);
- 17 void DrawLine(HDC, BOOL);
- 18
- 19 void PrintGraph(HWND);
- 20
- 21
- 22 FARPROC lpAbortProc;
- 23 FARPROC lpPrnDlgProc;
- 24
- 25 int ToolID = IDM_PENCIL;
- 26 POINT OrgPoint;
- 27 POINT PrePoint;
- 28 POINT CurPoint;
- 29
- 30 int CX, CY;
- 31
- 32 HANDLE hInst;
- 33 BOOL bCancel;
- 34 HWND hPrnDlg;
- 35
- 36
- 37 /****************************************************************/
- 38 /* WinMain() */
- 39 /****************************************************************/
- 40
- 41 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 42 LPSTR lpszCmdLine, int nCmdShow)
- 43 {
- 44 WNDCLASS wclass;
- 45 MSG msg;
- 46 HWND hWnd;
- 47 char szName[] = "Demo8_2";
- 48
- 49 if (!hPrevInstance)
- 50 {
- 51 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 52 wclass.lpfnWndProc = MainWndProc;
- 53 wclass.cbClsExtra = 0;
- 54 wclass.cbWndExtra = 0;
- 55 wclass.hInstance = hInstance;
- 56 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 57 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 58 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 59 wclass.lpszMenuName = szName;
- 60 wclass.lpszClassName = szName;
- 61
- 62 if (!RegisterClass (&wclass))
- 63 return (FALSE);
- 64 }
- 65
- 66 hWnd = CreateWindow(
- 67 szName,
- 68 "Abort Procedure" ,
- 69 WS_OVERLAPPEDWINDOW,
- 70 CW_USEDEFAULT,
- 71 CW_USEDEFAULT,
- 72 CW_USEDEFAULT,
- 73 CW_USEDEFAULT,
- 74 NULL,
- 75 NULL,
- 76 hInstance,
- 77 NULL );
- 78
- 79 if (!hWnd)
- 80 return (FALSE);
- 81
- 82 ShowWindow(hWnd, nCmdShow);
- 83 UpdateWindow(hWnd);
- 84
- 85 while (GetMessage(&msg, NULL, NULL,NULL))
- 86 {
- 87 TranslateMessage(&msg);
- 88 DispatchMessage(&msg);
- 89 }
- 90 return (msg.wParam);
- 91 }
- 92
- 93
- 94
- 95 /****************************************************************/
- 96 /* MainWndProc() */
- 97 /****************************************************************/
- 98
- 99 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 100 WORD wParam, LONG lParam)
- 101 {
- 102 HDC hDC;
- 103 HMENU hMenu;
- 104 static BOOL bLBDown;
- 105
- 106 switch (message)
- 107 {
- 108 case WM_CREATE :
- 109 hMenu = GetMenu(hWnd);
- 110 CheckMenuItem(hMenu, IDM_PENCIL,
- 111 MF_CHECKED);
- 112
- 113 hInst =
- 114 ((LPCREATESTRUCT) lParam)->hInstance;
- 115 return (0);
- 116
- 117 case WM_COMMAND :
- 118 hMenu = GetMenu(hWnd);
- 119 switch (wParam)
- 120 {
- 121 case IDM_PENCIL :
- 122 case IDM_LINE :
- 123
- 124 if (ToolID == wParam)
- 125 return (0);
- 126
- 127 CheckMenuItem(hMenu, ToolID,
- 128 MF_UNCHECKED);
- 129 ToolID = wParam;
- 130 CheckMenuItem(hMenu, ToolID,
- 131 MF_CHECKED);
- 132 break;
- 133
- 134 case IDM_CLEAR :
- 135 InvalidateRect(hWnd, NULL, TRUE);
- 136 break;
- 137
- 138 case IDM_PRINT :
- 139 lpPrnDlgProc =
- 140 MakeProcInstance(PrnDlgProc,
- 141 hInst);
- 142 lpAbortProc =
- 143 MakeProcInstance(AbortProc,
- 144 hInst);
- 145 PrintGraph(hWnd);
- 146
- 147 FreeProcInstance(lpPrnDlgProc);
- 148 FreeProcInstance(lpAbortProc);
- 149
- 150 break;
- 151
- 152 case IDM_QUIT :
- 153 DestroyWindow(hWnd);
- 154 break;
- 155 }
- 156 return (0);
- 157
- 158 case WM_SIZE :
- 159 CX = LOWORD(lParam);
- 160 CY = HIWORD(lParam);
- 161 return (0);
- 162
- 163 case WM_LBUTTONDOWN :
- 164 SetCapture(hWnd);
- 165 bLBDown = TRUE;
- 166
- 167 OrgPoint = MAKEPOINT(lParam);
- 168 CurPoint = PrePoint = OrgPoint;
- 169
- 170 return (0);
- 171
- 172 case WM_LBUTTONUP :
- 173 bLBDown = FALSE;
- 174 ReleaseCapture();
- 175
- 176 hDC = GetDC(hWnd);
- 177 DrawGraph(hDC, TRUE);
- 178 ReleaseDC(hWnd, hDC);
- 179
- 180 return (0);
- 181
- 182 case WM_MOUSEMOVE :
- 183 if (bLBDown)
- 184 {
- 185 PrePoint = CurPoint;
- 186 CurPoint = MAKEPOINT(lParam);
- 187
- 188 hDC = GetDC(hWnd);
- 189 DrawGraph(hDC, FALSE);
- 190 ReleaseDC(hWnd, hDC);
- 191 }
- 192 return (0);
- 193
- 194 case WM_DESTROY :
- 195 PostQuitMessage(0);
- 196 return (0);
- 197
- 198 default :
- 199 return(DefWindowProc(hWnd, message, wParam, lParam));
- 200 }
- 201 }
- 202
- 203
- 204
- 205 void DrawGraph(HDC hDC, BOOL bSure)
- 206 {
- 207 switch (ToolID)
- 208 {
- 209 case IDM_PENCIL :
- 210 DrawPencil(hDC);
- 211 break;
- 212
- 213 case IDM_LINE :
- 214 DrawLine(hDC, bSure);
- 215 break;
- 216 }
- 217 }
- 218
- 219
- 220
- 221 void DrawPencil(HDC hDC)
- 222 {
- 223 MoveTo(hDC, PrePoint.x, PrePoint.y);
- 224 LineTo(hDC, CurPoint.x, CurPoint.y);
- 225 }
- 226
- 227
- 228
- 229 void DrawLine(HDC hDC, BOOL bSure)
- 230 {
- 231 int nDrawMode;
- 232
- 233 if (! bSure)
- 234 {
- 235 nDrawMode = SetROP2(hDC, R2_NOT);
- 236
- 237 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 238 LineTo(hDC, PrePoint.x, PrePoint.y);
- 239
- 240 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 241 LineTo(hDC, CurPoint.x, CurPoint.y);
- 242
- 243 SetROP2(hDC, nDrawMode);
- 244 }
- 245 else
- 246 {
- 247 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 248 LineTo(hDC, CurPoint.x, CurPoint.y);
- 249 }
- 250 }
- 251
- 252
- 253
- 254 HDC CreateDC_Printer()
- 255 {
- 256 HDC hPrnDC;
- 257 char szProfile[70];
- 258 char *szDriver, *szDevice, *szOutput;
- 259
- 260 GetProfileString("windows", "device", "", szProfile, 70);
- 261
- 262 szDevice = (char *) strtok(szProfile, ",");
- 263 szDriver = (char *) strtok(NULL, ",");
- 264 szOutput = (char *) strtok(NULL, ",");
- 265
- 266 if (szDevice && szDriver && szOutput)
- 267 {
- 268 hPrnDC = CreateDC(szDriver, szDevice, szOutput, NULL);
- 269 return (hPrnDC);
- 270 }
- 271
- 272 return (NULL);
- 273 }
- 274
- 275
- 276
- 277 void PrintGraph(HWND hWnd)
- 278 {
- 279 HDC hDC, hMemDC;
- 280 HDC hPrnDC;
- 281 HBITMAP hBitmap;
- 282 BOOL bPrinted = TRUE;
- 283 char szName[] = "Demo8_2 -- Abort Proc";
- 284
- 285 hPrnDC = CreateDC_Printer();
- 286 if (hPrnDC == NULL)
- 287 {
- 288 MessageBox(hWnd, "Printer Error", NULL,
- 289 MB_OK | MB_ICONHAND);
- 290 return ;
- 291 }
- 292
- 293 hDC = GetDC(hWnd);
- 294 hMemDC = CreateCompatibleDC(hDC);
- 295 hBitmap = CreateCompatibleBitmap(hDC, CX, CY);
- 296
- 297 SelectObject(hMemDC, hBitmap);
- 298 BitBlt(hMemDC, 0, 0, CX, CY, hDC, 0, 0, SRCCOPY);
- 299 ReleaseDC(hWnd, hDC);
- 300
- 301 bCancel = FALSE;
- 302 hPrnDlg = CreateDialog(hInst, "PRNDLG", hWnd,
- 303 lpPrnDlgProc);
- 304 Escape(hPrnDC, SETABORTPROC, 0, (LPSTR) lpAbortProc,
- 305 NULL);
- 306
- 307 if (Escape(hPrnDC, STARTDOC,
- 308 strlen(szName), szName, NULL) > 0)
- 309 {
- 310 BitBlt(hPrnDC, 0, 0, CX, CY, hMemDC, 0, 0, SRCCOPY);
- 311
- 312 if (Escape(hPrnDC, NEWFRAME, 0, NULL, NULL) > 0)
- 313 Escape(hPrnDC, ENDDOC, 0, NULL, NULL);
- 314 else
- 315 bPrinted = FALSE;
- 316 }
- 317 else
- 318 bPrinted = FALSE;
- 319
- 320 if (! bPrinted)
- 321 MessageBox(hWnd, "Print Error", NULL,
- 322 MB_OK | MB_ICONHAND);
- 323
- 324 if (! bCancel)
- 325 {
- 326 EnableWindow(hWnd, TRUE);
- 327 DestroyWindow(hPrnDlg);
- 328 }
- 329
- 330 DeleteDC(hPrnDC);
- 331 DeleteDC(hMemDC);
- 332 DeleteObject(hBitmap);
- 333 }
- 334
- 335
- 336 BOOL FAR PASCAL AbortProc(HDC hPrnDC, short nCode)
- 337 {
- 338 MSG msg;
- 339
- 340 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- 341 {
- 342 if (bCancel) break;
- 343 if (! IsDialogMessage(hPrnDlg, &msg))
- 344 {
- 345 TranslateMessage(&msg);
- 346 DispatchMessage(&msg);
- 347 }
- 348 }
- 349
- 350 return (! bCancel);
- 351 }
- 352
- 353
- 354 BOOL FAR PASCAL PrnDlgProc(HWND hDlg, WORD msg,
- 355 WORD wParam, LONG lParam)
- 356 {
- 357 HWND hPWnd;
- 358 HMENU hSysMenu;
- 359
- 360 switch (msg)
- 361 {
- 362 case WM_INITDIALOG :
- 363 hPWnd = GetParent(hDlg);
- 364 EnableWindow(hPWnd, FALSE);
- 365
- 366 hSysMenu = GetSystemMenu(hDlg, 0);
- 367 EnableMenuItem(hSysMenu, SC_CLOSE,
- 368 MF_GRAYED);
- 369 return (TRUE);
- 370
- 371 case WM_COMMAND :
- 372 switch (wParam)
- 373 {
- 374 case DI_CANCEL :
- 375 bCancel = TRUE;
- 376 hPWnd = GetParent(hDlg);
- 377 EnableWindow(hPWnd, TRUE);
- 378 DestroyWindow(hDlg);
- 379 break;
- 380 }
- 381 return (TRUE);
- 382 }
- 383
- 384 return (FALSE);
- 385 }