DEMO3_2.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:13k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo3_2 --- The simple CAD */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "demo3_2.h"
- 7
- 8 #define MAXPOINTS 30
- 9 #define MaxX 1000
- 10 #define MaxY 1000
- 11
- 12 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 13 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 14
- 15
- 16
- 17 /****************************************************************/
- 18 /* WinMain() */
- 19 /****************************************************************/
- 20
- 21 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 22 LPSTR lpszCmdLine, int nCmdShow)
- 23 {
- 24 WNDCLASS wclass;
- 25 MSG msg;
- 26 HWND hWnd;
- 27 char szName[] = "Demo3_2";
- 28
- 29 if (!hPrevInstance)
- 30 {
- 31 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 32 wclass.lpfnWndProc = MainWndProc;
- 33 wclass.cbClsExtra = 0;
- 34 wclass.cbWndExtra = 0;
- 35 wclass.hInstance = hInstance;
- 36 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 37 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 38 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 39 wclass.lpszMenuName = szName;
- 40 wclass.lpszClassName = szName;
- 41
- 42 if (!RegisterClass (&wclass))
- 43 return (FALSE);
- 44 }
- 45
- 46 hWnd = CreateWindow(
- 47 szName,
- 48 "The simple CAD",
- 49 WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
- 50 CW_USEDEFAULT,
- 51 CW_USEDEFAULT,
- 52 CW_USEDEFAULT,
- 53 CW_USEDEFAULT,
- 54 NULL,
- 55 NULL,
- 56 hInstance,
- 57 NULL );
- 58
- 59 if (!hWnd)
- 60 return (FALSE);
- 61
- 62 ShowWindow(hWnd, nCmdShow);
- 63 UpdateWindow(hWnd);
- 64
- 65 while (GetMessage(&msg, NULL, NULL,NULL))
- 66 {
- 67 TranslateMessage(&msg);
- 68 DispatchMessage(&msg);
- 69 }
- 70 return (msg.wParam);
- 71 }
- 72
- 73
- 74
- 75 /****************************************************************/
- 76 /* MainWndProc() */
- 77 /****************************************************************/
- 78
- 79 typedef struct tagSHAPE {
- 80 int x1, y1;
- 81 int x2, y2;
- 82 int ShapeID;
- 83 } SHAPE;
- 84
- 85 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 86 WORD wParam, LONG lParam)
- 87 {
- 88 PAINTSTRUCT ps;
- 89 HDC hDC;
- 90 HMENU hMenu;
- 91 static int ShapeID = IDM_LINE;
- 92 RECT Rect, dstRect;
- 93 int x1, x2, y1, y2;
- 94 static int CX, CY;
- 95 int i, Temp;
- 96 static int Num = 0;
- 97 static SHAPE Shape[MAXPOINTS];
- 98 static int OX, OY;
- 99
- 100 switch (message)
- 101 {
- 102 case WM_CREATE :
- 103 srand(LOWORD(GetCurrentTime()));
- 104 break;
- 105
- 106 case WM_COMMAND :
- 107 switch(wParam)
- 108 {
- 109 case IDM_CLEAR :
- 110 Num = 0;
- 111 InvalidateRect(hWnd, NULL, TRUE);
- 112 UpdateWindow(hWnd);
- 113 break;
- 114
- 115 case IDM_EXIT :
- 116 DestroyWindow (hWnd);
- 117 break;
- 118
- 119 case IDM_LINE :
- 120 if (ShapeID != IDM_LINE)
- 121 {
- 122 hMenu = GetMenu(hWnd);
- 123 CheckMenuItem(hMenu, ShapeID,
- 124 MF_UNCHECKED);
- 125 ShapeID = IDM_LINE;
- 126 CheckMenuItem(hMenu, ShapeID,
- 127 MF_CHECKED);
- 128 }
- 129 break;
- 130
- 131 case IDM_RECT :
- 132 if (ShapeID != IDM_RECT)
- 133 {
- 134 hMenu = GetMenu(hWnd);
- 135 CheckMenuItem(hMenu, ShapeID,
- 136 MF_UNCHECKED);
- 137 ShapeID = IDM_RECT;
- 138 CheckMenuItem(hMenu, ShapeID,
- 139 MF_CHECKED);
- 140 }
- 141 break;
- 142
- 143 case IDM_ELLIP :
- 144 if (ShapeID != IDM_ELLIP)
- 145 {
- 146 hMenu = GetMenu(hWnd);
- 147 CheckMenuItem(hMenu, ShapeID,
- 148 MF_UNCHECKED);
- 149 ShapeID = IDM_ELLIP;
- 150 CheckMenuItem(hMenu, ShapeID,
- 151 MF_CHECKED);
- 152 }
- 153 break;
- 154
- 155 case IDM_DRAW :
- 156 if (Num == MAXPOINTS)
- 157 {
- 158 MessageBox(hWnd,
- 159 "Memory not enough", "Warning",
- 160 MB_OK | MB_ICONEXCLAMATION);
- 161 break;
- 162 }
- 163
- 164 hDC = GetDC(hWnd);
- 165
- 166 x1 = rand() % CX;
- 167 y1 = rand() % CY;
- 168 x2 = rand() % CX;
- 169 y2 = rand() % CY;
- 170
- 171 switch (ShapeID)
- 172 {
- 173 case IDM_LINE :
- 174 MoveTo(hDC, x1, y1);
- 175 LineTo(hDC, x2, y2);
- 176 break;
- 177
- 178 case IDM_RECT :
- 179 Rectangle(hDC, x1, y1, x2, y2);
- 180 break;
- 181
- 182 case IDM_ELLIP :
- 183 Ellipse(hDC, x1, y1, x2, y2);
- 184 break;
- 185 }
- 186
- 187 ReleaseDC(hWnd, hDC);
- 188
- 189 Shape[Num].x1 = OX + x1;
- 190 Shape[Num].y1 = OY + y1;
- 191 Shape[Num].x2 = OX + x2;
- 192 Shape[Num].y2 = OY + y2;
- 193 Shape[Num].ShapeID = ShapeID;
- 194 Num ++;
- 195 break;
- 196 }
- 197 break;
- 198
- 199 case WM_PAINT :
- 200 hDC = BeginPaint(hWnd, &ps);
- 201 for (i=0; i<Num; i++)
- 202 {
- 203 Rect.left = min(Shape[i].x1-OX, Shape[i].x2-OX);
- 204 Rect.right = max(Shape[i].x1-OX, Shape[i].x2-OX);
- 205 Rect.top = min(Shape[i].y1-OY, Shape[i].y2-OY);
- 206 Rect.bottom= max(Shape[i].y1-OY, Shape[i].y2-OY);
- 207 IntersectRect(&dstRect, &Rect, &(ps.rcPaint));
- 208 if (IsRectEmpty(&dstRect))
- 209 continue;
- 210
- 211 SetWindowOrg(hDC, OX, OY);
- 212 switch (Shape[i].ShapeID)
- 213 {
- 214 case IDM_LINE :
- 215 MoveTo(hDC, Shape[i].x1, Shape[i].y1);
- 216 LineTo(hDC, Shape[i].x2, Shape[i].y2);
- 217 break;
- 218
- 219 case IDM_RECT :
- 220 Rectangle(hDC, Shape[i].x1, Shape[i].y1,
- 221 Shape[i].x2, Shape[i].y2);
- 222 break;
- 223
- 224 case IDM_ELLIP :
- 225 Ellipse(hDC, Shape[i].x1, Shape[i].y1,
- 226 Shape[i].x2, Shape[i].y2);
- 227 break;
- 228 }
- 229 }
- 230 EndPaint(hWnd, &ps);
- 231 break;
- 232
- 233 case WM_SIZE :
- 234
- 235 CX = LOWORD(lParam);
- 236 CY = HIWORD(lParam);
- 237 SetScrollRange(hWnd, SB_HORZ, 0, MaxX-CX, TRUE);
- 238 SetScrollRange(hWnd, SB_VERT, 0, MaxY-CY, TRUE);
- 239 OX = min(OX, MaxX-CX);
- 240 OY = min(OY, MaxY-CY);
- 241 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
- 242 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
- 243 break;
- 244
- 245 case WM_KEYDOWN :
- 246 switch (wParam)
- 247 {
- 248 case VK_HOME :
- 249 SendMessage(hWnd, WM_HSCROLL,
- 250 SB_TOP, 0L);
- 251 break;
- 252
- 253 case VK_END :
- 254 SendMessage(hWnd, WM_HSCROLL,
- 255 SB_BOTTOM, 0L);
- 256 break;
- 257
- 258 case VK_PRIOR :
- 259 SendMessage(hWnd, WM_VSCROLL,
- 260 SB_TOP, 0L);
- 261 break;
- 262
- 263 case VK_NEXT :
- 264 SendMessage(hWnd, WM_VSCROLL,
- 265 SB_BOTTOM, 0L);
- 266 break;
- 267
- 268 case VK_LEFT :
- 269 SendMessage(hWnd, WM_HSCROLL,
- 270 SB_LINEUP, 0L);
- 271 break;
- 272
- 273 case VK_RIGHT :
- 274 SendMessage(hWnd, WM_HSCROLL,
- 275 SB_LINEDOWN, 0L);
- 276 break;
- 277
- 278 case VK_UP :
- 279 SendMessage(hWnd, WM_VSCROLL,
- 280 SB_LINEUP, 0L);
- 281 break;
- 282
- 283 case VK_DOWN :
- 284 SendMessage(hWnd, WM_VSCROLL,
- 285 SB_LINEDOWN, 0L);
- 286 break;
- 287 }
- 288 break;
- 289
- 290 case WM_HSCROLL :
- 291 switch (wParam)
- 292 {
- 293 case SB_TOP :
- 294 Temp = 0;
- 295 break;
- 296
- 297 case SB_BOTTOM :
- 298 Temp = MaxX - CX;
- 299 break;
- 300
- 301 case SB_LINEUP :
- 302 case SB_PAGEUP :
- 303 Temp = OX - CX/4;
- 304 break;
- 305
- 306 case SB_LINEDOWN :
- 307 case SB_PAGEDOWN :
- 308 Temp = OX + CX/4;
- 309 break;
- 310
- 311 case SB_THUMBPOSITION :
- 312 Temp = LOWORD(lParam);
- 313 break;
- 314
- 315 default :
- 316 return (NULL);
- 317 }
- 318 Temp = min(max(0, Temp), MaxX-CX);
- 319 if (Temp == OX) break;
- 320
- 321 OX = Temp;
- 322 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
- 323 InvalidateRect(hWnd, NULL, TRUE);
- 324 break;
- 325
- 326 case WM_VSCROLL :
- 327 switch (wParam)
- 328 {
- 329 case SB_TOP :
- 330 Temp = 0;
- 331 break;
- 332
- 333 case SB_BOTTOM :
- 334 Temp = MaxY - CY;
- 335 break;
- 336
- 337 case SB_LINEUP :
- 338 case SB_PAGEUP :
- 339 Temp = OY - CY/4;
- 340 break;
- 341
- 342 case SB_LINEDOWN :
- 343 case SB_PAGEDOWN :
- 344 Temp = OY + CY/4;
- 345 break;
- 346
- 347 case SB_THUMBPOSITION :
- 348 Temp = LOWORD(lParam);
- 349 break;
- 350
- 351 default :
- 352 return (NULL);
- 353 }
- 354 Temp = min(max(0, Temp), MaxY-CY);
- 355 if (Temp == OY) break;
- 356
- 357 OY = Temp;
- 358 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
- 359 InvalidateRect(hWnd, NULL, TRUE);
- 360 break;
- 361
- 362 case WM_DESTROY :
- 363 PostQuitMessage(0);
- 364 break ;
- 365
- 366 default :
- 367 return (DefWindowProc(hWnd, message, wParam, lParam));
- 368 }
- 369 return (NULL);
- 370 }