怎么样在vc++中添加上下文菜单?
怎么样在vc++中添加上下文菜单?
首先要在在文件首定义菜单项:
#defineID_MENU_EDIT5001
#defineID_MENU_DELETE5002
#defineID_MENU_DELETE5002
然后添加对话框的WM_CONTEXTMENU消息函数,函数内容为:
CMenumenuPopup;
if(menuPopup.CreatePopupMenu())
{
menuPopup.AppendMenu(MF_STRING,ID_MENU_EDIT,"修改(&E)");
menuPopup.AppendMenu(MF_STRING,ID_MENU_DELETE,"删除(&D)");
menuPopup.TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
}
if(menuPopup.CreatePopupMenu())
{
menuPopup.AppendMenu(MF_STRING,ID_MENU_EDIT,"修改(&E)");
menuPopup.AppendMenu(MF_STRING,ID_MENU_DELETE,"删除(&D)");
menuPopup.TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
}
然后定义菜单相应函数,
1,在头文件中添加函数定义语句:
//Generatedmessagemapfunctions
//{{AFX_MSG(CAdo2Dlg)
virtualBOOLOnInitDialog();
afx_msgvoidonInfoEdit();//这个是编辑菜单的响应函数
afx_msgvoidonInfoDelete();//这个是删除菜单的响应函数
afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);
afx_msgvoidOnPaint();
afx_msgHCURSOROnQueryDragIcon();
afx_msgvoidOnButton1();
afx_msgvoidOnButton2();
afx_msgvoidOnRdblclkList1(NMHDR*pNMHDR,LRESULT*pResult);
afx_msgvoidOnDblclkList1(NMHDR*pNMHDR,LRESULT*pResult);
afx_msgvoidOnContextMenu(CWnd*pWnd,CPointpoint);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
//{{AFX_MSG(CAdo2Dlg)
virtualBOOLOnInitDialog();
afx_msgvoidonInfoEdit();//这个是编辑菜单的响应函数
afx_msgvoidonInfoDelete();//这个是删除菜单的响应函数
afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);
afx_msgvoidOnPaint();
afx_msgHCURSOROnQueryDragIcon();
afx_msgvoidOnButton1();
afx_msgvoidOnButton2();
afx_msgvoidOnRdblclkList1(NMHDR*pNMHDR,LRESULT*pResult);
afx_msgvoidOnDblclkList1(NMHDR*pNMHDR,LRESULT*pResult);
afx_msgvoidOnContextMenu(CWnd*pWnd,CPointpoint);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
2,在cpp文件中添加函数体:
voidCAdo2Dlg::OnInfoEdit()
{
AfxMessageBox("edit");
}
voidCAdo2Dlg::OnInfoDelete()
{
AfxMessageBox("delete");
}
{
AfxMessageBox("edit");
}
voidCAdo2Dlg::OnInfoDelete()
{
AfxMessageBox("delete");
}
3,然后在cpp文件中添加影射:
BEGIN_MESSAGE_MAP(CAdo2Dlg,CDialog)
//{{AFX_MSG_MAP(CAdo2Dlg)
ON_COMMAND(ID_MENU_EDIT,OnInfoEdit)
ON_COMMAND(ID_MENU_DELETE,OnInfoDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//{{AFX_MSG_MAP(CAdo2Dlg)
ON_COMMAND(ID_MENU_EDIT,OnInfoEdit)
ON_COMMAND(ID_MENU_DELETE,OnInfoDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
所有的工作完成了!