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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo3_3   ---  Mapping Mode test                     */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include "demo3_3.h"
  7.   7 
  8.   8 #define MAXPOINTS 100
  9.   9 
  10.  10 typedef struct tagSHAPE {
  11.  11            int  x1, y1;
  12.  12            int  x2, y2;
  13.  13            int  ShapeID;
  14.  14            int  MapModeID;
  15.  15 } SHAPE;
  16.  16 
  17.  17 static RECT LineRect;
  18.  18 static RECT RectRect;
  19.  19 static RECT EllipRect;
  20.  20 
  21.  21 
  22.  22 int  PASCAL  WinMain(HANDLE, HANDLE, LPSTR, int);
  23.  23 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  24.  24 BOOL FAR PASCAL LineDlgProc(HWND, unsigned, WORD, LONG);
  25.  25 BOOL FAR PASCAL RectDlgProc(HWND, unsigned, WORD, LONG);
  26.  26 BOOL FAR PASCAL EllipDlgProc(HWND, unsigned, WORD, LONG);
  27.  27 
  28.  28 FARPROC lpLineDlgProc;
  29.  29 FARPROC lpRectDlgProc;
  30.  30 FARPROC lpEllipDlgProc;
  31.  31 
  32.  32 void DrawAllGraph(HDC, RECT, SHAPE *, int);
  33.  33 
  34.  34 
  35.  35 
  36.  36 /****************************************************************/
  37.  37 /*                      WinMain()                               */
  38.  38 /****************************************************************/
  39.  39 
  40.  40 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  41.  41                    LPSTR lpszCmdLine, int nCmdShow)
  42.  42 {
  43.  43    WNDCLASS wclass;
  44.  44    MSG      msg;
  45.  45    HWND     hWnd;
  46.  46    char     szName[] = "Demo3_3";
  47.  47 
  48.  48    if (!hPrevInstance)
  49.  49     {
  50.  50         wclass.style         = NULL;
  51.  51         wclass.lpfnWndProc   = MainWndProc;
  52.  52         wclass.cbClsExtra    = 0;
  53.  53         wclass.cbWndExtra    = 0;
  54.  54         wclass.hInstance     = hInstance;
  55.  55         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  56.  56         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  57.  57         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  58.  58         wclass.lpszMenuName  = szName;
  59.  59         wclass.lpszClassName = szName;
  60.  60 
  61.  61         if (!RegisterClass (&wclass))
  62.  62            return (FALSE);
  63.  63     }
  64.  64 
  65.  65     hWnd = CreateWindow(
  66.  66                 szName,
  67.  67                 "Mapping Mode test",
  68.  68                 WS_OVERLAPPEDWINDOW,
  69.  69                 CW_USEDEFAULT,
  70.  70                 CW_USEDEFAULT,
  71.  71                 CW_USEDEFAULT,
  72.  72                 CW_USEDEFAULT,
  73.  73                 NULL,
  74.  74                 NULL,
  75.  75                 hInstance,
  76.  76                 NULL );
  77.  77 
  78.  78     if (!hWnd)
  79.  79         return (FALSE);
  80.  80 
  81.  81     ShowWindow(hWnd, nCmdShow);
  82.  82     UpdateWindow(hWnd);
  83.  83 
  84.  84     while (GetMessage(&msg, NULL, NULL,NULL))
  85.  85        {
  86.  86            TranslateMessage(&msg);
  87.  87            DispatchMessage(&msg);
  88.  88        }
  89.  89     return (msg.wParam);
  90.  90 }
  91.  91 
  92.  92 
  93.  93 
  94.  94 /****************************************************************/
  95.  95 /*                      MainWndProc()                           */
  96.  96 /****************************************************************/
  97.  97 
  98.  98 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  99.  99                             WORD wParam, LONG lParam)
  100. 100 {
  101. 101    PAINTSTRUCT   ps;
  102. 102    HDC           hDC;
  103. 103    HMENU         hMenu;
  104. 104    static int    MapModeID = IDM_TEXT;
  105. 105    static int    ShapeID = IDM_LINE;
  106. 106    int           i;
  107. 107    static int    Num = 0;
  108. 108    static SHAPE  Shape[MAXPOINTS];
  109. 109    static HANDLE hInst;
  110. 110    HANDLE        hBrush;
  111. 111 
  112. 112    switch (message)
  113. 113     {
  114. 114         case WM_CREATE :
  115. 115                 hMenu = GetMenu(hWnd);
  116. 116                 CheckMenuItem(hMenu, MapModeID, MF_CHECKED);
  117. 117                 CheckMenuItem(hMenu, ShapeID, MF_CHECKED);
  118. 118 
  119. 119                 SetRect(&LineRect, 0, 0, 0, 0);
  120. 120                 SetRect(&RectRect, 0, 0, 0, 0);
  121. 121                 SetRect(&EllipRect, 0, 0, 0, 0);
  122. 122                 hInst =  ((LPCREATESTRUCT)lParam)->hInstance;
  123. 123                 return (0);
  124. 124 
  125. 125         case WM_COMMAND :
  126. 126                 switch(wParam)
  127. 127                   {
  128. 128                     case IDM_TEXT  :
  129. 129                     case IDM_LOMET :
  130. 130                     case IDM_HIMET :
  131. 131                     case IDM_LOENG :
  132. 132                     case IDM_HIENG :
  133. 133                     case IDM_TWIPS :
  134. 134 
  135. 135                          if (MapModeID != wParam)
  136. 136                            {
  137. 137                              hMenu = GetMenu(hWnd);
  138. 138                              CheckMenuItem(hMenu, MapModeID,
  139. 139                                                   MF_UNCHECKED);
  140. 140                              MapModeID = wParam;
  141. 141                              CheckMenuItem(hMenu, wParam,
  142. 142                                                   MF_CHECKED);
  143. 143                            }
  144. 144                          break;
  145. 145 
  146. 146                     case IDM_LINE :
  147. 147                          if (ShapeID != wParam)
  148. 148                            {
  149. 149                              hMenu = GetMenu(hWnd);
  150. 150                              CheckMenuItem(hMenu, ShapeID,
  151. 151                                                   MF_UNCHECKED);
  152. 152                              ShapeID = wParam;
  153. 153                              CheckMenuItem(hMenu, wParam,
  154. 154                                                   MF_CHECKED);
  155. 155                            }
  156. 156 
  157. 157                          lpLineDlgProc = MakeProcInstance(
  158. 158                                  (FARPROC) LineDlgProc, hInst);
  159. 159 
  160. 160                          DialogBox(hInst, "LINEDLG", hWnd,
  161. 161                                    lpLineDlgProc);
  162. 162 
  163. 163                          FreeProcInstance(lpLineDlgProc);
  164. 164                          break;
  165. 165 
  166. 166                     case IDM_RECT :
  167. 167                          if (ShapeID != wParam)
  168. 168                            {
  169. 169                              hMenu = GetMenu(hWnd);
  170. 170                              CheckMenuItem(hMenu, ShapeID,
  171. 171                                                   MF_UNCHECKED);
  172. 172                              ShapeID = wParam;
  173. 173                              CheckMenuItem(hMenu, wParam,
  174. 174                                                   MF_CHECKED);
  175. 175                            }
  176. 176 
  177. 177                          lpRectDlgProc = MakeProcInstance(
  178. 178                                  (FARPROC) RectDlgProc, hInst);
  179. 179 
  180. 180                          DialogBox(hInst, "RECTDLG", hWnd,
  181. 181                                    lpRectDlgProc);
  182. 182 
  183. 183                          FreeProcInstance(lpRectDlgProc);
  184. 184                          break;
  185. 185 
  186. 186                     case IDM_ELLIP :
  187. 187 
  188. 188                          if (ShapeID != wParam)
  189. 189                            {
  190. 190                              hMenu = GetMenu(hWnd);
  191. 191                              CheckMenuItem(hMenu, ShapeID,
  192. 192                                                   MF_UNCHECKED);
  193. 193                              ShapeID = wParam;
  194. 194                              CheckMenuItem(hMenu, wParam,
  195. 195                                                   MF_CHECKED);
  196. 196                            }
  197. 197 
  198. 198                          lpEllipDlgProc = MakeProcInstance(
  199. 199                                  (FARPROC) EllipDlgProc, hInst);
  200. 200 
  201. 201                          DialogBox(hInst, "ELLIPDLG", hWnd,
  202. 202                                    lpEllipDlgProc);
  203. 203 
  204. 204                          FreeProcInstance(lpEllipDlgProc);
  205. 205                          break;
  206. 206 
  207. 207                     case IDM_DRAW :
  208. 208                          if (Num == MAXPOINTS)
  209. 209                            {
  210. 210                               MessageBox(hWnd,
  211. 211                                 "Memory not enough", "Warning",
  212. 212                                 MB_OK | MB_ICONEXCLAMATION);
  213. 213                               break;
  214. 214                            }
  215. 215 
  216. 216                          hDC = GetDC(hWnd);
  217. 217 
  218. 218                          switch (MapModeID)
  219. 219                            {
  220. 220                              case IDM_TEXT :
  221. 221                                  SetMapMode(hDC, MM_TEXT);
  222. 222                                  break;
  223. 223 
  224. 224                              case IDM_LOMET :
  225. 225                                  SetMapMode(hDC, MM_LOMETRIC);
  226. 226                                  break;
  227. 227 
  228. 228                              case IDM_HIMET :
  229. 229                                  SetMapMode(hDC, MM_HIMETRIC);
  230. 230                                  break;
  231. 231 
  232. 232                              case IDM_LOENG :
  233. 233                                  SetMapMode(hDC, MM_LOENGLISH);
  234. 234                                  break;
  235. 235 
  236. 236                              case IDM_HIENG :
  237. 237                                  SetMapMode(hDC, MM_HIENGLISH);
  238. 238                                  break;
  239. 239 
  240. 240                              case IDM_TWIPS :
  241. 241                                  SetMapMode(hDC, MM_TWIPS);
  242. 242                                  break;
  243. 243                            }
  244. 244 
  245. 245                          switch (ShapeID)
  246. 246                            {
  247. 247                             case IDM_LINE :
  248. 248                                  MoveTo(hDC, LineRect.left,
  249. 249                                            LineRect.top);
  250. 250                                  LineTo(hDC, LineRect.right,
  251. 251                                            LineRect.bottom);
  252. 252                                  Shape[Num].x1 =
  253. 253                                         LineRect.left;
  254. 254                                  Shape[Num].y1 =
  255. 255                                         LineRect.top;
  256. 256                                  Shape[Num].x2 =
  257. 257                                         LineRect.right;
  258. 258                                  Shape[Num].y2 =
  259. 259                                         LineRect.bottom;
  260. 260                                  break;
  261. 261 
  262. 262                             case IDM_RECT :
  263. 263                                  hBrush = GetStockObject(
  264. 264                                                  NULL_BRUSH);
  265. 265                                  SelectObject(hDC, hBrush);
  266. 266                                  Rectangle(hDC, RectRect.left,
  267. 267                                            RectRect.top,
  268. 268                                            RectRect.right,
  269. 269                                            RectRect.bottom);
  270. 270                                  Shape[Num].x1 =
  271. 271                                         RectRect.left;
  272. 272                                  Shape[Num].y1 =
  273. 273                                         RectRect.top;
  274. 274                                  Shape[Num].x2 =
  275. 275                                         RectRect.right;
  276. 276                                  Shape[Num].y2 =
  277. 277                                         RectRect.bottom;
  278. 278                                  break;
  279. 279 
  280. 280                             case IDM_ELLIP :
  281. 281                                  hBrush = GetStockObject(
  282. 282                                                  NULL_BRUSH);
  283. 283                                  SelectObject(hDC, hBrush);
  284. 284                                  Ellipse(hDC, EllipRect.left,
  285. 285                                          EllipRect.top,
  286. 286                                          EllipRect.right,
  287. 287                                          EllipRect.bottom);
  288. 288                                  Shape[Num].x1 =
  289. 289                                         EllipRect.left;
  290. 290                                  Shape[Num].y1 =
  291. 291                                         EllipRect.top;
  292. 292                                  Shape[Num].x2 =
  293. 293                                         EllipRect.right;
  294. 294                                  Shape[Num].y2 =
  295. 295                                         EllipRect.bottom;
  296. 296                                  break;
  297. 297                            }
  298. 298 
  299. 299                          ReleaseDC(hWnd, hDC);
  300. 300 
  301. 301                          Shape[Num].ShapeID = ShapeID;
  302. 302                          Shape[Num].MapModeID = MapModeID;
  303. 303                          Num ++;
  304. 304                          break;
  305. 305 
  306. 306                     case IDM_CLEAR :
  307. 307                          Num = 0;
  308. 308                          InvalidateRect(hWnd, NULL, TRUE);
  309. 309                          UpdateWindow(hWnd);
  310. 310                          break;
  311. 311 
  312. 312                   }
  313. 313                 return (0);
  314. 314 
  315. 315       case WM_PAINT :
  316. 316             hDC = BeginPaint(hWnd, &ps);
  317. 317 
  318. 318             DrawAllGraph(hDC, ps.rcPaint, Shape, Num);
  319. 319 
  320. 320             EndPaint(hWnd, &ps);
  321. 321             return (0);
  322. 322 
  323. 323       case WM_DESTROY :
  324. 324                 PostQuitMessage(0);
  325. 325                 return (0);
  326. 326 
  327. 327       default :
  328. 328         return (DefWindowProc(hWnd, message, wParam, lParam));
  329. 329     }
  330. 330 }
  331. 331 
  332. 332 
  333. 333 
  334. 334 void DrawAllGraph(HDC hDC, RECT psRect, SHAPE Shape[], int Num)
  335. 335 {
  336. 336    int     i, temp;
  337. 337    RECT    Rect, dstRect;
  338. 338    HANDLE  hBrush;
  339. 339 
  340. 340    for (i=0; i<Num; i++)
  341. 341      {
  342. 342         switch (Shape[i].MapModeID)
  343. 343           {
  344. 344             case IDM_TEXT :
  345. 345               SetMapMode(hDC, MM_TEXT);
  346. 346               break;
  347. 347 
  348. 348             case IDM_LOMET :
  349. 349               SetMapMode(hDC, MM_LOMETRIC);
  350. 350               break;
  351. 351 
  352. 352             case IDM_HIMET :
  353. 353               SetMapMode(hDC, MM_HIMETRIC);
  354. 354               break;
  355. 355 
  356. 356             case IDM_LOENG :
  357. 357               SetMapMode(hDC, MM_LOENGLISH);
  358. 358               break;
  359. 359 
  360. 360             case IDM_HIENG :
  361. 361               SetMapMode(hDC, MM_HIENGLISH);
  362. 362               break;
  363. 363 
  364. 364             case IDM_TWIPS :
  365. 365               SetMapMode(hDC, MM_TWIPS);
  366. 366               break;
  367. 367           }
  368. 368 
  369. 369         Rect.left  = Shape[i].x1;
  370. 370         Rect.right = Shape[i].x2;
  371. 371         Rect.top   = Shape[i].y1;
  372. 372         Rect.bottom= Shape[i].y2;
  373. 373 
  374. 374         LPtoDP(hDC, (LPPOINT) &Rect, 2);
  375. 375 
  376. 376         if (Rect.left > Rect.right)
  377. 377           {
  378. 378             temp = Rect.left;
  379. 379             Rect.left = Rect.right;
  380. 380             Rect.right = temp;
  381. 381           }
  382. 382         if (Rect.top > Rect.bottom)
  383. 383           {
  384. 384             temp = Rect.top;
  385. 385             Rect.top = Rect.bottom;
  386. 386             Rect.bottom = temp;
  387. 387           }
  388. 388 
  389. 389         IntersectRect(&dstRect, &Rect, &psRect);
  390. 390         if (IsRectEmpty(&dstRect))
  391. 391             continue;
  392. 392 
  393. 393         switch (Shape[i].ShapeID)
  394. 394          {
  395. 395            case IDM_LINE :
  396. 396                      MoveTo(hDC, Shape[i].x1, Shape[i].y1);
  397. 397                      LineTo(hDC, Shape[i].x2, Shape[i].y2);
  398. 398                      break;
  399. 399 
  400. 400            case IDM_RECT :
  401. 401                      hBrush = GetStockObject(NULL_BRUSH);
  402. 402                      SelectObject(hDC, hBrush);
  403. 403                      Rectangle(hDC, Shape[i].x1, Shape[i].y1,
  404. 404                                     Shape[i].x2, Shape[i].y2);
  405. 405                      break;
  406. 406 
  407. 407            case IDM_ELLIP :
  408. 408                      hBrush = GetStockObject(NULL_BRUSH);
  409. 409                      SelectObject(hDC, hBrush);
  410. 410                      Ellipse(hDC, Shape[i].x1, Shape[i].y1,
  411. 411                                   Shape[i].x2, Shape[i].y2);
  412. 412                      break;
  413. 413          }
  414. 414      }
  415. 415 }
  416. 416 
  417. 417 
  418. 418 BOOL FAR PASCAL LineDlgProc(HWND hDlg, unsigned msg,
  419. 419                             WORD wParam, LONG lParam)
  420. 420 {
  421. 421    BOOL  btran1, btran2, btran3, btran4;
  422. 422    int   x1, y1, x2, y2;
  423. 423 
  424. 424    switch (msg)
  425. 425      {
  426. 426        case WM_INITDIALOG :
  427. 427           SetDlgItemInt(hDlg, DI_LLEFT,
  428. 428                                 LineRect.left, TRUE);
  429. 429           SetDlgItemInt(hDlg, DI_LTOP,
  430. 430                                 LineRect.top, TRUE);
  431. 431           SetDlgItemInt(hDlg, DI_LRIGHT,
  432. 432                                 LineRect.right, TRUE);
  433. 433           SetDlgItemInt(hDlg, DI_LBOTTOM,
  434. 434                                 LineRect.bottom, TRUE);
  435. 435           return (TRUE);
  436. 436 
  437. 437        case WM_COMMAND :
  438. 438           switch (wParam)
  439. 439             {
  440. 440               case DI_LOK :
  441. 441                   x1 = GetDlgItemInt(hDlg, DI_LLEFT,
  442. 442                                      &btran1, TRUE);
  443. 443                   y1 = GetDlgItemInt(hDlg, DI_LTOP,
  444. 444                                      &btran2, TRUE);
  445. 445                   x2 = GetDlgItemInt(hDlg, DI_LRIGHT,
  446. 446                                      &btran3, TRUE);
  447. 447                   y2 = GetDlgItemInt(hDlg, DI_LBOTTOM,
  448. 448                                      &btran4, TRUE);
  449. 449 
  450. 450                   if (btran1 && btran2 && btran3 && btran4)
  451. 451                     SetRect(&LineRect, x1, y1, x2, y2);
  452. 452 
  453. 453                   EndDialog(hDlg, TRUE);
  454. 454                   return (TRUE);
  455. 455 
  456. 456               case DI_LCANCEL :
  457. 457                   EndDialog(hDlg, FALSE);
  458. 458                   return (TRUE);
  459. 459             }
  460. 460           return (TRUE);
  461. 461 
  462. 462        default :
  463. 463           return (FALSE);
  464. 464      }
  465. 465 }
  466. 466 
  467. 467 
  468. 468 BOOL FAR PASCAL RectDlgProc(HWND hDlg, unsigned msg,
  469. 469                             WORD wParam, LONG lParam)
  470. 470 {
  471. 471    BOOL  btran1, btran2, btran3, btran4;
  472. 472    int   x1, y1, x2, y2;
  473. 473 
  474. 474    switch (msg)
  475. 475      {
  476. 476        case WM_INITDIALOG :
  477. 477           SetDlgItemInt(hDlg, DI_RLEFT,
  478. 478                                 RectRect.left, TRUE);
  479. 479           SetDlgItemInt(hDlg, DI_RTOP,
  480. 480                                 RectRect.top, TRUE);
  481. 481           SetDlgItemInt(hDlg, DI_RRIGHT,
  482. 482                                 RectRect.right, TRUE);
  483. 483           SetDlgItemInt(hDlg, DI_RBOTTOM,
  484. 484                                 RectRect.bottom, TRUE);
  485. 485           return (TRUE);
  486. 486 
  487. 487        case WM_COMMAND :
  488. 488           switch (wParam)
  489. 489             {
  490. 490               case DI_ROK :
  491. 491                   x1 = GetDlgItemInt(hDlg, DI_RLEFT,
  492. 492                                      &btran1, TRUE);
  493. 493                   y1 = GetDlgItemInt(hDlg, DI_RTOP,
  494. 494                                      &btran2, TRUE);
  495. 495                   x2 = GetDlgItemInt(hDlg, DI_RRIGHT,
  496. 496                                      &btran3, TRUE);
  497. 497                   y2 = GetDlgItemInt(hDlg, DI_RBOTTOM,
  498. 498                                      &btran4, TRUE);
  499. 499 
  500. 500                   if (btran1 && btran2 && btran3 && btran4)
  501. 501                     SetRect(&RectRect, x1, y1, x2, y2);
  502. 502 
  503. 503                   EndDialog(hDlg, TRUE);
  504. 504                   return (TRUE);
  505. 505 
  506. 506               case DI_RCANCEL :
  507. 507                   EndDialog(hDlg, FALSE);
  508. 508                   return (TRUE);
  509. 509             }
  510. 510           return (TRUE);
  511. 511 
  512. 512        default :
  513. 513           return (FALSE);
  514. 514      }
  515. 515 }
  516. 516 
  517. 517 
  518. 518 BOOL FAR PASCAL EllipDlgProc(HWND hDlg, unsigned msg,
  519. 519                              WORD wParam, LONG lParam)
  520. 520 {
  521. 521    BOOL  btran1, btran2, btran3, btran4;
  522. 522    int   x1, y1, x2, y2;
  523. 523 
  524. 524    switch (msg)
  525. 525      {
  526. 526        case WM_INITDIALOG :
  527. 527           SetDlgItemInt(hDlg, DI_ELEFT,
  528. 528                                 EllipRect.left, TRUE);
  529. 529           SetDlgItemInt(hDlg, DI_ETOP,
  530. 530                                 EllipRect.top, TRUE);
  531. 531           SetDlgItemInt(hDlg, DI_ERIGHT,
  532. 532                                 EllipRect.right, TRUE);
  533. 533           SetDlgItemInt(hDlg, DI_EBOTTOM,
  534. 534                                 EllipRect.bottom, TRUE);
  535. 535           return (TRUE);
  536. 536 
  537. 537        case WM_COMMAND :
  538. 538           switch (wParam)
  539. 539             {
  540. 540               case DI_EOK :
  541. 541                   x1 = GetDlgItemInt(hDlg, DI_ELEFT,
  542. 542                                      &btran1, TRUE);
  543. 543                   y1 = GetDlgItemInt(hDlg, DI_ETOP,
  544. 544                                      &btran2, TRUE);
  545. 545                   x2 = GetDlgItemInt(hDlg, DI_ERIGHT,
  546. 546                                      &btran3, TRUE);
  547. 547                   y2 = GetDlgItemInt(hDlg, DI_EBOTTOM,
  548. 548                                      &btran4, TRUE);
  549. 549 
  550. 550                   if (btran1 && btran2 && btran3 && btran4)
  551. 551                     SetRect(&EllipRect, x1, y1, x2, y2);
  552. 552 
  553. 553                   EndDialog(hDlg, TRUE);
  554. 554                   return (TRUE);
  555. 555 
  556. 556               case DI_ECANCEL :
  557. 557                   EndDialog(hDlg, FALSE);
  558. 558                   return (TRUE);
  559. 559             }
  560. 560           return (TRUE);
  561. 561 
  562. 562        default :
  563. 563           return (FALSE);
  564. 564      }
  565. 565 }
  566. 566