Windows API函数封装的成员函数:
CMenu::TrackPopupMenu显示上下文菜单
CMenu::EnableMenuItem使菜单项有效或灰化。
加载并显示菜单
1.Create(NULL, _T("MY Application"), WS_OVERLAPPEDWINDOW,
rectDefault, NULL, MAKEINTRESOURCE(IDR_MAINFRAME));
2.LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW, NULL, NULL);
CMenu nenu;
menu.LoadMenu(IDR_**);
SetMenu(&menu);
DrawMenuBar();
ON_COMMAND(ID_COLOR_RED, OnColor)
ON_COMMAND(ID_COLOR_GREEN, OnColor)
ON_COMMAND(ID_COLOR_BLUE, OnColor)
void CMainWindow::OnColor()
{
UINT nID = (UINT)LOWORD(GetCurrentMessage() -> wParam);
m_nCurrentColor = nID -ID_COLOR_RED;
}
ON_COMMAND_RANGE(ID_COLOR_RED, ID_COLOR_BLUE, OnColor)
void CMainWIndow::OnColor(UINT nID)
{
m_nCurrentColor = nID - ID_COLOR_RED;
}
更新菜单项:
ON_UPDATE_COMMAND_UI(ID_COLOR_RED, OnUpdateColorRed)
void CMainWindow::OnUpdateCOlorRed(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_nCurrentColor == 0);
}
键盘加速键:
LoadAccelTable(MAKEINTRESOURCE(IDR_MAINFRAME));
如要加速键起作用,消息循环必须包含一个API::TranslateAccelerator();
自绘ITEM代码:
void CMainFrame::OnDrawItem (int nIDCtl, LPDRAWITEMSTRUCT lpdis)
{
BITMAP bm;
CBitmap bitmap;
bitmap.LoadOEMBitmap (OBM_CHECK);// 选择图标
bitmap.GetObject (sizeof (bm), &bm);
CDC dc;
dc.Attach (lpdis->hDC);
CBrush* pBrush = new CBrush (::GetSysColor ((lpdis->itemState &
ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_MENU));
dc.FrameRect (&(lpdis->rcItem), pBrush);
delete pBrush;
if (lpdis->itemState & ODS_CHECKED) {
CDC dcMem;
dcMem.CreateCompatibleDC (&dc);
CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
dc.BitBlt (lpdis->rcItem.left + 4, lpdis->rcItem.top +
(((lpdis->rcItem.bottom - lpdis->rcItem.top) -
bm.bmHeight) / 2), bm.bmWidth, bm.bmHeight, &dcMem,
0, 0, SRCCOPY);
dcMem.SelectObject (pOldBitmap);
}
UINT itemID = lpdis->itemID & 0xFFFF; // Fix for Win95/98 bug
pBrush = new CBrush (m_wndView.m_clrColors[itemID - ID_COLOR_RED]);
CRect rect = lpdis->rcItem;
rect.DeflateRect (6, 4);
rect.left += bm.bmWidth;
dc.FillRect (rect, pBrush);
delete pBrush;
dc.Detach ();
}
首先获取指向系统菜单的指针:
CMenu* pSystemMenu = GetSystemMenu(FALSE);
2.pSystemMenu->AppendMenu(MF_SEPARATOR);
3.用户选中系统菜单中的某项,窗口接收WM_SYSCOMMAND.
别忘了看看www.codeproject.com中的相关代码,很难度的:)