CMenu::CreateMenu
建立功能表並將它附加至 CMenu 物件。
BOOL CreateMenu( );
傳回值
如果不是零,功能表成功建立,則為 0。
備註
功能表一開始是空的。您可以使用 AppendMenu 或 InsertMenu 成員函式,功能表項目可加入。
如果功能表指派給視窗,自動終結此物件終結時, 視窗。
在結束之前,應用程式必須釋放系統資源與功能表,如果功能表未指派給視窗。應用程式會呼叫 DestroyMenu 成員函式來釋放的功能表。
範例
// The code fragment below shows how to create a new menu for the
// application window using CreateMenu() and CreatePopupMenu().
// Then, the created menu will replace the current menu of the
// application. The old menu will be destroyed with DestroyMenu().
// NOTE: The code fragment below is done in a CFrameWnd-derived class.
// Create a new menu for the application window.
VERIFY(m_NewMenu.CreateMenu());
// Create a "File" popup menu and insert this popup menu to the
// new menu of the application window. The "File" menu has only
// one menu item, i.e. "Exit".
VERIFY(m_FileMenu.CreatePopupMenu());
m_FileMenu.AppendMenu(MF_STRING, ID_APP_EXIT, _T("E&xit"));
m_NewMenu.AppendMenu(MF_POPUP, (UINT_PTR)m_FileMenu.m_hMenu, _T("&File"));
// Remove and destroy old menu
SetMenu(NULL);
CMenu* old_menu = CMenu::FromHandle(m_hMenuDefault);
old_menu->DestroyMenu();
// Add new menu.
SetMenu(&m_NewMenu);
// Assign default menu
m_hMenuDefault = m_NewMenu.m_hMenu;
需求
Header: afxwin.h