September 24, 2018
Hot Topics:

Create Flexible Custom Menus in Windows CE

  • November 13, 2002
  • By Nancy Nicolaisen
  • Send Email »
  • More Articles »

In this final installment of our explorations of Windows CE menus, we're going to learn how to implement a familiar control, the Combo Box. We'll also explore the use of a novel CE menu construct: the Dropdown button. Dropdown buttons are powerful tools, because they use very little menu real estate, but allow you great latitude in creating application specific functionality.

Adding Combo Boxes

Compared to buttons, adding a combo box to a command bar menu is a snap. There is really only one sticky point, and it's something that is sinuously woven throughout Windows CE: Unicode. More than any other single thing, Unicode will likely be the memorable porting gotcha of moving your code from Win32 to Win CE.

You can read more about exactly what Unicode is and why its such an integral design feature of Windows CE in the sidebar at the end of this article. Here are a few important Unicode rules to keep in mind when porting the user interface of your Win 32 application to Windows CE.

  • Always use the TEXT( ) macro when passing string literals as function arguments

  • Never assume a reference to a Unicode string is the same as the starting address for the string. When you need an address, use the & operator

  • Don't assume that a count of characters in a string is the same as the size of the buffer needed to hold the characters. Always multiply the character count ( plus a terminal null, if one is necessary ) by either sizeof(TCHAR) or sizeof(wchar_t) to calculate storage space.

  • Always use the string handling functions that are matched to the declared type of the strings you are manipulating.

Here's the code to add a combo box to a combo bar:

HWND    AddCombo( HWND hWnd )
    int i;
    TCHAR tszBuff[24];

    //we have to destroy the old one before expanding
    CommandBar_Destroy( hwndCB ); 
    hwndCB = CommandBar_Create(hInst, hWnd, 1);     

     // Insert the menu band
    CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
    // Add a combo box between the view icons and the sort icons.

    CommandBar_InsertComboBox (hwndCB, hInst, 
                     GetSystemMetrics(SM_CXSCREEN ) / 4,
                     CBS_DROPDOWNLIST | WS_VSCROLL,
                     IDC_COMBO_BUTTON, 1);
    // Fill in combo box.
    for (i = 0; i < 5; i++)
      wsprintf (tszBuff, TEXT ("Combo List %d"), i);
      SendDlgItemMessage (hwndCB, IDC_COMBO_BUTTON, 
                          CB_INSERTSTRING, -1, (LPARAM)tszBuff);
    //select list item 0
    SendDlgItemMessage (hwndCB, 
                        0, 0);
   // AddAdornments puts an exit button on the command 
   // band control. It replaces the "Exit" menu item
    CommandBar_AddAdornments (hwndCB, 0, 0);
    CommandBar_Show(hwndCB, TRUE);
    return hwndCB;

Let's look at the combo box code in a bit more detail.

    CommandBar_InsertComboBox (hwndCB, hInst, 
                     GetSystemMetrics(SM_CXSCREEN ) / 4,
                     CBS_DROPDOWNLIST | WS_VSCROLL,
                     IDC_COMBO_BUTTON, 1);

The CommandBar_InsertComboBox() parameters, in the order shown, are the command bar handle, the instance handle, the width of the box in device units( pixels), the style flags, the combo box's control ID, and the 0 based index of the button to its right.

Notice that the width of the combo is calculated using a call to GetSystemMetrics(). There is no "standard" screen size across Windows CE devices, so this the safe and portable way to specify size for visual elements like controls and graphics.

The next parameter is the style flags. Here we begin to see some of the "shrinkage" of the Win 32 API. Windows CE supports only two combo styles: CBS_DROPDOWN or CBS_DROPDOWNLIST. The familiar CBS_SIMPLE style, where the list box is always visible and the current selection is displayed in the edit control is not supported under Win CE. This style was eliminated because it uses too much screen real estate.

The CBS_DROPDOWN and CBS_DROPDOWNLIST styles show something that looks like a single line text edit control, with a dropdown arrow icon displayed next to it. The list box is displayed when the user taps the dropdown arrow control. The CBS_DROPDOWNLIST style has a static text field that always displays the current selection and only allows users to select from a predefined list of items. CBS_DROPDOWN has an edit control and allows users to select from the list or provide their own input.

You can combine the combo style with other window style flags as we have done in the call shown above, but again, under Windows CE there is a limited subset of these style flags. A few particularly useful flag appear in the list below.

Table 1- My Favorite Combo Control Style Flags

Style Description
CBS_AUTOHSCROLL Horizontally scrolls text as a user types. Without this flag, a user can only enter as much text as the control width accommodates.
CBS_DROPDOWN Displays only an edit control unless the user taps the dropdown arrow next to the edit control. Users can provide their own input or choose from the list.
CBS_DROPDOWNLIST Displays only the current list selection unless the user taps the dropdown arrow next to the edit control.
CBS_LOWERCASE Converts all input to lower case lowercase.
CBS_UPPERCASE Converts all input to upper case.

Finally, we insert strings into the combo like this:

    TCHAR tszBuff[24];
    for (i = 0; i < 5; i++)
      wsprintf (tszBuff, TEXT ("Combo List %d"), i);
      SendDlgItemMessage (hwndCB, IDC_COMBO_BUTTON, 
                          CB_INSERTSTRING, -1, (LPARAM)tszBuff);

Notice the function wsprintf(), which looks a lot like the familiar sprintf(),but with a couple of important differences. Note that the first parameter, tszBuff, is of type TCHAR.

TCHAR resolves to a WCHAR type if Unicode is defined in the Project build settings; otherwise it resolves to CHAR.

I make a special point of highlighting this, because as I mentioned earlier, Unicode is one of the most difficult and ubiquitous porting issues you will encounter. Using the TCHAR type is about the best insurance you'll find against memory overwrite errors when copying strings. The second parameter is also a key tool for working with Unicode strings. TEXT("Combo List %d") converts what is between the double quotes to a Unicode string. Like with ASCII character literals, you use the backslash to precede escape characters like tabs and newlines, and a double backslash to create a string that includes a backslash.

To create Unicode literal strings suitable for list box text, use the TEXT() macro, with the string literal enclosed in double quotes as a parameter.

   TEXT("This is how to make a Unicode string literal")

Page 1 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date