给对话框中的CTreeCtrl控件添加右键菜单
给对话框中的CTreeCtrl控件添加右键菜单,有几种方法,一种是从CTreeCtrl派生一个类,在这个类的WM_RBUTTONDOWN中弹出右键菜单,但这样做会增加类。另一个方法就是在对话框的WM_CONTEXTMENU消息中,弹出右键菜单:
void TreeTestDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
if (pWnd == &m_treeCtrl) {
UINT flags;
m_treeCtrl.ScreenToClient(&point);
HTREEITEM hItem = m_treeCtrl.HitTest(point, &flags);
if (hItem) {
m_treeCtrl.SelectItem(hItem);
showTreePopMenu(point, hItem);
}
}
}
其中,showTreePopMenu,用来弹出右键菜单:
void TreeTestDlg::showTreePopMenu(CPoint& point, HTREEITEM hItem)
{
CMenu menu;
CMenu* pMenu = NULL;
menu.LoadMenu(IDR_TREE_MENU);
pMenu = menu.GetSubMenu(0); // 0表示取第一个子菜单
if (!pMenu) return;
GetCursorPos(&point);
pMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
}
没有评论:
发表评论