home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / GDIINPUT.PAK / BRUSHDLG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  11.2 KB  |  410 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   brushdlg.c
  9. //
  10. //  PURPOSE:   Displays the "Brush Style" dialog box
  11. //
  12. //  FUNCTIONS:
  13. //    BrushDlg        - Process messages for "Brush Style" dialog box.
  14. //    MsgBrushInit    - Initialize the Brush dialog with info from lparam.
  15. //    MsgBrushPaint   - Paint the Example Window in the Brush dialog
  16. //    MsgBrushCommand - Process WM_COMMAND messages sent to the Brush dialog.
  17. //    CmdBrushStyle   - Track the currently selected brush style.
  18. //    CmdBrushHatch   - Track the currently selected hatch style.
  19. //    CmdBrushColor   - Put up the ChooseColor dialog to select brush color.
  20. //    CmdBrushDone    - Free the Brush dialog and related data.
  21. //
  22. //  COMMENTS:
  23. //
  24.  
  25. #include <windows.h>            // required for all Windows applications
  26. #include <windowsx.h>
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "brushdlg.h"           // Controls ID's for the Brush dialog
  29.  
  30. // global variables specific to this module
  31. RECT rcExample;                 // location of example window in dialog
  32.  
  33. // prototypes specific to this module
  34. LRESULT MsgBrushCommand (HWND, UINT, WPARAM, LPARAM);
  35. LRESULT MsgBrushInit    (HWND, UINT, WPARAM, LPARAM);
  36. LRESULT MsgBrushPaint   (HWND, UINT, WPARAM, LPARAM);
  37. LRESULT CmdBrushStyle   (HWND, WORD, WORD, HWND);
  38. LRESULT CmdBrushHatch   (HWND, WORD, WORD, HWND);
  39. LRESULT CmdBrushColor   (HWND, WORD, WORD, HWND);
  40. LRESULT CmdBrushDone    (HWND, WORD, WORD, HWND);
  41.  
  42. // Brush dialog message table definition.
  43. MSD rgmsdBrush[] =
  44. {
  45.     {WM_COMMAND,    MsgBrushCommand},
  46.     {WM_PAINT,      MsgBrushPaint},
  47.     {WM_INITDIALOG, MsgBrushInit}
  48. };
  49.  
  50. MSDI msdiBrush =
  51. {
  52.     sizeof(rgmsdBrush) / sizeof(MSD),
  53.     rgmsdBrush,
  54.     edwpNone
  55. };
  56.  
  57. // Brush dialog command table definition.
  58. CMD rgcmdBrush[] =
  59. {
  60.     {IDD_SOLIDBRUSH,    CmdBrushStyle}, // Brush Style notifications
  61.     {IDD_NULLBRUSH,     CmdBrushStyle},
  62.     {IDD_HATCHBRUSH,    CmdBrushStyle},
  63.     {IDD_HATCHSTYLE,    CmdBrushHatch}, // Hatch Style notification
  64.     {IDD_BRUSHCOLOR,    CmdBrushColor}, // Color button
  65.     {IDOK,              CmdBrushDone},  // OK and Cancel buttons
  66.     {IDCANCEL,          CmdBrushDone}
  67. };
  68.  
  69. CMDI cmdiBrush =
  70. {
  71.     sizeof(rgcmdBrush) / sizeof(CMD),
  72.     rgcmdBrush,
  73.     edwpNone
  74. };
  75.  
  76.  
  77. //
  78. //  FUNCTION: BrushDlg(HWND, UINT, WPARAM, LPARAM)
  79. //
  80. //  PURPOSE:  Processes messages for "Brush Style" dialog box.
  81. //
  82. //  PARAMETERS:
  83. //    hdlg - window handle of the dialog box
  84. //    wMessage - type of message
  85. //    wparam - message-specific information
  86. //    lparam - message-specific information
  87. //
  88. //  RETURN VALUE:
  89. //    TRUE - message handled
  90. //    FALSE - message not handled
  91. //
  92. //  COMMENTS:
  93. //
  94. //
  95.  
  96. LRESULT CALLBACK BrushDlg(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  97. {
  98.     return DispMessage(&msdiBrush, hdlg, uMessage, wparam, lparam);
  99. }
  100.  
  101.  
  102. //
  103. //  FUNCTION: MsgBrushCommand(HWND, UINT, WPARAM, LPARAM)
  104. //
  105. //  PURPOSE: Process WM_COMMAND messages sent to the Brush dialog.
  106. //
  107. //  PARAMETERS:
  108. //    hwnd      - The window handing the message.
  109. //    uMessage  - The message number. (unused).
  110. //    wparam    - Message specific data (unused).
  111. //    lparam    - Message specific data (unused).
  112. //
  113. //  RETURN VALUE:
  114. //    Always returns 0 - message handled.
  115. //
  116. //  COMMENTS:
  117. //    Uses this DipsCommand function defined in wndproc.c combined
  118. //    with the cmdiBrush structure defined in this file to handle
  119. //    the command messages for the Brush dialog box.
  120. //
  121.  
  122. #pragma argsused
  123. LRESULT MsgBrushCommand(HWND   hwnd,
  124.                         UINT   uMessage, 
  125.                         WPARAM wparam, 
  126.                         LPARAM lparam)
  127. {
  128.     return DispCommand(&cmdiBrush, hwnd, wparam, lparam);
  129. }
  130.  
  131.  
  132. //
  133. //  FUNCTION: MsgBrushInit(HWND, UINT, WPARAM, LPARAM)
  134. //
  135. //  PURPOSE: To initialize the Brush dialog with info from lparam.
  136. //
  137. //  PARAMETERS:
  138. //    hwnd - The window handing the message.
  139. //    uMessage - The message number. (unused).
  140. //    wparam - Message specific data (unused).
  141. //    lparam - points to LOGBRUSH structure.
  142. //
  143. //  RETURN VALUE:
  144. //    Always returns TRUE
  145. //
  146. //  COMMENTS:
  147. //    Sets the initial state of the controls according to the LOGBRUSH
  148. //    passed in via lparam.
  149. //
  150.  
  151. #pragma argsused
  152. LRESULT MsgBrushInit(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  153. {
  154.     int i, nIndex, nSel;
  155.     HWND hctlHatch;
  156.     char szTmp[32];
  157.     LPLOGBRUSH lpLB;
  158.  
  159.     // lparam is a pointer to a LOGBRUSH structure
  160.     lpLB = (LPLOGBRUSH)lparam;
  161.  
  162.     // Save pointer to LOGBRUSH structure in window bytes
  163.      SetWindowLong(hdlg, DWL_USER, (LONG)lpLB);
  164.  
  165.     // Check the correct brush style button
  166.     CheckRadioButton(hdlg,
  167.                      IDD_BRUSHFIRST,
  168.                      IDD_BRUSHLAST,
  169.                      IDD_BRUSHSTYLE + lpLB->lbStyle);
  170.  
  171.     // Fill up the hatch style combobox.  The item data for each item is
  172.     // set to the hatch style value defined by Windows (e.g. HS_DIAGCROSS).
  173.  
  174.     hctlHatch = GetDlgItem(hdlg, IDD_HATCHSTYLE);
  175.     nSel = 0;
  176.  
  177.     for (i = IDD_HATCHFIRST; i <= IDD_HATCHLAST; i++)
  178.     {
  179.         LoadString(hInst, i, szTmp, sizeof(szTmp));
  180.         nIndex = SendMessage(hctlHatch, CB_ADDSTRING, 0, (LPARAM)(LPSTR)szTmp);
  181.         SendMessage(hctlHatch, CB_SETITEMDATA, nIndex, i - IDD_HATCHSTYLE);
  182.  
  183.         // If this item is the current style, remember it
  184.         if (i == IDD_HATCHSTYLE + (int)lpLB->lbHatch)
  185.             nSel = nIndex;
  186.     }
  187.  
  188.     // Set the initial hatch selection
  189.      SendMessage(hctlHatch, CB_SETCURSEL, nSel, 0L);
  190.  
  191.     // Center the dialog over the application window
  192.     CenterWindow(hdlg, GetWindow(hdlg, GW_OWNER));
  193.  
  194.     // Get coordinates of the example window in the dialog
  195.     GetWindowRect(GetDlgItem(hdlg, IDD_BRUSHEXAMPLE), &rcExample);
  196.     ScreenToClient(hdlg, (LPPOINT)&rcExample);
  197.     ScreenToClient(hdlg, ((LPPOINT)&rcExample) + 1);
  198.  
  199.     // Reduce rect slightly so we don't paint over its frame
  200.     InflateRect(&rcExample, -1, -1);
  201.  
  202.      return 1;
  203. }
  204.  
  205.  
  206. //
  207. //  FUNCTION: MsgBrushPaint(HWND, UINT, WPARAM, LPARAM)
  208. //
  209. //  PURPOSE: Paint the example window with the current brush style
  210. //
  211. //  PARAMETERS:
  212. //    hwnd - The window handling the message.
  213. //    uMessage - The message number. (unused).
  214. //    wparam - Message specific data (unused).
  215. //    lparam - Message specific data (unused).
  216. //
  217. //  RETURN VALUE:
  218. //    Always returns TRUE
  219. //
  220. //  COMMENTS:
  221. //
  222.  
  223. #pragma argsused
  224. LRESULT MsgBrushPaint(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  225. {
  226.      PAINTSTRUCT ps;
  227.      LPLOGBRUSH lpLB;
  228.      HBRUSH hbr;
  229.  
  230.      BeginPaint(hdlg, &ps);
  231.  
  232.     // Get pointer to LOGBRUSH from window bytes
  233.     lpLB = (LPLOGBRUSH)GetWindowLong(hdlg, DWL_USER);
  234.  
  235.     // Create brush from current LOGBRUSH
  236.     hbr = CreateBrushIndirect(lpLB);
  237.  
  238.     // Paint the example window with the brush.
  239.     FillRect(ps.hdc, &rcExample, hbr);
  240.  
  241.     // Don't need the brush any more
  242.     DeleteObject(hbr);
  243.  
  244.      EndPaint(hdlg, &ps);
  245.     return 0;
  246. }
  247.  
  248.  
  249. //
  250. //  FUNCTION: CmdBrushStyle(HWND, WORD, WORD, HWND)
  251. //
  252. //  PURPOSE: Keeps track of which style button is selected.
  253. //
  254. //  PARAMETERS:
  255. //    hwnd      - The window handling the command.
  256. //    wCommand  - Child control ID.
  257. //    wNotify   - Child notification code (unused).
  258. //    hwndCtrl  - NULL (unused).
  259. //
  260. //  RETURN VALUE:
  261. //    Always returns TRUE.
  262. //
  263. //  COMMENTS:
  264. //
  265.  
  266. #pragma argsused
  267. LRESULT CmdBrushStyle(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  268. {
  269.     // Get pointer to LOGBRUSH from window bytes
  270.     LPLOGBRUSH lpLB = (LPLOGBRUSH)GetWindowLong(hdlg, DWL_USER);
  271.  
  272.     // Save the style of the button that was just clicked
  273.     lpLB->lbStyle = wCommand - IDD_BRUSHSTYLE;
  274.  
  275.     // Repaint the example window
  276.      InvalidateRect(hdlg, &rcExample, TRUE);
  277.  
  278.     return TRUE;
  279. }
  280.  
  281.  
  282. //
  283. //  FUNCTION: CmdBrushHatch(HWND, WORD, WORD, HWND)
  284. //
  285. //  PURPOSE: Keeps track of which hatch style is selected.
  286. //
  287. //  PARAMETERS:
  288. //    hwnd      - The window handling the command.
  289. //    wCommand  - Child control ID (unused).
  290. //    wNotify   - Child notification code.
  291. //    hwndCtrl  - Handle to the Hatch Style combobox.
  292. //
  293. //  RETURN VALUE:
  294. //    Always returns TRUE.
  295. //
  296. //  COMMENTS:
  297. //
  298.  
  299. #pragma argsused
  300. LRESULT CmdBrushHatch(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  301. {
  302.     LPLOGBRUSH lpLB;
  303.     int nSel;
  304.  
  305.     // Update the hatch style selection if necessary.
  306.  
  307.     if (CBN_SELCHANGE == wNotify)
  308.     {
  309.         // Get pointer to LOGBRUSH from window bytes
  310.         lpLB = (LPLOGBRUSH)GetWindowLong(hdlg, DWL_USER);
  311.  
  312.         nSel = SendMessage(hwndCtrl, CB_GETCURSEL, 0, 0L);
  313.         if (CB_ERR != nSel)
  314.           {
  315.             // Set brush style to hatched
  316.             lpLB->lbStyle = BS_HATCHED;
  317.             CheckRadioButton(hdlg,
  318.                              IDD_BRUSHFIRST,
  319.                              IDD_BRUSHLAST,
  320.                              IDD_HATCHBRUSH);
  321.  
  322.             // Save new hatch style
  323.             lpLB->lbHatch = SendMessage(hwndCtrl, CB_GETITEMDATA, nSel, 0L);
  324.  
  325.             // Repaint the example window
  326.             InvalidateRect(hdlg, &rcExample, TRUE);
  327.           }
  328.     }
  329.  
  330.     return TRUE;
  331. }
  332.  
  333.  
  334. //
  335. //  FUNCTION: CmdBrushColor(HWND, WORD, WORD, HWND)
  336. //
  337. //  PURPOSE: Puts up ChooseColor dialog to choose brush color.
  338. //
  339. //  PARAMETERS:
  340. //    hwnd      - The window handling the command.
  341. //    wCommand  - IDD_BRUSHCOLOR (unused).
  342. //    wNotify   - Child notification code (unused).
  343. //    hwndCtrl  - NULL (unused).
  344. //
  345. //  RETURN VALUE:
  346. //    Always returns TRUE.
  347. //
  348. //  COMMENTS:
  349. //
  350.  
  351. #pragma argsused
  352. LRESULT CmdBrushColor(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  353. {
  354.     LPLOGBRUSH lpLB;
  355.     CHOOSECOLOR cc;
  356.     static DWORD dwCustColors[16];
  357.  
  358.     // Get pointer to LOGBRUSH from window bytes
  359.     lpLB = (LPLOGBRUSH)GetWindowLong(hdlg, DWL_USER);
  360.  
  361.     // Initialize CHOOSECOLOR struct
  362.     cc.lStructSize      = sizeof(cc);
  363.     cc.hwndOwner        = hdlg;
  364.      cc.hInstance        = NULL;
  365.     cc.rgbResult        = lpLB->lbColor;
  366.     cc.lpCustColors     = dwCustColors;
  367.     cc.Flags            = CC_RGBINIT;
  368.     cc.lCustData        = 0;
  369.     cc.lpfnHook         = NULL;
  370.     cc.lpTemplateName   = NULL;
  371.  
  372.     if (ChooseColor(&cc))
  373.     {
  374.         // Save new color
  375.         lpLB->lbColor = cc.rgbResult;
  376.  
  377.           // Repaint the example window
  378.         InvalidateRect(hdlg, &rcExample, TRUE);
  379.     }
  380.  
  381.     return TRUE;
  382. }
  383.  
  384.  
  385. //
  386. //  FUNCTION: CmdBrushDone(HWND, WORD, HWND)
  387. //
  388. //  PURPOSE: Free the Brush dialog and related data.
  389. //
  390. //  PARAMETERS:
  391. //    hwnd      - The window handling the command.
  392. //    wCommand  - The command to be handled.
  393. //    hwndCtrl  - NULL (unused).
  394. //
  395. //  RETURN VALUE:
  396. //    Always returns TRUE.
  397. //
  398. //  COMMENTS:
  399. //    Calls EndDialog to finish the dialog session.
  400. //
  401.  
  402. #pragma argsused
  403. LRESULT CmdBrushDone(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  404. {
  405.     // Exit the dialog
  406.     EndDialog(hdlg, (IDOK == wCommand));
  407.  
  408.     return TRUE;
  409. }
  410.