怎么在鼠标右键 单击CTreeCtrl中的项的时候,弹出不同的菜单?

enterprise 2003-05-09 06:20:41
在一个对话框的程序里,放一个CTreeCtrl,
增加NM_RCLICK事件,在里面加了代码,弹出菜单,但那个菜单的定位有问题,总是偏得很厉害。
不知道是应该用ClientToScreen(&rect)还是用ScreenToClient(&rect)?
void CDialog_System_Configure::OnRclickTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
//先获取选择的点的Rect
HTREEITEM ht=this->m_Tree1.GetSelectedItem();
CString str;
//获取节点文字
sprintf(str,"[OnRclickTree1]%s",this->m_Tree1.GetItemText(ht));
AfxMessageBox(str);

CRect rect;
CPoint point;
//获取节点的位置
this->m_Tree1.GetItemRect(ht,&rect,false);
//转换有问题????
ClientToScreen(&rect);
point.x=rect.left;
point.y=rect.top;
//右键菜单
CMenu menu;
VERIFY(menu.LoadMenu(CG_IDR_POPUP_DIALOG__SYSTEM__CONFIGURE));

CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;

while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();

pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
*pResult = 0;


}
...全文
283 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
maojincxj 2003-05-09
  • 打赏
  • 举报
回复
void CTabClassView::OnRButtonDown(UINT nFlags, CPoint point)
{
CTreeView::OnRButtonDown(nFlags, point);

m_pTreeCtrl->SetFocus();

// map the point that is passed to the
// function from client coordinates
// to screen coordinates
ClientToScreen(&point);

// Get the currently selected item
HTREEITEM hCurrSel = m_pTreeCtrl->GetSelectedItem();

// Figure out which item was right clicked
CPoint pt(0, 0);
::GetCursorPos(&pt);
ScreenToClient (&pt);

HTREEITEM hNewSel = m_pTreeCtrl->HitTest(pt, &nFlags);

// Set the selection to the item that the
// mouse was over when the user right
// clicked
if (NULL == hNewSel)
{
m_pTreeCtrl->SelectItem(NULL);
}
else if (hCurrSel != hNewSel)
{
m_pTreeCtrl->SelectItem(hNewSel);
m_pTreeCtrl->SetFocus();
}

CMenu menu;
VERIFY(menu.LoadMenu(IDR_POPUP));

// 选择不同的弹出菜单
if(!hNewSel)
{
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = GetTopLevelFrame();
//..删除不要的菜单,这里往下有5个不要显示
for(int i=0;i<6;i++)
{
pPopup->DeleteMenu(1, MF_BYPOSITION);
}
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);

}
else
{
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = GetTopLevelFrame();

pPopup->DeleteMenu(0, MF_BYPOSITION);

pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
}

/* if(theApp.m_SoftUser != "administrator")
{
menu.EnableMenuItem(0,MF_GRAYED);
menu.EnableMenuItem(3,MF_GRAYED);
menu.EnableMenuItem(4,MF_GRAYED);
}
*/
// TODO: Add your message handler code here and/or call default


}
dizzo 2003-05-09
  • 打赏
  • 举报
回复
同意楼上,up
ndy_w 2003-05-09
  • 打赏
  • 举报
回复
应该用GetCursorPos,得到鼠标位置,而不是item位置。

HTREEITEM CLeftView::GetItemFromPoint(POINT* ppt, BOOL bStrict)
{
TVHITTESTINFO hti;
RECT rc;
CTreeCtrl& tc=GetTreeCtrl();
//make sure click on a item
tc.GetWindowRect(&rc);
hti.pt.x=ppt->x-rc.left;
hti.pt.y=ppt->y-rc.top;

if(bStrict)
{
HTREEITEM hItem=tc.HitTest(&hti);
if(hItem && (hti.flags & (TVHT_ONITEMICON | TVHT_ONITEMLABEL)))
return hItem;
else
return 0;
}
else
return tc.HitTest(&hti);
}

void CLeftView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
POINT pt;
GetCursorPos(&pt);

HTREEITEM hItem=GetItemFromPoint(&pt, FALSE);
if(!hItem)
return;

CTreeCtrl& tc=GetTreeCtrl();
tc.SelectItem(hItem);
int nPopupIndex=GetPopupMenuIndex((CTreeItemData*)tc.GetItemData(hItem));
if(nPopupIndex == ID_POPUP_NONE)
return;
CMenu* pSub=m_PopupMenu.GetSubMenu(nPopupIndex);
pSub->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
pt.x,
pt.y,
AfxGetMainWnd());
*pResult=1;
}

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧