Creating a List Box

A list box is a window that displays a list of character strings. A user selects a string from the list by tapping it with the stylus. When a string is selected, it is highlighted. List boxes are typically placed in dialog boxes, but they can also be placed in a normal window by specifying the LISTBOX window class in the lpClassName parameter of the CreateWindow or CreateWindowEx function.

    To create a list box control in a dialog box

The following screen shot shows a list box.

Because list boxes are empty by default, you must initialize or populate the list box with data when the dialog box containing the list box is displayed. Each time a dialog box is activated, the system sends your dialog box procedure a WM_INITDIALOG message. A dialog box procedure is responsible for initializing and monitoring its child windows, including any list boxes. The dialog box procedure communicates with the list box by sending messages to it and by processing the notification messages sent by the list box. To initialize the list box, modify your dialog box procedure by adding a case to the SWITCH statement in you dialog box procedure. The following code example shows how to do this.

#define IDC_FONTLIST  1000        // List control identifier

int iNumOfFonts = 0;              // The total number of fonts
int iCurrItem = -1;               // The index of newly selected or
                                  // added font name in the list box
BOOL CALLBACK FontListDlgProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, 
                               LPARAM lParam)
{
  int index = 1,
      iFontIndex;           // Font list index
  TCHAR szFontName[80],     // For the currently enumerated font name
        szFontNamePrev[80]; // For the previously enumerated font name
  HWND hwndFontListCtrl;    // Window handle to the font list box
  
  // Get the window handle to the font list box control in the dialog
  // box.
  hwndFontListCtrl = GetDlgItem (hwndDlg, IDC_FONTLIST);
        
  switch (uMsg)
  {
    case WM_INITDIALOG:
      // Need to find the total number of fonts, iNumOfFonts, prior to 
      // the following code.

      for (iFontIndex = 0; iFontIndex < iNumOfFonts; ++iFontIndex)
      {
        // Insert code here to retrieve the name of each font and then 
        // put it in szFontName.
        // ...

        // Add the font name to the list control.
        iCurrItem = SendMessage (hwndFontListCtrl, LB_ADDSTRING, 0, 
                                 (LPARAM)(LPCTSTR) szFontName);

        // Set a 32-bit value, (LPARAM) iFontIndex, associated with the
        // newly added item in the list control.
        SendMessage (hwndFontListCtrl, LB_SETITEMDATA,      
                     (WPARAM) iCurrItem, (LPARAM) iFontIndex);  
      }

      // Select the first font name in the list control.
      SendMessage (hwndFontListCtrl, LB_SETCURSEL, 0, 0);

      return TRUE;  

    case WM_COMMAND:
      switch (LOWORD (wParam))
      {
        case IDOK:
          // Retrieve the index of the currently selected font.
          if ((iCurrItem = SendMessage (hwndFontListCtrl, LB_GETCURSEL, 
                                        0, 0)) != LB_ERR)
          {
            iFontIndex = SendMessage (hwndFontListCtrl, LB_GETITEMDATA, 
                                      iCurrItem, 0);

            // Get the currently selected font from the index.
            CurrLogFont = g_lpEnumLogFont[iFontIndex].elfLogFont;

            EndDialog (hwndDlg, 0);
            return TRUE;
          }

        case IDCANCEL:
          iCurrItem = -1;
          EndDialog (hwndDlg, 0);
          return TRUE;
      }
      break;
  }
  return FALSE;
}