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

GDI/图象编程

开发平台:

Visual C++

  1.   1 /****************************************************************/
  2.   2 /*         Demo4_5   ---  Simple Paint                          */
  3.   3 /****************************************************************/
  4.   4 
  5.   5 #include <windows.h>
  6.   6 #include <math.h>
  7.   7 #include "demo4_5.h"
  8.   8 
  9.   9 
  10.  10 int  PASCAL  WinMain(HANDLE, HANDLE, LPSTR, int);
  11.  11 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
  12.  12 long FAR PASCAL ChooseCtrlProc(HWND, unsigned, WORD, LONG);
  13.  13 long FAR PASCAL LineWSCtrlProc(HWND, unsigned, WORD, LONG);
  14.  14 BOOL FAR PASCAL PenDlgProc(HWND, unsigned, WORD, LONG);
  15.  15 BOOL FAR PASCAL BrushDlgProc(HWND, unsigned, WORD, LONG);
  16.  16 
  17.  17 void DrawGraph(HDC, BOOL);
  18.  18 void DrawPencil(HDC);
  19.  19 void DrawLine(HDC, BOOL);
  20.  20 void DrawRect(HDC, BOOL);
  21.  21 void DrawEllip(HDC, BOOL);
  22.  22 void DrawCircle(HDC, BOOL);
  23.  23 void DrawRoundRect(HDC, BOOL);
  24.  24 
  25.  25 FARPROC lpPenDlgProc;
  26.  26 FARPROC lpBrushDlgProc;
  27.  27 
  28.  28 HANDLE  hInst;
  29.  29 
  30.  30 int     ToolID  = IDM_PENCIL;
  31.  31 
  32.  32 int     nPenColor = 1;
  33.  33 int     nPenStyle = PS_SOLID;
  34.  34 int     nPenWidth = 1;
  35.  35 
  36.  36 int     nBrushColor = 1;
  37.  37 int     nHatch      = -1;
  38.  38 
  39.  39 typedef struct tagCOLORSTRUCT {
  40.  40    int  cR;
  41.  41    int  cG;
  42.  42    int  cB;
  43.  43 } COLORSTRUCT;
  44.  44 
  45.  45 #define MKCOLOR(A) (RGB(A.cR, A.cG, A.cB))
  46.  46 
  47.  47 extern COLORSTRUCT crDefColor[28];
  48.  48 extern COLORSTRUCT crPCurColor[28];
  49.  49 extern COLORSTRUCT crBCurColor[28];
  50.  50 
  51.  51 POINT OrgPoint;
  52.  52 POINT PrePoint;
  53.  53 POINT CurPoint;
  54.  54 
  55.  55 /****************************************************************/
  56.  56 /*                      WinMain()                               */
  57.  57 /****************************************************************/
  58.  58 
  59.  59 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  60.  60                    LPSTR lpszCmdLine, int nCmdShow)
  61.  61 {
  62.  62    WNDCLASS wclass;
  63.  63    MSG      msg;
  64.  64    HWND     hWnd;
  65.  65    char     szName[] = "Demo4_5";
  66.  66 
  67.  67    if (!hPrevInstance)
  68.  68     {
  69.  69         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  70.  70         wclass.lpfnWndProc   = MainWndProc;
  71.  71         wclass.cbClsExtra    = 0;
  72.  72         wclass.cbWndExtra    = 0;
  73.  73         wclass.hInstance     = hInstance;
  74.  74         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  75.  75         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  76.  76         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  77.  77         wclass.lpszMenuName  = szName;
  78.  78         wclass.lpszClassName = szName;
  79.  79 
  80.  80         if (!RegisterClass (&wclass))
  81.  81            return (FALSE);
  82.  82 
  83.  83         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  84.  84         wclass.lpfnWndProc   = ChooseCtrlProc;
  85.  85         wclass.cbClsExtra    = 0;
  86.  86         wclass.cbWndExtra    = 0;
  87.  87         wclass.hInstance     = hInstance;
  88.  88         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  89.  89         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  90.  90         wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  91.  91         wclass.lpszMenuName  = NULL;
  92.  92         wclass.lpszClassName = "Choose";
  93.  93 
  94.  94         if (!RegisterClass (&wclass))
  95.  95            return (FALSE);
  96.  96 
  97.  97         wclass.style         = CS_HREDRAW | CS_VREDRAW;
  98.  98         wclass.lpfnWndProc   = LineWSCtrlProc;
  99.  99         wclass.cbClsExtra    = 0;
  100. 100         wclass.cbWndExtra    = 0;
  101. 101         wclass.hInstance     = hInstance;
  102. 102         wclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  103. 103         wclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  104. 104         wclass.hbrBackground = COLOR_WINDOW + 1;
  105. 105         wclass.lpszMenuName  = NULL;
  106. 106         wclass.lpszClassName = "LineWS";
  107. 107 
  108. 108         if (!RegisterClass (&wclass))
  109. 109            return (FALSE);
  110. 110     }
  111. 111 
  112. 112     hWnd = CreateWindow(
  113. 113                 szName,
  114. 114                 "Simple Paint" ,
  115. 115                 WS_OVERLAPPEDWINDOW,
  116. 116                 CW_USEDEFAULT,
  117. 117                 CW_USEDEFAULT,
  118. 118                 CW_USEDEFAULT,
  119. 119                 CW_USEDEFAULT,
  120. 120                 NULL,
  121. 121                 NULL,
  122. 122                 hInstance,
  123. 123                 NULL );
  124. 124 
  125. 125     if (!hWnd)
  126. 126         return (FALSE);
  127. 127 
  128. 128     ShowWindow(hWnd, nCmdShow);
  129. 129     UpdateWindow(hWnd);
  130. 130 
  131. 131     while (GetMessage(&msg, NULL, NULL,NULL))
  132. 132        {
  133. 133            TranslateMessage(&msg);
  134. 134            DispatchMessage(&msg);
  135. 135        }
  136. 136     return (msg.wParam);
  137. 137 }
  138. 138 
  139. 139 
  140. 140 
  141. 141 /****************************************************************/
  142. 142 /*                      MainWndProc()                           */
  143. 143 /****************************************************************/
  144. 144 
  145. 145 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
  146. 146                             WORD wParam, LONG lParam)
  147. 147 {
  148. 148    HDC           hDC;
  149. 149    HMENU         hMenu;
  150. 150    static BOOL   bLBDown;
  151. 151 
  152. 152    switch (message)
  153. 153     {
  154. 154       case WM_CREATE :
  155. 155                 hMenu = GetMenu(hWnd);
  156. 156                 CheckMenuItem(hMenu, IDM_PENCIL, MF_CHECKED);
  157. 157 
  158. 158                 memcpy(crPCurColor, crDefColor,
  159. 159                                 sizeof(crDefColor));
  160. 160                 memcpy(crBCurColor, crDefColor,
  161. 161                                 sizeof(crDefColor));
  162. 162 
  163. 163                 hInst = ((LPCREATESTRUCT) lParam)->hInstance;
  164. 164                 return (0);
  165. 165 
  166. 166       case WM_COMMAND :
  167. 167                 hMenu = GetMenu(hWnd);
  168. 168                 switch (wParam)
  169. 169                   {
  170. 170                     case IDM_PENCIL     :
  171. 171                     case IDM_LINE       :
  172. 172                     case IDM_RECT_F     :
  173. 173                     case IDM_RECT       :
  174. 174                     case IDM_ELLIP_F    :
  175. 175                     case IDM_ELLIP      :
  176. 176                     case IDM_CIRCLE_F   :
  177. 177                     case IDM_CIRCLE     :
  178. 178                     case IDM_ROUNDRECT_F:
  179. 179                     case IDM_ROUNDRECT  :
  180. 180 
  181. 181                          if (ToolID == wParam)
  182. 182                             return (0);
  183. 183 
  184. 184                          CheckMenuItem(hMenu, ToolID,
  185. 185                                         MF_UNCHECKED);
  186. 186                          ToolID = wParam;
  187. 187                          CheckMenuItem(hMenu, ToolID,
  188. 188                                         MF_CHECKED);
  189. 189                          break;
  190. 190 
  191. 191                     case IDM_CHOOSEPEN :
  192. 192 
  193. 193                          lpPenDlgProc = MakeProcInstance(
  194. 194                                (FARPROC) PenDlgProc, hInst);
  195. 195 
  196. 196                          DialogBox(hInst, "PENDLG", hWnd,
  197. 197                                    lpPenDlgProc);
  198. 198 
  199. 199                          FreeProcInstance(lpPenDlgProc);
  200. 200                          break;
  201. 201 
  202. 202                     case IDM_CHOOSEBRUSH :
  203. 203 
  204. 204                          lpBrushDlgProc = MakeProcInstance(
  205. 205                                (FARPROC) BrushDlgProc, hInst);
  206. 206 
  207. 207                          DialogBox(hInst, "BRUSHDLG", hWnd,
  208. 208                                    lpBrushDlgProc);
  209. 209 
  210. 210                          FreeProcInstance(lpBrushDlgProc);
  211. 211                          break;
  212. 212 
  213. 213                     case IDM_CLEAR :
  214. 214                          InvalidateRect(hWnd, NULL, TRUE);
  215. 215                          break;
  216. 216 
  217. 217                     case IDM_QUIT :
  218. 218                          DestroyWindow(hWnd);
  219. 219                          break;
  220. 220                   }
  221. 221                 return (0);
  222. 222 
  223. 223       case WM_LBUTTONDOWN :
  224. 224                 SetCapture(hWnd);
  225. 225                 bLBDown = TRUE;
  226. 226 
  227. 227                 OrgPoint = MAKEPOINT(lParam);
  228. 228                 CurPoint = PrePoint = OrgPoint;
  229. 229 
  230. 230                 return (0);
  231. 231 
  232. 232       case WM_LBUTTONUP :
  233. 233                 bLBDown = FALSE;
  234. 234                 ReleaseCapture();
  235. 235 
  236. 236                 hDC = GetDC(hWnd);
  237. 237                 DrawGraph(hDC, TRUE);
  238. 238                 ReleaseDC(hWnd, hDC);
  239. 239 
  240. 240                 return (0);
  241. 241 
  242. 242       case WM_MOUSEMOVE :
  243. 243                 if (bLBDown)
  244. 244                   {
  245. 245                     PrePoint = CurPoint;
  246. 246                     CurPoint = MAKEPOINT(lParam);
  247. 247 
  248. 248                     hDC = GetDC(hWnd);
  249. 249                     DrawGraph(hDC, FALSE);
  250. 250                     ReleaseDC(hWnd, hDC);
  251. 251                   }
  252. 252                 return (0);
  253. 253 
  254. 254       case WM_DESTROY :
  255. 255                 PostQuitMessage(0);
  256. 256                 return (0);
  257. 257 
  258. 258       default :
  259. 259          return(DefWindowProc(hWnd, message, wParam, lParam));
  260. 260     }
  261. 261 }
  262. 262 
  263. 263 
  264. 264 
  265. 265 HBRUSH MyCreateBrush(int nHatchStyle, COLORREF crColor)
  266. 266 {
  267. 267    HBRUSH hBrush;
  268. 268 
  269. 269    if (nHatchStyle == -1)
  270. 270      hBrush = CreateSolidBrush(crColor);
  271. 271    else
  272. 272      hBrush = CreateHatchBrush(nHatchStyle, crColor);
  273. 273 
  274. 274    return (hBrush);
  275. 275 }
  276. 276 
  277. 277 
  278. 278 
  279. 279 void DrawGraph(HDC hDC, BOOL bSure)
  280. 280 {
  281. 281    HPEN   hPen, hPrePen;
  282. 282    HBRUSH hBrush, hPreBrush;
  283. 283 
  284. 284    if (ToolID==IDM_PENCIL || bSure)
  285. 285      {
  286. 286        hPen   = CreatePen(nPenStyle, nPenWidth,
  287. 287                         MKCOLOR(crPCurColor[nPenColor]));
  288. 288        hPrePen   = SelectObject(hDC, hPen);
  289. 289 
  290. 290        if (ToolID==IDM_RECT_F   || ToolID==IDM_ELLIP_F ||
  291. 291            ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
  292. 292          {
  293. 293            hBrush = MyCreateBrush(nHatch,
  294. 294                         MKCOLOR(crBCurColor[nBrushColor]));
  295. 295            hPreBrush = SelectObject(hDC, hBrush);
  296. 296          }
  297. 297        else
  298. 298          {
  299. 299            hBrush = GetStockObject(NULL_BRUSH);
  300. 300            hPreBrush = SelectObject(hDC, hBrush);
  301. 301          }
  302. 302      }
  303. 303    else
  304. 304      SelectObject(hDC, GetStockObject(NULL_BRUSH));
  305. 305 
  306. 306    switch (ToolID)
  307. 307      {
  308. 308        case IDM_PENCIL :
  309. 309               DrawPencil(hDC);
  310. 310               break;
  311. 311 
  312. 312        case IDM_LINE :
  313. 313               DrawLine(hDC, bSure);
  314. 314               break;
  315. 315 
  316. 316        case IDM_RECT_F :
  317. 317        case IDM_RECT   :
  318. 318               DrawRect(hDC, bSure);
  319. 319               break;
  320. 320 
  321. 321        case IDM_ELLIP_F :
  322. 322        case IDM_ELLIP   :
  323. 323               DrawEllip(hDC, bSure);
  324. 324               break;
  325. 325 
  326. 326        case IDM_CIRCLE_F :
  327. 327        case IDM_CIRCLE   :
  328. 328               DrawCircle(hDC, bSure);
  329. 329               break;
  330. 330 
  331. 331        case IDM_ROUNDRECT_F :
  332. 332        case IDM_ROUNDRECT   :
  333. 333               DrawRoundRect(hDC, bSure);
  334. 334               break;
  335. 335      }
  336. 336 
  337. 337    if (ToolID==IDM_PENCIL || bSure)
  338. 338      {
  339. 339        SelectObject(hDC, hPrePen);
  340. 340        DeleteObject(hPen);
  341. 341 
  342. 342        if (ToolID==IDM_RECT_F   || ToolID==IDM_ELLIP_F ||
  343. 343            ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
  344. 344          {
  345. 345            SelectObject(hDC, hPreBrush);
  346. 346            DeleteObject(hBrush);
  347. 347          }
  348. 348        else
  349. 349          {
  350. 350            SelectObject(hDC, hPreBrush);
  351. 351          }
  352. 352      }
  353. 353 }
  354. 354 
  355. 355 
  356. 356 
  357. 357 void DrawPencil(HDC hDC)
  358. 358 {
  359. 359    MoveTo(hDC, PrePoint.x, PrePoint.y);
  360. 360    LineTo(hDC, CurPoint.x, CurPoint.y);
  361. 361 }
  362. 362 
  363. 363 
  364. 364 
  365. 365 void DrawLine(HDC hDC, BOOL bSure)
  366. 366 {
  367. 367    int  nDrawMode;
  368. 368 
  369. 369    if (! bSure)
  370. 370      {
  371. 371        nDrawMode = SetROP2(hDC, R2_NOT);
  372. 372 
  373. 373        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
  374. 374        LineTo(hDC, PrePoint.x, PrePoint.y);
  375. 375 
  376. 376        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
  377. 377        LineTo(hDC, CurPoint.x, CurPoint.y);
  378. 378 
  379. 379        SetROP2(hDC, nDrawMode);
  380. 380      }
  381. 381    else
  382. 382      {
  383. 383        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
  384. 384        LineTo(hDC, CurPoint.x, CurPoint.y);
  385. 385      }
  386. 386 }
  387. 387 
  388. 388 
  389. 389 
  390. 390 void DrawRect(HDC hDC, BOOL bSure)
  391. 391 {
  392. 392    int  nDrawMode;
  393. 393 
  394. 394    if (! bSure)
  395. 395      {
  396. 396        nDrawMode = SetROP2(hDC, R2_NOT);
  397. 397 
  398. 398        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
  399. 399                       PrePoint.x, PrePoint.y);
  400. 400 
  401. 401        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
  402. 402                       CurPoint.x, CurPoint.y);
  403. 403 
  404. 404        SetROP2(hDC, nDrawMode);
  405. 405      }
  406. 406    else
  407. 407      {
  408. 408        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
  409. 409                       CurPoint.x, CurPoint.y);
  410. 410      }
  411. 411 }
  412. 412 
  413. 413 
  414. 414 
  415. 415 void DrawEllip(HDC hDC, BOOL bSure)
  416. 416 {
  417. 417    int  nDrawMode;
  418. 418 
  419. 419    if (! bSure)
  420. 420      {
  421. 421        nDrawMode = SetROP2(hDC, R2_NOT);
  422. 422 
  423. 423        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
  424. 424                     PrePoint.x, PrePoint.y);
  425. 425 
  426. 426        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
  427. 427                     CurPoint.x, CurPoint.y);
  428. 428 
  429. 429        SetROP2(hDC, nDrawMode);
  430. 430      }
  431. 431    else
  432. 432      {
  433. 433        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
  434. 434                     CurPoint.x, CurPoint.y);
  435. 435      }
  436. 436 }
  437. 437 
  438. 438 
  439. 439 
  440. 440 void DrawRoundRect(HDC hDC, BOOL bSure)
  441. 441 {
  442. 442    int  nDrawMode;
  443. 443 
  444. 444    if (! bSure)
  445. 445      {
  446. 446        nDrawMode = SetROP2(hDC, R2_NOT);
  447. 447 
  448. 448        RoundRect(hDC, OrgPoint.x, OrgPoint.y,
  449. 449                       PrePoint.x, PrePoint.y,
  450. 450                       (PrePoint.x-OrgPoint.x)/4,
  451. 451                       (PrePoint.y-OrgPoint.y)/4);
  452. 452 
  453. 453        RoundRect(hDC, OrgPoint.x, OrgPoint.y,
  454. 454                       CurPoint.x, CurPoint.y,
  455. 455                       (CurPoint.x-OrgPoint.x)/4,
  456. 456                       (CurPoint.y-OrgPoint.y)/4);
  457. 457 
  458. 458        SetROP2(hDC, nDrawMode);
  459. 459      }
  460. 460    else
  461. 461      {
  462. 462        RoundRect(hDC, OrgPoint.x, OrgPoint.y,
  463. 463                       CurPoint.x, CurPoint.y,
  464. 464                       (CurPoint.x-OrgPoint.x)/4,
  465. 465                       (CurPoint.y-OrgPoint.y)/4);
  466. 466      }
  467. 467 }
  468. 468 
  469. 469 
  470. 470 
  471. 471 void DrawCircle(HDC hDC, BOOL bSure)
  472. 472 {
  473. 473    int    nDrawMode;
  474. 474    int    nLogPixSx, nLogPixSy;
  475. 475    int    Width, Height;
  476. 476    int    SignX, SignY;
  477. 477 
  478. 478    nLogPixSx = GetDeviceCaps(hDC, LOGPIXELSX);
  479. 479    nLogPixSy = GetDeviceCaps(hDC, LOGPIXELSY);
  480. 480 
  481. 481    Width  = CurPoint.x - OrgPoint.x;
  482. 482    Height = CurPoint.y - OrgPoint.y;
  483. 483    SignX  = (Width  >= 0 ? 1 : -1);
  484. 484    SignY  = (Height >= 0 ? 1 : -1);
  485. 485 
  486. 486    if (fabs((float) Width/nLogPixSx) >
  487. 487        fabs((float) Height/nLogPixSy) )
  488. 488       {
  489. 489          CurPoint.x = OrgPoint.x + (float)
  490. 490            fabs(Height) * nLogPixSx / nLogPixSy * SignX;
  491. 491       }
  492. 492    else
  493. 493       {
  494. 494          CurPoint.y = OrgPoint.y + (float)
  495. 495            fabs(Width) * nLogPixSy / nLogPixSx * SignY;
  496. 496       }
  497. 497 
  498. 498 
  499. 499    if (! bSure)
  500. 500      {
  501. 501        nDrawMode = SetROP2(hDC, R2_NOT);
  502. 502 
  503. 503        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
  504. 504                     PrePoint.x, PrePoint.y);
  505. 505 
  506. 506        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
  507. 507                     CurPoint.x, CurPoint.y);
  508. 508 
  509. 509        SetROP2(hDC, nDrawMode);
  510. 510      }
  511. 511    else
  512. 512      {
  513. 513        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
  514. 514                     CurPoint.x, CurPoint.y);
  515. 515      }
  516. 516 }