DEMO5_3.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:8k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo5_3 --- Moving Region */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "Demo5_3.h"
- 7
- 8 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 9 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 10
- 11 void MakeScreenBitmap(HDC);
- 12 void DrawRegion(HDC, int, int, int, int, int);
- 13 void GetViewSize(POINT *);
- 14 void Run(HWND);
- 15
- 16
- 17 HDC hMemDC;
- 18 HBITMAP hBitmap;
- 19 HRGN hPreRgn;
- 20
- 21 int MemX, MemY;
- 22 int RegionTypeID = IDM_CIRCLE;
- 23 int ViewSizeID = IDM_75;
- 24
- 25 /****************************************************************/
- 26 /* WinMain() */
- 27 /****************************************************************/
- 28
- 29 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 30 LPSTR lpszCmdLine, int nCmdShow)
- 31 {
- 32 WNDCLASS wclass;
- 33 MSG msg;
- 34 HWND hWnd;
- 35 char szName[] = "Demo5_3";
- 36
- 37 if (!hPrevInstance)
- 38 {
- 39 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 40 wclass.lpfnWndProc = MainWndProc;
- 41 wclass.cbClsExtra = 0;
- 42 wclass.cbWndExtra = 0;
- 43 wclass.hInstance = hInstance;
- 44 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 45 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 46 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 47 wclass.lpszMenuName = szName;
- 48 wclass.lpszClassName = szName;
- 49
- 50 if (!RegisterClass (&wclass))
- 51 return (FALSE);
- 52 }
- 53
- 54 hWnd = CreateWindow(
- 55 szName,
- 56 "Moving Region" ,
- 57 WS_OVERLAPPEDWINDOW,
- 58 CW_USEDEFAULT,
- 59 CW_USEDEFAULT,
- 60 CW_USEDEFAULT,
- 61 CW_USEDEFAULT,
- 62 NULL,
- 63 NULL,
- 64 hInstance,
- 65 NULL );
- 66
- 67 if (!hWnd)
- 68 return (FALSE);
- 69
- 70 ShowWindow(hWnd, nCmdShow);
- 71 UpdateWindow(hWnd);
- 72
- 73 while (GetMessage(&msg, NULL, NULL,NULL))
- 74 {
- 75 TranslateMessage(&msg);
- 76 DispatchMessage(&msg);
- 77 }
- 78 return (msg.wParam);
- 79 }
- 80
- 81
- 82
- 83 /****************************************************************/
- 84 /* MainWndProc() */
- 85 /****************************************************************/
- 86
- 87 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 88 WORD wParam, LONG lParam)
- 89 {
- 90 HMENU hMenu;
- 91
- 92 switch (message)
- 93 {
- 94 case WM_CREATE :
- 95 hMenu = GetMenu(hWnd);
- 96 CheckMenuItem(hMenu, RegionTypeID,
- 97 MF_CHECKED);
- 98 CheckMenuItem(hMenu, ViewSizeID,
- 99 MF_CHECKED);
- 100 return (0);
- 101
- 102 case WM_COMMAND :
- 103 switch (wParam)
- 104 {
- 105 case IDM_CIRCLE :
- 106 case IDM_SQUARE :
- 107
- 108 hMenu = GetMenu(hWnd);
- 109 if (RegionTypeID == wParam)
- 110 break;
- 111
- 112 CheckMenuItem(hMenu, RegionTypeID,
- 113 MF_UNCHECKED);
- 114 RegionTypeID = wParam;
- 115 CheckMenuItem(hMenu, RegionTypeID,
- 116 MF_CHECKED);
- 117 break;
- 118
- 119 case IDM_50 :
- 120 case IDM_75 :
- 121 case IDM_100 :
- 122 case IDM_150 :
- 123
- 124 hMenu = GetMenu(hWnd);
- 125 if (ViewSizeID == wParam)
- 126 break;
- 127
- 128 CheckMenuItem(hMenu, ViewSizeID,
- 129 MF_UNCHECKED);
- 130 ViewSizeID = wParam;
- 131 CheckMenuItem(hMenu, ViewSizeID,
- 132 MF_CHECKED);
- 133 break;
- 134
- 135 case IDM_RUN :
- 136 Run(hWnd);
- 137 break;
- 138
- 139 case IDM_EXIT :
- 140 DestroyWindow(hWnd);
- 141 break;
- 142 }
- 143 return (0);
- 144
- 145 case WM_DESTROY :
- 146 PostQuitMessage(0);
- 147 return (0);
- 148 }
- 149
- 150 return(DefWindowProc(hWnd, message, wParam, lParam));
- 151 }
- 152
- 153
- 154 void MakeScreenBitmap(HDC hDC)
- 155 {
- 156 hMemDC = CreateCompatibleDC(hDC);
- 157
- 158 MemX = GetSystemMetrics(SM_CXSCREEN);
- 159 MemY = GetSystemMetrics(SM_CYSCREEN);
- 160
- 161 hBitmap = CreateCompatibleBitmap(hDC, MemX, MemY);
- 162 SelectObject(hMemDC, hBitmap);
- 163
- 164 BitBlt(hMemDC, 0, 0, MemX, MemY, hDC, 0, 0, SRCCOPY);
- 165 PatBlt(hDC, 0, 0, MemX, MemY, BLACKNESS);
- 166 }
- 167
- 168
- 169 void DrawRegion(HDC hDC, int MX, int MY,
- 170 int Width, int Height, int bFirst)
- 171 {
- 172 HRGN hRgn, hDstRgn;
- 173 static int PreMX, PreMY;
- 174
- 175 switch (RegionTypeID)
- 176 {
- 177 case IDM_CIRCLE :
- 178 hRgn = CreateEllipticRgn(
- 179 MX-Width/2, MY-Height/2,
- 180 MX+Width/2, MY+Height/2);
- 181 break;
- 182 case IDM_SQUARE :
- 183 hRgn = CreateRectRgn(
- 184 MX-Width/2, MY-Height/2,
- 185 MX+Width/2, MY+Height/2);
- 186 break;
- 187 }
- 188
- 189 SelectObject(hDC, hRgn);
- 190 BitBlt(hDC, MX-Width/2, MY-Height/2,
- 191 Width, Height,
- 192 hMemDC, MX-Width/2, MY-Height/2,
- 193 SRCCOPY);
- 194
- 195 if (! bFirst)
- 196 {
- 197 hDstRgn = CreateRectRgn(0, 0, 1, 1);
- 198 CombineRgn(hDstRgn, hPreRgn, hRgn, RGN_DIFF);
- 199
- 200 SelectObject(hDC, hDstRgn);
- 201 PatBlt(hDC, PreMX-Width/2, PreMY-Height/2,
- 202 Width, Height, BLACKNESS);
- 203 SelectObject(hDC, hRgn);
- 204
- 205 DeleteObject(hDstRgn);
- 206 DeleteObject(hPreRgn);
- 207 }
- 208
- 209 hPreRgn = hRgn;
- 210
- 211 PreMX = MX;
- 212 PreMY = MY;
- 213
- 214 bFirst = FALSE;
- 215 }
- 216
- 217
- 218 void GetViewSize(POINT *lpSize)
- 219 {
- 220 switch (ViewSizeID)
- 221 {
- 222 case IDM_50 :
- 223 lpSize->x = 50;
- 224 break;
- 225
- 226 case IDM_75 :
- 227 lpSize->x = 75;
- 228 break;
- 229
- 230 case IDM_100 :
- 231 lpSize->x = 100;
- 232 break;
- 233
- 234 case IDM_150 :
- 235 lpSize->x = 150;
- 236 break;
- 237 }
- 238
- 239 lpSize->y = -lpSize->x;
- 240 }
- 241
- 242
- 243 void Run(HWND hWnd)
- 244 {
- 245 int KeyState;
- 246 HDC hDC;
- 247 HRGN hScrRgn;
- 248 POINT Pt, ptSize;
- 249
- 250 SetCapture(hWnd);
- 251 ShowCursor(FALSE);
- 252
- 253 hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
- 254 MakeScreenBitmap(hDC);
- 255
- 256 GetViewSize(&ptSize);
- 257 SetMapMode(hDC, MM_LOENGLISH);
- 258 LPtoDP(hDC, &ptSize, 1);
- 259 SetMapMode(hDC, MM_TEXT);
- 260
- 261 GetCursorPos(&Pt);
- 262 DrawRegion(hDC, Pt.x, Pt.y, ptSize.x, ptSize.y, 1);
- 263
- 264 for (;;)
- 265 {
- 266 KeyState = GetAsyncKeyState(VK_SPACE);
- 267 if (KeyState & 0x8000)
- 268 break;
- 269
- 270 GetCursorPos(&Pt);
- 271 DrawRegion(hDC, Pt.x, Pt.y, ptSize.x, ptSize.y, 0);
- 272 }
- 273
- 274 hScrRgn = CreateRectRgn(0, 0, MemX, MemY);
- 275 SelectObject(hDC, hScrRgn);
- 276 BitBlt(hDC, 0, 0, MemX, MemY, hMemDC, 0, 0, SRCCOPY);
- 277
- 278 DeleteDC(hMemDC);
- 279 DeleteObject(hBitmap);
- 280 DeleteObject(hPreRgn);
- 281 DeleteObject(hScrRgn);
- 282
- 283 DeleteDC(hDC);
- 284
- 285 ReleaseCapture();
- 286 ShowCursor(TRUE);
- 287 }