添加下拉菜单:
#在头文件声明一个CMenu对象m_Menu CMenu m_Menu; #在Resource.h中定义三个ID #define ID_MENUCAT 1011 #define ID_MENUDOG 1012 #define ID_MENUMONKEY 1013 #在OnInitDialog方法中添加代码 m_Menu.CreateMenu(); CMenu m_PopMenu; m_PopMenu.CreatePopupMenu(); m_Menu.AppendMenu(MF_POPUP, (UINT)m_PopMenu.m_hMenu, "动物"); m_PopMenu.AppendMenu(MF_STRING, ID_MENUCAT, "猫"); m_PopMenu.AppendMenu(MF_STRING, ID_MENUDOG, "狗"); m_PopMenu.AppendMenu(MF_STRING, ID_MENUMONKEY, "猴子"); m_PopMenu.Detach(); CMenu m_PopMenuPlant; m_PopMenuPlant.CreatePopupMenu(); m_Menu.AppendMenu(MF_POPUP, (UINT)m_PopMenuPlant.m_hMenu, "植物"); m_PopMenuPlant.AppendMenu(MF_STRING, ID_MENUFLOWER, "花朵"); m_PopMenuPlant.Detach(); SetMenu(&m_Menu); #添加菜单的消息处理函数 #在头文件中声明 afx_msg void OnMenucat(); afx_msg void OnMenudog(); afx_msg void OnMenumonkey(); #在源文件中添加消息宏映射 ON_COMMAND(ID_MENUCAT, OnMenucat) ON_COMMAND(ID_MENUDOG, OnMenudog) ON_COMMAND(ID_MENUMONKEY, OnMenumonkey) #在源文件中实现消息处理函数 void CTestDlg::OnMenucat(){ MessageBox("猫菜单被按下"); } void CTestDlg::OnMenudog(){ MessageBox("小狗菜单被按下"); } void CTestDlg::OnMenumonkey(){ MessageBox("猴子菜单被按下"); }
添加右键菜单:
#添加一个菜单资源,ID自定义,我在这里定为IDR_MENU_RIGHT #在头文件添加消息处理函数 afx_msg void OnRButtonUp(UINT nFlags, CPoint point); #在源文件中添加消息宏映射 ON_WM_RBUTTONUP() #在源文件中实现消息处理函数 void CTestDlg::OnRButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CMenu PopMenu; PopMenu.LoadMenu(IDR_MENU_RIGHT); CMenu* pMenu = PopMenu.GetSubMenu(0); CRect rect; ClientToScreen(&point); rect.top = point.x; rect.left = point.y; pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL, rect.top, rect.left, this); CDialog::OnRButtonUp(nFlags, point); }
版权声明:
此文为本站源创文章[或由本站编辑从网络整理改编],
转载请备注出处:
[狂码一生]
https://www.sindsun.com/articles/16/122
[若此文确切存在侵权,请联系本站管理员进行删除!]
--THE END--