手动添加消息映射

来源:互联网 发布:纽约房价知乎 编辑:程序博客网 时间:2024/04/28 21:03

     本文以CMenu类为例子,手动添加菜单消息.

1. 先建立一个基于对话框的工程,工程名字为test.

2.在testDlg.h中加入

public:

CMenu m_menu,m_menuItem;

#define WM_GETMSG WM_USER + 10000

 // Generated message map functions
 //{{AFX_MSG(CTestDlg)
 virtual BOOL OnInitDialog();
 afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
 afx_msg void OnPaint();
 afx_msg HCURSOR OnQueryDragIcon();
 afx_msg void OnMenuOne();//此行为添加内容,菜单映射的函数
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()

 LRESULT OnGetMsg(WPARAM wParam, LPARAM lParam);

3.在testDlg.cpp中加入

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
 //{{AFX_MSG_MAP(CTestDlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_COMMAND(10000,OnMenuOne)//此行为添加内容,点击ONE菜单

 ON_MESSAGE(WM_GETMSG,OnGetMsg)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CTestDlg::OnInitDialog()

{

 // TODO: Add extra initialization here
 m_menu.CreateMenu();
 m_menuItem.CreateMenu();
 m_menuItem.AppendMenu(MF_STRING,10000,"one(&O)");//O为热键
 m_menuItem.AppendMenu(MF_STRING,10001,"two(&T)");//T为热键
 m_menu.AppendMenu(MF_POPUP,(UINT)m_menuItem.m_hMenu,"menu(&M)");//ALT+M为热键
 m_menu.EnableMenuItem(10001,1);//TWO菜单为不可用
 this->SetMenu(&m_menu);

}

void CTestDlg::OnMenuOne()
{
 AfxMessageBox("the number one of menu cliecked!");
}

LRESULT CTestGroupDlg::OnGetMsg(WPARAM wParam, LPARAM lParam)
{
 UpdateData(TRUE);
 return 0;
}

原创粉丝点击