次の方法で共有


Using Menu Creation Functions (Windows Embedded CE 6.0)

1/6/2010

You use menu creation functions when you want to create or change a menu at run time. To create an empty menu bar, use the CreateMenu function; to create an empty menu, use the CreatePopupMenu function. To add items to a menu, use the AppendMenu and InsertMenu functions.

The following code example shows how to create an empty menu by using CreatePopupMenu, and how to add items to the menu by using AppendMenu.

HMENU hmTrackPopup;    // The popup menu to track.

// Create the popup menu.
hmTrackPopup = CreatePopupMenu();

// Append some items.
AppendMenu(hmTrackPopup,MF_STRING, 1000,TEXT("Append Item 1"));
AppendMenu(hmTrackPopup,MF_STRING, 1001, TEXT("Append Item 2"));

When a menu contains more items than will fit in one column, the menu is truncated unless you force a line break. You can cause a column break to occur at a specific item in a menu by assigning the MFT_MENUBREAK menu item type flag to the item. Windows Embedded CE places that item and all subsequent items in a new column. You can also assign the MFT_MENUBARBREAK menu item type flag to the item, which has the same effect as the MFT_MENUBREAK menu item type flag except that a vertical line appears between the new and existing columns.

To display a shortcut menu, use the TrackPopupMenuEx function. Shortcut menus, also called floating pop-up menus or context menus, are typically displayed when the WM_CONTEXTMENU message is processed. The older TrackPopupMenu function is supported, but new applications should use the TrackPopupMenuEx function.

The following code example shows how to display a shortcut menu by using TrackPopupMenuEx.

case WM_LBUTTONDOWN:
  {
    WORD xPos = LOWORD(lParam);  // horizontal position of the cursor 
    WORD yPos = HIWORD(lParam);  // vertical position of the cursor 

    if (fTrack && hmTrackPopup){
      TrackPopupMenuEx(hmTrackPopup,0, xPos, yPos, hWnd, NULL);
    }
  }
  break;

If a menu is assigned to a window and that window is destroyed, Windows Embedded CE automatically destroys the menu, freeing the menu handle and the memory occupied by the menu. Windows Embedded CE does not automatically destroy a menu that is not assigned to a window. An application must destroy the unassigned menu by calling the DestroyMenu function.

The following code example shows how to destroy a menu by using the DestroyMenu function.

// Ensure that the old menu is gone.
DestroyMenu(hmTrackPopup);
hmTrackPopup = NULL;

See Also

Concepts

Creating Menus
Using Resources

Other Resources

GWES Application Development