Retrieving the Text Color from the Font Common Dialog Box

Last reviewed: November 2, 1995
Article ID: Q86331
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

The common dialog box library (COMMDLG.DLL) provides the ChooseFont() routine that provides a common interface (Font dialog box) to specify the attributes for a font in an application. When the user chooses the Apply button in the Font dialog box, the application can format selected text using the specified font. This button is also useful in an application that allows the user to select more than one font simultaneously.

The Font dialog box provides a common method to specify a number of font attributes, including color. An application can send the CB_GETITEMDATA message to the Color combo box to retrieve the currently selected color.

This article discusses the procedure required to obtain all the information about the currently specified font.

MORE INFORMATION

The Font dialog box includes an Apply button if the application includes the CF_APPLY value in the specification for the Flags member of the CHOOSEFONT data structure. The dialog box includes the Color combo box if the CF_EFFECTS value is also specified. The remainder of this article assumes that the application has specified both of these values.

To properly process input from the Apply button, an application must install a hook function. For more information on installing a hook function from an application, query on the following words in the Microsoft Knowledge Base:

   steps adding hook function

The following code illustrates one method to process input from the Apply button:

   case WM_COMMAND:
      switch (wParam)
      {
         case psh3:  // The Apply button

         // Retrieve the font information...
            SendMessage(hDlg, WM_CHOOSEFONT_GETLOGFONT, 0,
                       (LONG)(LPLOGFONT)&lfLogFont);

         // Perform any required processing
         // (create the specified font, for example)

         // Retrieve color information...
            iIndex = (int)SendDlgItemMessage(hDlg, cmb4,
                                          CB_GETCURSEL, 0, 0L);
            if (iIndex != CB_ERR)
            {
               dwRGB = SendDlgItemMessage(hDlg, cmb4, CB_GETITEMDATA,
                                       (WORD)iIndex, 0L);
               wRed = GetRValue(dwRGB);
               wGreen = GetGValue(dwRGB);
               wBlue = GetBValue(dwRGB);

               wsprintf(szBuffer, "RGB Value is %u %u %u\r\n", wRed,
                        wGreen, wBlue);

               OutputDebugString(szBuffer);
            }
            break;

         default:
            break;
      }
      break;

The color information is not required to create the font; however, this information is required to accurately display the font according to the user's specification.

In an application that does not use the Apply button, the rgbColors member of the CHOOSEFONT data structure contains the selected color. In this case, no special processing to retrieve the color is required.


Additional reference words: 3.10 3.50 3.51 4.00 95
KBCategory: kbui
KBSubcategory: UsrCmnDlg


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.