BOOL LoadMenuIndirect( const void* lpMenuTemplate );
Return Value
Nonzero if the menu resource was loaded successfully; otherwise 0.
Parameters
lpMenuTemplate
Points to a menu template (which is a single MENUITEMTEMPLATEHEADER structure and a collection of one or more MENUITEMTEMPLATE structures). For more information on these two structures, see the Win32 SDK documentation.
Remarks
Loads a resource from a menu template in memory and attaches it to the CMenu object. A menu template is a header followed by a collection of one or more MENUITEMTEMPLATE structures, each of which may contain one or more menu items and pop-up menus.
The version number should be 0.
The mtOption flags should include MF_END for the last item in a pop-up list and for the last item in the main list. See the AppendMenu member function for other flags. The mtId member must be omitted from the MENUITEMTEMPLATE structure when MF_POPUP is specified in mtOption.
The space allocated for the MENUITEMTEMPLATE structure must be large enough for mtString to contain the name of the menu item as a null-terminated string.
Before exiting, an application must free system resources associated with a menu if the menu is not assigned to a window. An application frees a menu by calling the DestroyMenu member function.
Example
// CMainFrame::OnLoadMenuIndirect() is a menu command handler for
// CMainFrame class, which in turn is a CFrameWnd-derived class. It
// shows how to use LoadMenuIndirect() to load a resource from a
// menu template in memory.
void CMainFrame::OnLoadMenuIndirect()
{
// For simplicity, allocate 500 bytes from stack. May use
// GlobalAlloc() to allocate memory bytes from heap.
BYTE milist[500];
memset(milist, 0, 500);
// Fill up the MENUITEMTEMPLATEHEADER structure.
MENUITEMTEMPLATEHEADER* mheader = (MENUITEMTEMPLATEHEADER*) milist;
mheader->versionNumber = 0;
mheader->offset = 0;
int bytes_used = sizeof(MENUITEMTEMPLATEHEADER);
// Add the following menu items to menu bar:
// File Edit
// Exit Copy
// Paste
bytes_used += AddMenuItem(milist + bytes_used, L"&File", 0, TRUE, FALSE);
bytes_used += AddMenuItem(milist + bytes_used, L"E&xit", ID_APP_EXIT, FALSE, TRUE);
bytes_used += AddMenuItem(milist + bytes_used, L"&Edit", 0, TRUE, TRUE);
bytes_used += AddMenuItem(milist + bytes_used, L"&Copy", ID_EDIT_COPY, FALSE, FALSE);
bytes_used += AddMenuItem(milist + bytes_used, L"&Paste", ID_EDIT_PASTE, FALSE, TRUE);
// Load resource from a menu template in memory.
ASSERT(m_NewMenu.LoadMenuIndirect(milist));
// Remove and destroy old menu
SetMenu(NULL);
::DestroyMenu(m_hMenuDefault);
// Add new menu.
SetMenu(&m_NewMenu);
// Assign default menu
m_hMenuDefault = m_NewMenu.m_hMenu;
}
// This is a helper function for adding a menu item (either a popup
// or command item) to the specified menu template.
//
// MenuTemplate - pointer to a menu template
// MenuString - string for the menu item to be added
// MenuID - id for the command item. Its value is ignored if
IsPopup is TRUE.
// IsPopup - TRUE for popup menu (or submenu); FALSE for command
item
// LastItem - TRUE if MenuString is the last item for the popup;
FALSE otherwise.
UINT AddMenuItem(LPVOID MenuTemplate, WCHAR* MenuString, WORD MenuID, BOOL IsPopup, BOOL LastItem)
{
MENUITEMTEMPLATE* mitem = (MENUITEMTEMPLATE*) MenuTemplate;
UINT bytes_used = 0;
if (IsPopup) // for popup menu
{
if (LastItem)
mitem->mtOption = MF_POPUP | MF_END;
else
mitem->mtOption = MF_POPUP;
bytes_used += sizeof (mitem->mtOption);
mitem = (MENUITEMTEMPLATE*) ((BYTE*) MenuTemplate + bytes_used);
// a popup doesn't have mtID!!!
wcscpy((WCHAR*) mitem, MenuString);
bytes_used += sizeof (WCHAR) * (wcslen(MenuString) + 1); // include '\0'
}
else // for command item
{
mitem->mtOption = LastItem ? MF_END : 0;
mitem->mtID = MenuID;
wcscpy(mitem->mtString, MenuString);
bytes_used += sizeof (mitem->mtOption ) + sizeof (mitem->mtID) +
sizeof (WCHAR) * (wcslen(MenuString) + 1); // include '\0'
}
return bytes_used;
}
CMenu Overview | Class Members | Hierarchy Chart
See Also CMenu::DestroyMenu, CMenu::LoadMenu, ::LoadMenuIndirect, CMenu::AppendMenu