在消息处理函数里,处理WM_COMMAND消息,来处理不同的按键。
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
3.可以通过参数设置,来通过不同方式或者完成不同效果的MenuBar
//set up menu bar structure
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hDlg;
mbi.hInstRes = g_hInst;
switch(g_dwCreateMenuType)
{
case ID_SHCREATEMENUBAR_RCDATA:
mbi.nToolBarId = IDR_CMB_CMDBAR;//RCDATA资源类型,二进制
break;
case ID_SHCREATEMENUBAR_HMENU:
mbi.dwFlags = SHCMBF_HMENU;
mbi.nToolBarId = IDM_CMB_MENU;//菜单资源
break;
case ID_SHCREATEMENUBAR_EMPTYBAR:
mbi.dwFlags = SHCMBF_EMPTYBAR;
break;
case ID_SHCREATEMENUBAR_NOSIPBUTTON:
mbi.dwFlags = SHCMBF_EMPTYBAR|SHCMBF_HIDESIPBUTTON;
break;
case ID_SHCREATEMENUBAR_HIDDENMENUBAR:
mbi.dwFlags = SHCMBF_HMENU;//设置此单数但不设置nToolBarId,会隐藏MenuBar
break;
default:
break;
}
if (FALSE == SHCreateMenuBar(&mbi))
{
MessageBox(hDlg, _T("SHCreateMenuBar Failed"), _T("Error"), MB_OK);
}
//retain the hwnd to the toolbar to destroy it if necessary
hwndMB = mbi.hwndMB;
4. RCDATA 格式说明
/////////////////////////////////////////////////////////////////////////////
//
// RCDATA
//
IDR_MENUOK RCDATA
BEGIN
0, // menu
1, // Count of buttons
I_IMAGENONE,
IDC_OK,
TBSTATE_ENABLED,
TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE,
IDS_OK,
0,
NOMENU,
END