HOWTO: Retrieve the Text Color from the Font Common Dialog Box

ID: Q86331

The information in this article applies to:

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 query words:

Keywords          : kbCmnDlg kbCmnDlgFont kbGrpUser kbWinOS310 kbWinOS300 
Issue type        : kbhowto

Last Reviewed: December 26, 1998