How to: Create a Soft Key Bar

Code Example 1

Code Example 2

To create a soft key bar and soft keys, you create a resource file and specify the Windows CE SHMENUBARINFO structure and the SHCreateMenuBar function during the handling of the Windows CE WM_INITDIALOG message.

To create a soft key bar and soft keys

  1. Create a resource file by doing the following:

    1. Identify any menu that is displayed by a soft key. In the following example, the IDR_MAIN_MENU menu is displayed and contains the DONE command.

      // MENU 
      IDR_MAIN_MENU MENU 
      BEGIN
      :
      :
          // SUBMENU 2. This submenu is displayed by the right soft key.
          POPUP ""
          BEGIN
              MENUITEM "Done"     IDM_CARDVIEW_SK2_DONE
          END  
      END
      
    2. Indicate the text that appears on the soft key labels, and assign a string resource ID to each text string. In the following example, the text strings are "Edit" and "Menu" and the string resource IDs are IDS_CARDVIEW_EDIT and IDS_CARDVIEW_MENU.

      // STRING TABLE
      
      STRINGTABLE DISCARDABLEBEGIN
      :
          IDS_CARDVIEW_EDIT  "Edit"
          IDS_CARDVIEW_MENU  "Menu"  
      :
      END
      
    3. Indicate the display and functionality of the soft key bar and the soft keys, as shown in the following example.

      // Card View Soft key bar
      IDR_CARDVIEW_MENUBAR RCDATA
      BEGIN
          IDR_MAIN_MENU,
          2,
          I_IMAGENONE, IDM_CARDVIEW_SK1_EDIT, TBSTATE_ENABLED, 
              TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE, IDS_CARDVIEW_EDIT, 0, 
              NOMENU,
          I_IMAGENONE, IDM_CARDVIEW_SK2_MENU, TBSTATE_ENABLED, 
              TBSTYLE_DROPDOWN | TBSTYLE_AUTOSIZE, IDS_CARDVIEW_MENU, 0, 
              2,END
      

      The following table describes the above example.

      Value Description
      IDR_CARDVIEW_MENUBAR Resource ID for the soft key bar.
      IDR_MAIN_MENU A menu displayed by a soft key.
      2 The soft key bar contains two soft keys.
      I_IMAGENONE Indicates that the soft key label displays text instead of a bitmap. Windows Mobile-based Smartphones do not support bitmaps on the soft key bar.
      IDM_CARDVIEW_SK1_EDIT

      and

      IDM_CARDVIEW_SK2_MENU

      Resource IDs for the soft keys.
      TBSTATE_ENABLED Initial state of the button. For information about toolbar states supported by Windows CE, see Creating a Toolbar.
      TBSTYLE_BUTTON

      TBSTYLE_DROPDOWN

      TBSTYLE_AUTOSIZE

      Styles assigned to each soft key. For more information about toolbar styles supported by Windows CE, see Toolbar Styles.
      IDS_CARDVIEW_EDIT

      IDS_CARDVIEW_MENU

      String resource IDs for the text that appears on the soft key labels.
      NOMENU Indicates that the IDS_CARDVIEW_EDIT soft key does not display a menu.
      2 Submenu index of the menu displayed by the IDS_CARDVIEW_MENU soft key.
  2. Add the SHMENUBARINFO structure to the application source code. Specify the resource ID for the soft key bar by using the nToolBarID structure member. In the following example, the nToolBarID ** structure member specifies IDR_CARDVIEW_MENUBAR.

    SHMENUBARINFO mbi;
    
    memset(&mbi, 0, sizeof(SHMENUBARINFO));  // Reset mbi to 0.
    mbi.cbSize = sizeof(SHMENUBARINFO);
    mbi.hwndParent = hwnd;  // Soft key bar's owner.
    mbi.nToolBarId = IDR_CARDVIEW_MENUBAR;  // Soft key bar resource.
    mbi.hInstRes = g_hinst;  // HINST in which resource is located.
    
  3. Add the SHCreateMenuBar function to the application source code.

Example 1

////////////////////////////////////////////////////////////////////
// MENU 
IDR_MAIN_MENU MENU 
BEGIN
:
:
    // SUBMENU 2. This submenu is displayed by the right soft key
    POPUP ""
    BEGIN
        MENUITEM "Done"     IDM_CARDVIEW_SK2_DONE
    END  
END

////////////////////////////////////////////////////////////////////
// STRING TABLE

STRINGTABLE DISCARDABLEBEGIN
:
    IDS_CARDVIEW_EDIT  "Edit"
    IDS_CARDVIEW_MENU  "Menu"  
:
END

// Card View Soft key bar
IDR_CARDVIEW_MENUBAR RCDATA
BEGIN
    IDR_MAIN_MENU,
    2,
    I_IMAGENONE, IDM_CARDVIEW_SK1_EDIT, TBSTATE_ENABLED, 
        TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE, IDS_CARDVIEW_EDIT, 0, NOMENU,
    I_IMAGENONE, IDM_CARDVIEW_SK2_MENU, TBSTATE_ENABLED, 
        TBSTYLE_DROPDOWN | TBSTYLE_AUTOSIZE, IDS_CARDVIEW_MENU, 0, 2,
END

Example 2

BOOL rb;
int rc;
LRESULT lr;
SHMENUBARINFO mbi;

memset(&mbi, 0, sizeof(SHMENUBARINFO));  // Reset mbi to 0.
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hwnd;  // Soft key bar's owner.
mbi.nToolBarId = IDR_CARDVIEW_MENUBAR;  // Soft key bar resource.
mbi.hInstRes = g_hinst;  // HINST in which resource is located.

// Create the Soft key bar.
rb = SHCreateMenuBar(&mbi);
if (rb == FALSE)  // SHCreateMenuBar failed.
{
    rc = MessageBox(NULL, _T("Could not create soft key bar."),
                    _T("Error"), MB_OK);
    if (rc == 0)  // Not enough memory to create MessageBox.
        return E_OUTOFMEMORY;
    return E_FAIL;  // Replace with specific error handling.
}

See Also

How to: Obtain a Handle to a Soft Key Menu

Soft Key Overview

Last updated on Friday, April 22, 2005

© 2005 Microsoft Corporation. All rights reserved.

Send feedback on this topic to the authors.