CTreeView右键弹出菜单

今天在做CTreeView右键单击节点弹出菜单操作时,返回的HTREEITEM句柄总是空值,不知道是什么原因?程序是单文档的,使用的下面的方法:

 

POINT point;
	UINT uFlags;

	GetCursorPos(&point);
	CTreeCtrl &pCtrl = GetTreeCtrl();

	pCtrl.ScreenToClient(&point);
	HTREEITEM hItem = pCtrl.HitTest(point, &uFlags);


右键点击时返回的hItem总为空。至今不知原因。

 

 

void CMyTreeView::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	/*
	NM_TREEVIEW* pTreeView = (NM_TREEVIEW*)pNMHDR;
	HTREEITEM hItem = pTreeView->itemNew.hItem;
	*/
	
	HTREEITEM hSel = GetTreeCtrl().GetSelectedItem();
	
	if (hSel != NULL )
	{
		HTREEITEM hChildItem = GetTreeCtrl().GetChildItem(hSel);
		if (!hChildItem)//不存在儿子节点,证明是文件
		{
			CString strSel   = GetTreeCtrl().GetItemText(hSel);
			strFileName     = strSel;
 			strItemPath      = strSel;
			HTREEITEM hParentItem = GetTreeCtrl().GetParentItem(hSel);
			while (hParentItem)
			{
				strItemPath  = _T("\\")+strItemPath;
				strSel     = GetTreeCtrl().GetItemText(hParentItem);
				strItemPath  = strSel+strItemPath;
				hParentItem = GetTreeCtrl().GetParentItem(hParentItem);
			}
		}
	}

	CMenu Menu,*pSubMenu;
	Menu.LoadMenu(IDR_MENU1);
	pSubMenu = Menu.GetSubMenu(0);

	CPoint Pt;
	GetCursorPos(&Pt);
	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,Pt.x,Pt.y,this);

	*pResult = 0;
}

 

 

 

此方法要求在右击节点之前,先左击选中节点,之后在右击。此时可以弹出右键菜单。

 

 

 

你可能感兴趣的:(MFC界面)