How to: Add Application-Level Commands to the New Button Menu

Your application can extend the New menu with items that apply only within your application while it is running. To do this, handle any WM_NOTIFY messages containing NMN_GETAPPREGKEY in the Code member of the NMHDR Windows CE control structure, which is passed as the lParam of the message. This notification is sent after a user taps the New menu, and enables your application to add menu items specific to your application.

The following code adds a My Item command to the New menu when it is going to be displayed.

PNMNEWMENU lpNew = NULL;
LPNMHDR lpn = (LPNMHDR)lParam;

if (lpn->code == NMN_GETAPPREGKEY) 
{
    lpNew = (PNMNEWMENU) lParam;
    if (!AppendMenu (lpNew->hMenu, MF_ENABLED, 
        IDM_MYITEM, TEXT("My Item")))
    {
        // AppendMenu failed.
        MessageBox(NULL, _T("Can't add item to New menu."),
                  _T("Warning"), MB_OK);
        exit(0);  // Replace with specific error handling.
    }
    // Always put a separator under the last application
    // level New menu item.
    if (!AppendMenu (lpNew->hMenu, MF_SEPARATOR, 0, 0))
    {
        // AppendMenu failed.
        MessageBox(NULL, _T("Can't add separator to menu."),
                  _T("Warning"), MB_OK);
        exit(0);  // Replace with specific error handling.
    }
} 
return 0;

In the header file for the application, include a definition for the menu item ID. The application-level IDs for the command on the New menu should always start at IDM_NEWMENUMAX + 1 (shown in the following code) and increment from there. This ensures that the application IDs do not collide with those already in use by the New menu.

#define IDM_MYITEM (IDM_NEWMENUMAX + 1) // Custom item

When the user taps My Item, the application receives a WM_COMMAND message with the ID as the low word (LOWORD) of wParam, just like any other menu command or toolbar button. The WM_COMMAND message is as follows:

case WM_COMMAND:
    switch (LOWORD(wParam)
    {
       case IDM_MYITEM:
       // Take action based on the My Item command on the New menu.

See Also

Creating a Global Command for the New Menu

Menu Bar Overview

Using Commands on the New Menu to Transfer Data Between Applications

Send feedback on this topic to the authors.

© 2005 Microsoft Corporation. All rights reserved.