DEMO5_4.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:10k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo5_4 --- Stretch Graph */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "Demo5_4.h"
- 7
- 8 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 9 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 10
- 11 void DrawRect(POINT, POINT);
- 12 void CatchGraph(POINT, POINT);
- 13
- 14
- 15 HDC hMemDC;
- 16 HBITMAP hBitmap;
- 17
- 18 int RegionID = IDM_REG;
- 19 int MethodID = IDM_FIT;
- 20 int MemX, MemY;
- 21
- 22 /****************************************************************/
- 23 /* WinMain() */
- 24 /****************************************************************/
- 25
- 26 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 27 LPSTR lpszCmdLine, int nCmdShow)
- 28 {
- 29 WNDCLASS wclass;
- 30 MSG msg;
- 31 HWND hWnd;
- 32 char szName[] = "Demo5_4";
- 33
- 34 if (!hPrevInstance)
- 35 {
- 36 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 37 wclass.lpfnWndProc = MainWndProc;
- 38 wclass.cbClsExtra = 0;
- 39 wclass.cbWndExtra = 0;
- 40 wclass.hInstance = hInstance;
- 41 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 42 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 43 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 44 wclass.lpszMenuName = szName;
- 45 wclass.lpszClassName = szName;
- 46
- 47 if (!RegisterClass (&wclass))
- 48 return (FALSE);
- 49 }
- 50
- 51 hWnd = CreateWindow(
- 52 szName,
- 53 "Stretch Graph" ,
- 54 WS_OVERLAPPEDWINDOW,
- 55 CW_USEDEFAULT,
- 56 CW_USEDEFAULT,
- 57 CW_USEDEFAULT,
- 58 CW_USEDEFAULT,
- 59 NULL,
- 60 NULL,
- 61 hInstance,
- 62 NULL );
- 63
- 64 if (!hWnd)
- 65 return (FALSE);
- 66
- 67 ShowWindow(hWnd, nCmdShow);
- 68 UpdateWindow(hWnd);
- 69
- 70 while (GetMessage(&msg, NULL, NULL,NULL))
- 71 {
- 72 TranslateMessage(&msg);
- 73 DispatchMessage(&msg);
- 74 }
- 75 return (msg.wParam);
- 76 }
- 77
- 78
- 79
- 80 /****************************************************************/
- 81 /* MainWndProc() */
- 82 /****************************************************************/
- 83
- 84 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 85 WORD wParam, LONG lParam)
- 86 {
- 87 HMENU hMenu;
- 88 HDC hDC;
- 89 PAINTSTRUCT ps;
- 90 RECT Rect;
- 91 static POINT ptSize;
- 92 static BOOL bRun = FALSE;
- 93 static BOOL bLBDown = FALSE;
- 94 static POINT ptPoint1, ptPoint2, ptPrePoint;
- 95
- 96 switch (message)
- 97 {
- 98 case WM_CREATE :
- 99 hMenu = GetMenu(hWnd);
- 100 CheckMenuItem(hMenu, RegionID,
- 101 MF_CHECKED);
- 102 CheckMenuItem(hMenu, MethodID,
- 103 MF_CHECKED);
- 104
- 105 hDC = GetDC(hWnd);
- 106
- 107 MemX = GetSystemMetrics(SM_CXSCREEN);
- 108 MemY = GetSystemMetrics(SM_CYSCREEN);
- 109
- 110 hMemDC = CreateCompatibleDC(hDC);
- 111 hBitmap = CreateCompatibleBitmap(hDC,
- 112 MemX, MemY);
- 113 SelectObject(hMemDC, hBitmap);
- 114
- 115 PatBlt(hMemDC, 0, 0, MemX, MemY, WHITENESS);
- 116
- 117 ReleaseDC(hWnd, hDC);
- 118 return (0);
- 119
- 120 case WM_COMMAND :
- 121 switch (wParam)
- 122 {
- 123 case IDM_REG :
- 124 case IDM_WIN :
- 125 case IDM_SCR :
- 126
- 127 hMenu = GetMenu(hWnd);
- 128 if (RegionID == wParam)
- 129 break;
- 130
- 131 CheckMenuItem(hMenu, RegionID,
- 132 MF_UNCHECKED);
- 133 RegionID = wParam;
- 134 CheckMenuItem(hMenu, RegionID,
- 135 MF_CHECKED);
- 136 break;
- 137
- 138 case IDM_FIT :
- 139 case IDM_SAME :
- 140
- 141 hMenu = GetMenu(hWnd);
- 142 if (MethodID == wParam)
- 143 break;
- 144
- 145 CheckMenuItem(hMenu, MethodID,
- 146 MF_UNCHECKED);
- 147 MethodID = wParam;
- 148 CheckMenuItem(hMenu, MethodID,
- 149 MF_CHECKED);
- 150
- 151 InvalidateRect(hWnd, NULL, TRUE);
- 152 break;
- 153
- 154 case IDM_RUN :
- 155 switch (RegionID)
- 156 {
- 157 case IDM_REG :
- 158 SetCapture(hWnd);
- 159 SetCursor(LoadCursor(
- 160 NULL, IDC_CROSS));
- 161 bRun = TRUE;
- 162 break;
- 163
- 164 case IDM_SCR :
- 165
- 166 ptPoint1.x = 0;
- 167 ptPoint1.y = 0;
- 168 ptPoint2.x = MemX;
- 169 ptPoint2.y = MemY;
- 170
- 171 CatchGraph(ptPoint1,
- 172 ptPoint2);
- 173 InvalidateRect(hWnd, NULL,
- 174 TRUE);
- 175 break;
- 176
- 177 case IDM_WIN :
- 178
- 179 GetWindowRect(hWnd, &Rect);
- 180 ptPoint1.x = Rect.left;
- 181 ptPoint1.y = Rect.top;
- 182 ptPoint2.x = Rect.right;
- 183 ptPoint2.y = Rect.bottom;
- 184
- 185 CatchGraph(ptPoint1,
- 186 ptPoint2);
- 187 InvalidateRect(hWnd, NULL,
- 188 TRUE);
- 189 break;
- 190 }
- 191 break;
- 192
- 193 case IDM_EXIT :
- 194 DestroyWindow(hWnd);
- 195 break;
- 196 }
- 197 return (0);
- 198
- 199 case WM_LBUTTONDOWN :
- 200 if (bRun)
- 201 {
- 202 bLBDown = TRUE;
- 203 ptPoint1 = MAKEPOINT(lParam);
- 204 ClientToScreen(hWnd, &ptPoint1);
- 205 ptPrePoint = ptPoint1;
- 206 }
- 207 return (0);
- 208
- 209 case WM_MOUSEMOVE :
- 210 if (bLBDown)
- 211 {
- 212 ptPoint2 = MAKEPOINT(lParam);
- 213 ClientToScreen(hWnd, &ptPoint2);
- 214
- 215 DrawRect(ptPoint1, ptPrePoint);
- 216 DrawRect(ptPoint1, ptPoint2);
- 217
- 218 ptPrePoint = ptPoint2;
- 219 }
- 220 return (0);
- 221
- 222 case WM_LBUTTONUP :
- 223 if (bLBDown)
- 224 {
- 225 bLBDown = FALSE;
- 226 bRun = FALSE;
- 227
- 228 DrawRect(ptPoint1, ptPoint2);
- 229
- 230 SetCursor(LoadCursor(NULL, IDC_WAIT));
- 231 CatchGraph(ptPoint1, ptPoint2);
- 232 SetCursor(LoadCursor(NULL, IDC_ARROW));
- 233
- 234 ReleaseCapture();
- 235 InvalidateRect(hWnd, NULL, TRUE);
- 236 }
- 237 return (0);
- 238
- 239 case WM_SIZE :
- 240 ptSize = MAKEPOINT(lParam);
- 241 InvalidateRect(hWnd, NULL, TRUE);
- 242 break;
- 243
- 244 case WM_PAINT :
- 245 hDC = BeginPaint(hWnd, &ps);
- 246
- 247 SetCursor(LoadCursor(NULL, IDC_WAIT));
- 248 if (MethodID == IDM_FIT)
- 249 StretchBlt(hDC, 0, 0,
- 250 ptSize.x, ptSize.y,
- 251 hMemDC, 0, 0,
- 252 ptPoint2.x-ptPoint1.x,
- 253 ptPoint2.y-ptPoint1.y,
- 254 SRCCOPY);
- 255 else
- 256 BitBlt(hDC, 0, 0,
- 257 ptPoint2.x-ptPoint1.x,
- 258 ptPoint2.y-ptPoint1.y,
- 259 hMemDC, 0, 0,
- 260 SRCCOPY);
- 261
- 262 SetCursor(LoadCursor(NULL, IDC_ARROW));
- 263
- 264 EndPaint(hWnd, &ps);
- 265 return (0);
- 266
- 267 case WM_DESTROY :
- 268 DeleteDC(hMemDC);
- 269 DeleteObject(hBitmap);
- 270 PostQuitMessage(0);
- 271 return (0);
- 272 }
- 273
- 274 return(DefWindowProc(hWnd, message, wParam, lParam));
- 275 }
- 276
- 277
- 278
- 279 void DrawRect(POINT ptP1, POINT ptP2)
- 280 {
- 281 HDC hDC;
- 282
- 283 hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
- 284
- 285 SetROP2(hDC, R2_NOT);
- 286 Rectangle(hDC, ptP1.x, ptP1.y, ptP2.x, ptP2.y);
- 287
- 288 DeleteDC(hDC);
- 289 }
- 290
- 291
- 292
- 293 void CatchGraph(POINT ptP1, POINT ptP2)
- 294 {
- 295 HDC hDC;
- 296
- 297 hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
- 298
- 299 PatBlt(hMemDC, 0, 0,
- 300 MemX, MemY, WHITENESS);
- 301 BitBlt(hMemDC, 0, 0,
- 302 ptP2.x-ptP1.x, ptP2.y-ptP1.y,
- 303 hDC, ptP1.x, ptP1.y,
- 304 SRCCOPY);
- 305
- 306 DeleteDC(hDC);
- 307 }