How to Create a Soft Key Bar

4/8/2010

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

Note

Beginning with Windows Mobile 6.5.3, soft keys are replaced by touchable tiles on Windows Mobile Professional phones.

To Create a Soft Key Bar and Soft Keys

  1. Create a resource file by doing the following:

    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
    

    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
    

    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
    
  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

////////////////////////////////////////////////////////////////////
// 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

Tasks

How to Create a Soft Key Bar

Other Resources

Soft Key and Menu Guidelines