HOWTO: Add Point Sizes to the ChooseFont() Common Dialog Box

ID: Q99668


The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK)
  • Microsoft Windows Software Development Kit (SDK) 3.1
  • Microsoft Windows 2000


SUMMARY

When a TrueType font (or any other scalable font) is selected in the ChooseFont() common dialog box, a list of reasonable point sizes is displayed for selection. In some cases it is necessary to change this list to allow fewer or more selections.

The initial list of point sizes is hard-coded into COMMDLG.DLL but can be changed programmatically using a common dialog box hook function.


MORE INFORMATION

Most scalable fonts can be created at nearly any point size. Some TrueType fonts can be sized from 4 points to 999 points. A complete list of available point sizes for a font of this type would contain about 1000 elements, which can be prohibitively long and time-consuming to construct.

The ChooseFont() common dialog box attempts to limit the selection by listing only a few of the available point sizes in the size selection combo box.

In certain cases, it may be desirable to offer more point-size selections in this dialog box. In this case, a common dialog box hook procedure can be used to insert point sizes when a specific font is selected.

The following steps describe a technique that will allow new point sizes to be added to the size selection combo box. Pay special attention to step number 4:

  1. In your common dialog box hook procedure, look for WM_COMMAND messages with wParam equal to the font name combo box (which is "cmb1" for Windows version 3.1).


  2. When you get this message(s), check for the font name you are looking for (for example, you can compare the current selection to "Courier New"). If it's not the font you want, return.


  3. If this is the font you want, post yourself a user-defined message. In response to this message, add the new point size to the Point Size combo box (which is "cmb3" for Windows 3.1). It's a good idea to double-check here that the point size you are adding isn't already in the combo box (so you don't get duplicates).


  4. Once you add the new point size, set the item data for the new item equal to the point size you are adding. For example, if you are adding the string "15" to the combo box, you need to set the item data of this new item to 15.


The following code fragment demonstrates the above steps:

   // Common Dialog ChooseFont() hook procedure.

   UINT CALLBACK __export FontHook(HWND hwnd, UINT wm, WPARAM wParam,
   LPARAM lParam)
   {
      char szBuf[150];
      DWORD dwIndex;

      switch(wm)
      {
         case WM_COMMAND:
         // See if the notification is for the "Font name" combo box.
            if (wParam == cmb1)
            {
               switch (HIWORD(lParam))
               {
                  case CBN_SETFOCUS:
                  case CBN_KILLFOCUS:
                     break;
                  default:
                  // Check to see if it is the font you are looking for.
                     dwIndex = SendDlgItemMessage(hwnd, cmb1, CB_GETCURSEL,
                                                  0, 0);
                     if (dwIndex != CB_ERR)
                        SendDlgItemMessage(hwnd, cmb1, CB_GETLBTEXT,
                                           (WPARAM) dwIndex, (LPARAM)
                                           ((LPSTR) szBuf));

                  // Compare list box contents to the font you are looking
                  // for. In this case, it is "Courier New".
                     if (strcmp(szBuf, "Courier New") == 0)
                     // It is the font you want. Post a message.
                        PostMessage(hwnd, WM_ADDNEWPOINTSIZES, 0, 0L);
               }
            }
         break;

         case WM_ADDNEWPOINTSIZES:
         // First look to see if you have already added point sizes to this
         // combo box.
            if (SendDlgItemMessage(hwnd, cmb3, CB_FINDSTRING, -1,
                                  (LPARAM)(LPCSTR)"6") == CB_ERR)
            {
            // Not found, add new point size.
               dwIndex = SendDlgItemMessage(hwnd, cmb3, CB_INSERTSTRING,
                                            0, (LPARAM)(LPSTR)"6");

            // Also set the item data equal to the point size.
               SendDlgItemMessage(hwnd, cmb3, CB_SETITEMDATA,
                                  (WPARAM)dwIndex, 6);
            }
            return TRUE; // Don't pass this message on.
      }
      return FALSE;
   } 


REFERENCES

Additional query words:

Keywords : kbCmnDlg kbCmnDlgFont kbNTOS kbWinOS2000 kbSDKWin32 kbGrpUser kbWinOS
Version : WINDOWS:3.1
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: January 25, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.