DEMO1_4.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:14k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo1_4 --- The simple example */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "demo1_4.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[] = "Demo1_4";
- 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 example - V.3",
- 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 switch (Shape[i].ShapeID)
- 212 {
- 213 case IDM_LINE :
- 214 MoveTo(hDC, Shape[i].x1-OX, Shape[i].y1-OY);
- 215 LineTo(hDC, Shape[i].x2-OX, Shape[i].y2-OY);
- 216 break;
- 217
- 218 case IDM_RECT :
- 219 Rectangle(hDC, Shape[i].x1-OX, Shape[i].y1-OY,
- 220 Shape[i].x2-OX, Shape[i].y2-OY);
- 221 break;
- 222
- 223 case IDM_ELLIP :
- 224 Ellipse(hDC, Shape[i].x1-OX, Shape[i].y1-OY,
- 225 Shape[i].x2-OX, Shape[i].y2-OY);
- 226 break;
- 227 }
- 228 }
- 229 EndPaint(hWnd, &ps);
- 230 break;
- 231
- 232 case WM_SIZE :
- 233 GetClientRect(hWnd, &Rect);
- 234 CX = Rect.right - Rect.left;
- 235 CY = Rect.bottom - Rect.top;
- 236 SetScrollRange(hWnd, SB_HORZ, 0, MaxX-CX, TRUE);
- 237 SetScrollRange(hWnd, SB_VERT, 0, MaxY-CY, TRUE);
- 238 OX = min(OX, MaxX-CX);
- 239 OY = min(OY, MaxY-CY);
- 240 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
- 241 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
- 242 break;
- 243
- 244 case WM_KEYDOWN :
- 245 switch (wParam)
- 246 {
- 247 case VK_HOME :
- 248 SendMessage(hWnd, WM_HSCROLL,
- 249 SB_TOP, 0L);
- 250 break;
- 251
- 252 case VK_END :
- 253 SendMessage(hWnd, WM_HSCROLL,
- 254 SB_BOTTOM, 0L);
- 255 break;
- 256
- 257 case VK_PRIOR :
- 258 SendMessage(hWnd, WM_VSCROLL,
- 259 SB_TOP, 0L);
- 260 break;
- 261
- 262 case VK_NEXT :
- 263 SendMessage(hWnd, WM_VSCROLL,
- 264 SB_BOTTOM, 0L);
- 265 break;
- 266
- 267 case VK_LEFT :
- 268 SendMessage(hWnd, WM_HSCROLL,
- 269 SB_LINEUP, 0L);
- 270 break;
- 271
- 272 case VK_RIGHT :
- 273 SendMessage(hWnd, WM_HSCROLL,
- 274 SB_LINEDOWN, 0L);
- 275 break;
- 276
- 277 case VK_UP :
- 278 SendMessage(hWnd, WM_VSCROLL,
- 279 SB_LINEUP, 0L);
- 280 break;
- 281
- 282 case VK_DOWN :
- 283 SendMessage(hWnd, WM_VSCROLL,
- 284 SB_LINEDOWN, 0L);
- 285 break;
- 286 }
- 287 break;
- 288
- 289 case WM_HSCROLL :
- 290 switch (wParam)
- 291 {
- 292 case SB_TOP :
- 293 Temp = 0;
- 294 break;
- 295
- 296 case SB_BOTTOM :
- 297 Temp = MaxX - CX;
- 298 break;
- 299
- 300 case SB_LINEUP :
- 301 case SB_PAGEUP :
- 302 Temp = OX - CX/4;
- 303 break;
- 304
- 305 case SB_LINEDOWN :
- 306 case SB_PAGEDOWN :
- 307 Temp = OX + CX/4;
- 308 break;
- 309
- 310 case SB_THUMBPOSITION :
- 311 Temp = LOWORD(lParam);
- 312 break;
- 313
- 314 default :
- 315 return (NULL);
- 316 }
- 317 Temp = min(max(0, Temp), MaxX-CX);
- 318 if (Temp == OX) break;
- 319
- 320 OX = Temp;
- 321 SetScrollPos(hWnd, SB_HORZ, OX, TRUE);
- 322 InvalidateRect(hWnd, NULL, TRUE);
- 323 break;
- 324
- 325 case WM_VSCROLL :
- 326 switch (wParam)
- 327 {
- 328 case SB_TOP :
- 329 Temp = 0;
- 330 break;
- 331
- 332 case SB_BOTTOM :
- 333 Temp = MaxY - CY;
- 334 break;
- 335
- 336 case SB_LINEUP :
- 337 case SB_PAGEUP :
- 338 Temp = OY - CY/4;
- 339 break;
- 340
- 341 case SB_LINEDOWN :
- 342 case SB_PAGEDOWN :
- 343 Temp = OY + CY/4;
- 344 break;
- 345
- 346 case SB_THUMBPOSITION :
- 347 Temp = LOWORD(lParam);
- 348 break;
- 349
- 350 default :
- 351 return (NULL);
- 352 }
- 353 Temp = min(max(0, Temp), MaxY-CY);
- 354 if (Temp == OY) break;
- 355
- 356 OY = Temp;
- 357 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
- 358 InvalidateRect(hWnd, NULL, TRUE);
- 359 break;
- 360
- 361 case WM_DESTROY :
- 362 PostQuitMessage(0);
- 363 break ;
- 364
- 365 default :
- 366 return (DefWindowProc(hWnd, message, wParam, lParam));
- 367 }
- 368 return (NULL);
- 369 }