DEMO5_4.C
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:10k
源码类别:

GDI/图象编程

开发平台:

Visual C++

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