Adding Point Sizes to the ChooseFont() Common Dialog Box

Last reviewed: November 2, 1995
Article ID: Q99668
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

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's the font we're 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 we are looking for.
               // In this case, it's "Courier New".
                  if (strcmp(szBuf, "Courier New") == 0)
                  // It's the font we want. Post ourselves a message.
                     PostMessage(hwnd, WM_ADDNEWPOINTSIZES, 0, 0L);
            }
         }
      break;

      case WM_ADDNEWPOINTSIZES:
      // First look to see if we've 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;
}


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.