在OnInitDialog()函数里,mfc对系统菜单进行了处理,要想对你的程序增加属于你自己的系统菜单,可在其基础上进行修改,最后在进行响应.
具体步骤如下:
(1)定义属于自己的系统菜单ID,此ID必须<0xF000,这样才不会跟系统使用的ID产生冲突。
#define IDM_SYS_SELF 1
(2)在OnInitDialog里面增加如下红色内容:
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
ASSERT(IDM_SYS_SELF < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE); //取系统菜单
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty()) //About不为空
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
pSysMenu->AppendMenu(MF_SEPARATOR); //加一个分割线
pSysMenu->AppendMenu(MF_STRING, IDM_SYS_SELF, L" By Yourself");
}
(3)最后对自定义的菜单进行响应,可以PreTranslateMessage()中进行:
BOOL CPopupMenuPosDemoDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_SYSCOMMAND)
{
switch (LOWORD(pMsg->wParam))//菜单ID
{
case IDM_SYS_SELF:
MessageBox(L"MY SYSTEM ID");
break;
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
本文出自 “whatever957” 博客,请务必保留此出处http://whatever957.blog.51cto.com/6835003/1620135
原文:http://whatever957.blog.51cto.com/6835003/1620135