MFC Menu
创建menu
- 创建菜单资源
-
定义CFrameWnd的成员变量
Menu menu;
-
在CMyFrameWnd::OnCreate中加载菜单
menu.LoadMenuW(IDR_MENU1); this->SetMenu(&menu);
菜单消息
//ON_COMMAND(菜单项ID,消息处理函数名)
ON_COMMAND(ID_FILE_NEW,OnFileNew)
afx_msg void OnFileNew();
void CMyFrameWnd::OnFileNew()
{
AfxMessageBox(_T("Hello Menu"));
}
命令消息的处理顺序 ON_COMMAND
框架类 -> 应用程序类,只有当框架类中没有命令消息处理顺序,才会去应用程序类中找。
设置菜单选中状态
ON_WM_INITMENUPOPUP()
void CMyFrameWnd::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
CFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
// TODO: Add your message handler code here
pPopupMenu->CheckMenuItem(ID_FILE_NEW,MF_CHECKED);
}
右击菜单
ON_WM_CONTEXTMENU()
void CMyFrameWnd::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
//HMENU hPopup=::GetSubMenu(menu.m_hMenu, 0);
//::TrackPopupMenu(hPopup, TPM_LEFTALIGN | TPM_TOPALIGN, point.x, point.y, 0, this->m_hWnd, NULL);
CMenu* pPopup = menu.GetSubMenu(0);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN, point.x, point.y, this);
}
Win32 Menu
Display Menu
- Windows class menu
- CreateWindow
- SetMenu
Windows class menu
#include "resource.h"
#include <windows.h>
HINSTANCE hinst;
LRESULT APIENTRY MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(_In_ HINSTANCE hinstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
MSG msg = { }; // message
WNDCLASS wc; // windowclass data
HWND hwnd; // handle to the main window
// Create the window class for the main window. Specify
// the identifier of the menu-template resource as the
// lpszMenuName member of the WNDCLASS structure to create
// the class menu.
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
//wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.hbrBackground = NULL;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wc.lpszClassName = TEXT("MainWClass");
if (!RegisterClass(&wc))
return FALSE;
hinst = hinstance;
// Create the main window. Set the hmenu parameter to NULL so
// that the system uses the class menu for the window.
hwnd = CreateWindow(TEXT("MainWClass"), TEXT("Sample Application"),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinstance,
NULL);
if (hwnd == NULL)
return FALSE;
// Make the window visible and send a WM_PAINT message to the
// window procedure.
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Start the main message loop.
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
UNREFERENCED_PARAMETER(hPrevInstance);
}
LRESULT APIENTRY MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
// Process other window messages.
case WM_COMMAND:
// Test for the identifier of a command item.
switch (LOWORD(wParam))
{
case IDM_FI_OPEN:
//DoFileOpen(); // application-defined
break;
case IDM_FI_CLOSE:
//DoFileClose(); // application-defined
break;
// Process other menu commands.
default:
break;
}
return 0;
// Process other window messages.
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return NULL;
}