1.在资源中添加对话框并生成一个叫CMyDlg的类
2.在系统菜单中添加菜单项
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{ .
.
.
CMenu *pMenu=GetSystem(FALSE);//获得一个指向系统菜单的指针
pMenu->AppendMenu(MF_SEPARATOR);//添加分隔线
pMenu->AppendMenu(MF_STRING,ID_SYSMENU_ABOUT,"About");//添加系统菜单项
return 0;
}
3.消息处理,对应的是WM_SYSCOMMAND消息
(1)在CMAINFRAME的头文件中添加
protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSysCommand(UINT nID,LPARAM lParam);//要添加的代码
(2)添加消息映射
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
ON_WM_SYSCOMMAND()//添加的代码
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
(3)消息处理
void CMainFrame::OnSysCommand(UINT nID,LPARAM lParam)
{
if(nID==ID_SYSMENU_ABOUT)
{
CMyDlg dlg;
dlg.DoModal();
return ;
}
CFrameWnd::OnSysCommand(nID,lParam);
}
运行,按鼠标左键单击窗口标题栏的小图标或者ALT+空格,就添加了一项!!!
另外,别忘了在MainFrm.CPP中添加
#define ID_SYSMENU_ABOUT 105
#include "MyDlg.h"
实际上在MFC向导的第一步中,选择对话框后,生成的就是在系统菜单中添加了About,也可以去参考一下!!!!