1.为tab control 添加WM_RCLICK消息响应(使用类向导)
2.在响应函数中添加菜单的创建和显示的代码
1)需要判断鼠标右键点击的标签是否是当前页面
void CTTYMonitorDlg::OnRclickTabctrl(NMHDR* pNMHDR, LRESULT* pResult)
{// TODO: Add your control notification handler code here
//HXH Add 2014-08-18 Add Delete tab page
//获得tab control的当前标签页
int nActiveItem = m_TabCtrl.GetCurSel();
//获得相对于屏幕的鼠标位置坐标
CPoint WndCursePoint;
GetCursorPos(&WndCursePoint);
//将相对于屏幕的鼠标坐标转化为相对于tab control客户区的位置坐标
CPoint ClientCursePoint = WndCursePoint;//存储相对于客户区的鼠标位置坐标
::ScreenToClient(m_TabCtrl.GetSafeHwnd(),&ClientCursePoint);
//获得tab control当前标签页的标签矩形区域坐标
CRect RectItem;
m_TabCtrl.GetItemRect(nActiveItem,&RectItem);
//判断鼠标坐标点是否在当前标签区域中,非0:点在CRect对象中,0:点不在区域中
int nflag = RectItem.PtInRect(ClientCursePoint);
if( !nflag )//不在区域内,nflag = 0, 直接返回,右键不弹出不响应
return ;
//鼠标坐标在当前页的标签区域内,响应右键,弹出菜单
CMenu TabPopMenu;
CMenu *pTabPopMenu;
CBitmap AddBitMap , DelBitMap ;//选中之前的位图
CBitmap AddCheckBitMap , DelCheckBitMap ;//选中之后的位图
TabPopMenu.LoadMenu(IDR_MENU_TABCTRL);
pTabPopMenu=TabPopMenu.GetSubMenu(0);
//设置菜单的位图
AddBitMap.LoadBitmap(IDB_BITMAP33);
AddCheckBitMap.LoadBitmap(IDB_BITMAP31);
DelBitMap.LoadBitmap(IDB_BITMAP34);
DelCheckBitMap.LoadBitmap(IDB_BITMAP32);
//菜单所需位图的大小 13*13
// CString str;
// str.Format("x=%d,y=%d",GetSystemMetrics(SM_CXMENUCHECK),GetSystemMetrics(SM_CYMENUCHECK));
// MessageBox(str);
//设置菜单项处于选中状态
pTabPopMenu->CheckMenuItem(ID_MENUITEM32773_ADD,MF_BYCOMMAND|MF_CHECKED);
pTabPopMenu->CheckMenuItem(ID_MENUITEM32772_DEL,MF_BYCOMMAND|MF_CHECKED);
//设置tab control首页不能被删除,禁用删除监控的菜单项
if(nActiveItem == 0)
pTabPopMenu->EnableMenuItem(ID_MENUITEM32772_DEL,MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
pTabPopMenu->SetMenuItemBitmaps(ID_MENUITEM32773_ADD,MF_BYCOMMAND,&AddCheckBitMap,&AddBitMap);
pTabPopMenu->SetMenuItemBitmaps(ID_MENUITEM32772_DEL,MF_BYCOMMAND,&DelCheckBitMap,&DelBitMap);
pTabPopMenu->TrackPopupMenu(TPM_LEFTALIGN,WndCursePoint.x,WndCursePoint.y,this);
//HXH Add end 2014-08-18 Add Delete tab page
*pResult = 0;
}
3.点击菜单的相应菜单项关联到当前的窗口类(已经存在的类)
4.菜单项目关联相应的相应函数
BEGIN_MESSAGE_MAP(CTTYMonitorDlg, CDialog)
//{{AFX_MSG_MAP(CTTYMonitorDlg)
ON_COMMAND(ID_MENUITEM32773_ADD, OnButtonAddtabpage)
ON_COMMAND(ID_MENUITEM32772_DEL, OnButtonDeletetabpage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
5.写响应函数,并在头文件中声明afx_msg void OnButtonAddtabpage();
void CXXXDlg:: OnButtonDeletetabpage()