DEMO4_2.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:11k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo4_2 --- Pencil & Line */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "demo4_2.h"
- 7
- 8
- 9 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 10 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 11
- 12
- 13 void DrawGraph(HDC, BOOL);
- 14 void DrawPencil(HDC);
- 15 void DrawLine(HDC, BOOL);
- 16
- 17
- 18 int ToolID = IDM_PENCIL;
- 19 int ColorID = IDM_RED;
- 20 int StyleID = IDM_SOLID;
- 21 int WidthID = IDM_W1;
- 22 POINT OrgPoint;
- 23 POINT PrePoint;
- 24 POINT CurPoint;
- 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[] = "Demo4_2";
- 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 "Pencil & Line V.2" ,
- 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 int i;
- 95
- 96
- 97 switch (message)
- 98 {
- 99 case WM_CREATE :
- 100 hMenu = GetMenu(hWnd);
- 101 CheckMenuItem(hMenu, IDM_PENCIL, MF_CHECKED);
- 102 CheckMenuItem(hMenu, IDM_RED, MF_CHECKED);
- 103 CheckMenuItem(hMenu, IDM_SOLID, MF_CHECKED);
- 104 CheckMenuItem(hMenu, IDM_W1, MF_CHECKED);
- 105 return (0);
- 106
- 107 case WM_COMMAND :
- 108 hMenu = GetMenu(hWnd);
- 109 switch (wParam)
- 110 {
- 111 case IDM_PENCIL :
- 112 case IDM_LINE :
- 113
- 114 if (ToolID == wParam)
- 115 return (0);
- 116
- 117 CheckMenuItem(hMenu, ToolID,
- 118 MF_UNCHECKED);
- 119 ToolID = wParam;
- 120 CheckMenuItem(hMenu, ToolID,
- 121 MF_CHECKED);
- 122 break;
- 123
- 124 case IDM_RED :
- 125 case IDM_GREEN :
- 126 case IDM_BLUE :
- 127 case IDM_YELLOW :
- 128 case IDM_CYAN :
- 129 case IDM_MAGENTA :
- 130 case IDM_WHITE :
- 131 case IDM_GRAY :
- 132 case IDM_BLACK :
- 133
- 134 if (ColorID == wParam)
- 135 return (0);
- 136
- 137 CheckMenuItem(hMenu, ColorID,
- 138 MF_UNCHECKED);
- 139 ColorID = wParam;
- 140 CheckMenuItem(hMenu, ColorID,
- 141 MF_CHECKED);
- 142 break;
- 143
- 144 case IDM_SOLID :
- 145 case IDM_DASH :
- 146 case IDM_DOT :
- 147 case IDM_DASHDOT :
- 148 case IDM_DASHDOTDOT :
- 149
- 150 if (StyleID == wParam)
- 151 return (0);
- 152
- 153 CheckMenuItem(hMenu, StyleID,
- 154 MF_UNCHECKED);
- 155 StyleID = wParam;
- 156 CheckMenuItem(hMenu, StyleID,
- 157 MF_CHECKED);
- 158 break;
- 159
- 160 case IDM_W1 :
- 161 case IDM_W3 :
- 162 case IDM_W5 :
- 163 case IDM_W7 :
- 164 case IDM_W9 :
- 165
- 166 if (WidthID == wParam)
- 167 return (0);
- 168
- 169 if (wParam != IDM_W1)
- 170 {
- 171 SendMessage(hWnd, WM_COMMAND,
- 172 IDM_SOLID, 0);
- 173 for (i=1; i<5; i++)
- 174 EnableMenuItem(hMenu,
- 175 IDM_SOLID+i, MF_GRAYED);
- 176 }
- 177 else
- 178 {
- 179 for (i=1; i<5; i++)
- 180 EnableMenuItem(hMenu,
- 181 IDM_SOLID+i, MF_ENABLED);
- 182 }
- 183
- 184 CheckMenuItem(hMenu, WidthID,
- 185 MF_UNCHECKED);
- 186 WidthID = wParam;
- 187 CheckMenuItem(hMenu, WidthID,
- 188 MF_CHECKED);
- 189 break;
- 190
- 191 case IDM_CLEAR :
- 192 InvalidateRect(hWnd, NULL, TRUE);
- 193 break;
- 194
- 195 case IDM_QUIT :
- 196 DestroyWindow(hWnd);
- 197 break;
- 198 }
- 199 return (0);
- 200
- 201 case WM_LBUTTONDOWN :
- 202 SetCapture(hWnd);
- 203 bLBDown = TRUE;
- 204
- 205 OrgPoint = MAKEPOINT(lParam);
- 206 CurPoint = PrePoint = OrgPoint;
- 207
- 208 return (0);
- 209
- 210 case WM_LBUTTONUP :
- 211 bLBDown = FALSE;
- 212 ReleaseCapture();
- 213
- 214 hDC = GetDC(hWnd);
- 215 DrawGraph(hDC, TRUE);
- 216 ReleaseDC(hWnd, hDC);
- 217
- 218 return (0);
- 219
- 220 case WM_MOUSEMOVE :
- 221 if (bLBDown)
- 222 {
- 223 PrePoint = CurPoint;
- 224 CurPoint = MAKEPOINT(lParam);
- 225
- 226 hDC = GetDC(hWnd);
- 227 DrawGraph(hDC, FALSE);
- 228 ReleaseDC(hWnd, hDC);
- 229 }
- 230 return (0);
- 231
- 232 case WM_DESTROY :
- 233 PostQuitMessage(0);
- 234 return (0);
- 235
- 236 default :
- 237 return(DefWindowProc(hWnd, message, wParam, lParam));
- 238 }
- 239 }
- 240
- 241
- 242
- 243 COLORREF MakeRgbColor()
- 244 {
- 245 COLORREF crColor;
- 246
- 247 switch (ColorID)
- 248 {
- 249 case IDM_RED :
- 250 crColor = RGB(255, 0, 0);
- 251 break;
- 252
- 253 case IDM_GREEN :
- 254 crColor = RGB(0, 255, 0);
- 255 break;
- 256
- 257 case IDM_BLUE :
- 258 crColor = RGB(0, 0, 255);
- 259 break;
- 260
- 261 case IDM_YELLOW :
- 262 crColor = RGB(255, 255, 0);
- 263 break;
- 264
- 265 case IDM_CYAN :
- 266 crColor = RGB(0, 255, 255);
- 267 break;
- 268
- 269 case IDM_MAGENTA :
- 270 crColor = RGB(255, 0, 255);
- 271 break;
- 272
- 273 case IDM_WHITE :
- 274 crColor = RGB(255, 255, 255);
- 275 break;
- 276
- 277 case IDM_GRAY :
- 278 crColor = RGB(150, 150, 150);
- 279 break;
- 280
- 281 case IDM_BLACK :
- 282 crColor = RGB(0, 0, 0);
- 283 break;
- 284 }
- 285
- 286 return (crColor);
- 287 }
- 288
- 289
- 290
- 291 HPEN CreateColorPen()
- 292 {
- 293 HPEN hPen;
- 294 int nStyle;
- 295 int nWidth;
- 296 COLORREF crColor;
- 297
- 298 nStyle = StyleID - IDM_SOLID;
- 299 nWidth = 2*(WidthID - IDM_W1) + 1;
- 300 crColor = MakeRgbColor();
- 301
- 302 hPen = CreatePen(nStyle, nWidth, crColor);
- 303 return (hPen);
- 304 }
- 305
- 306
- 307
- 308 void DrawGraph(HDC hDC, BOOL bSure)
- 309 {
- 310 HPEN hPen, hPrePen;
- 311
- 312 if (ToolID==IDM_PENCIL || bSure)
- 313 {
- 314 hPen = CreateColorPen();
- 315 hPrePen = SelectObject(hDC, hPen);
- 316 }
- 317
- 318 switch (ToolID)
- 319 {
- 320 case IDM_PENCIL :
- 321 DrawPencil(hDC);
- 322 break;
- 323
- 324 case IDM_LINE :
- 325 DrawLine(hDC, bSure);
- 326 break;
- 327 }
- 328
- 329 if (ToolID==IDM_PENCIL || bSure)
- 330 {
- 331 SelectObject(hDC, hPrePen);
- 332 DeleteObject(hPen);
- 333 }
- 334 }
- 335
- 336
- 337
- 338 void DrawPencil(HDC hDC)
- 339 {
- 340 MoveTo(hDC, PrePoint.x, PrePoint.y);
- 341 LineTo(hDC, CurPoint.x, CurPoint.y);
- 342 }
- 343
- 344
- 345
- 346 void DrawLine(HDC hDC, BOOL bSure)
- 347 {
- 348 int nDrawMode;
- 349
- 350 if (! bSure)
- 351 {
- 352 nDrawMode = SetROP2(hDC, R2_NOT);
- 353
- 354 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 355 LineTo(hDC, PrePoint.x, PrePoint.y);
- 356
- 357 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 358 LineTo(hDC, CurPoint.x, CurPoint.y);
- 359
- 360 SetROP2(hDC, nDrawMode);
- 361 }
- 362 else
- 363 {
- 364 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
- 365 LineTo(hDC, CurPoint.x, CurPoint.y);
- 366 }
- 367 }