这是我自己编写的一个函数,被封装到一个类里了(在右击函数里调用该函数就可以了),你要求的,这里面应该都实现了,你自己挑一些用的上的吧,方法比较笨,不过还能用。不明白的地方可以问我。
工程名叫“XMLView”,这个类叫“CRightClick”。
.h文件里:==============================================
public:
HTREEITEM m_hItem;
CXMLView* m_hWnd;
void DisplayMenu();
private:
CBitmap m_AddMap;
CBitmap m_DelMap;
Resource.h==============================================
#define ID_MENU_ADDROOT 32771
#define ID_MENU_DELROOT 32772
#define ID_MENU_ADD1 32773
#define ID_MENU_DEL1 32774
#define ID_MENU_ADD2 32775
#define ID_MENU_DEL2 32776
#define ID_MENU_ADD3 32777
#define ID_MENU_DEL3 32778
#define ID_MENU_ADD4 32779
#define ID_MENU_DEL4 32780
.cpp里实现函数DisplayMenu():==========================
void CRightClick::DisplayMenu()
{
m_hWnd = (CXMLView*)(((CMainFrame*)AfxGetMainWnd())->GetActiveView());
CPoint pt;
CPoint pt1;
GetCursorPos(&pt); //读取滑鼠的坐标
GetCursorPos(&pt1);
m_hWnd->ScreenToClient(&pt); //将给定点或显示器上矩形的屏幕坐标转换为客户坐标
CMenu PopMenu;
PopMenu.CreatePopupMenu();
UINT uFlags;
m_hItem = m_hWnd->GetTreeCtrl().HitTest(pt, &uFlags);
if ((m_hItem != NULL) && (TVHT_ONITEM &uFlags))
{
m_hWnd->GetTreeCtrl().SetFocus(); //要求输入焦点
//选择,在视中滚动,或重画一个指定的tree view项, TVGN_CARET 设置给定项的选择
m_hWnd->GetTreeCtrl().Select(m_hItem,TVGN_CARET);
m_hWnd->GetFocus();
//设置了4层嵌套
HTREEITEM ItemRoot, Item1, Item2, Item3, Item4;
ItemRoot = m_hWnd->GetTreeCtrl().GetParentItem(m_hItem);
Item1 = m_hWnd->GetTreeCtrl().GetParentItem(m_hItem);
Item2 = m_hWnd->GetTreeCtrl().GetParentItem(Item1);
Item3 = m_hWnd->GetTreeCtrl().GetParentItem(Item2);
Item4 = m_hWnd->GetTreeCtrl().GetParentItem(Item3);
if(!ItemRoot)
{
PopMenu.AppendMenu(MF_STRING, ID_MENU_ADD1, "添加区域");
PopMenu.AppendMenu(MF_STRING, ID_MENU_DELROOT, "删除工程");
}
else if(Item4)
{
PopMenu.AppendMenu(MF_STRING, ID_MENU_DEL4, "删除计量点");
}
else if(Item3)
{
PopMenu.AppendMenu(MF_STRING, ID_MENU_ADD4, "添加计量点");
PopMenu.AppendMenu(MF_STRING, ID_MENU_DEL3, "删除仪表");
}
else if(Item2)
{
PopMenu.AppendMenu(MF_STRING, ID_MENU_ADD3, "添加仪表");
PopMenu.AppendMenu(MF_STRING, ID_MENU_DEL2, "删除站场");
}
else if(Item1)
{
PopMenu.AppendMenu(MF_STRING, ID_MENU_ADD2, "添加站场");
PopMenu.AppendMenu(MF_STRING, ID_MENU_DEL1, "删除区域");
}
}
else if(m_hItem == NULL && m_flag == TRUE)
{
m_hWnd->GetTreeCtrl().SetFocus(); //要求输入焦点
//选择,在视中滚动,或重画一个指定的tree view项, TVGN_CARET 设置给定项的选择
m_hWnd->GetTreeCtrl().Select(m_hItem,TVGN_CARET);
m_hWnd->GetFocus();
PopMenu.AppendMenu(MF_STRING, ID_MENU_ADDROOT, "创建新工程");
}
DWORD SelectionMade=PopMenu.TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_NONOTIFY|TPM_RETURNCMD,
pt1.x,pt1.y,m_hWnd);
//实现选中的菜单
switch(SelectionMade)
{
case ID_MENU_ADDROOT:
m_hWnd->GetTreeCtrl().InsertItem("新工程", 0, 1, m_RoothItem, m_RoothItem);
m_hWnd->GetTreeCtrl().Expand(m_RoothItem, TVE_EXPAND);
m_flag = FALSE;
break;
case ID_MENU_DELROOT:
m_hWnd->GetTreeCtrl().DeleteItem(m_hItem);
m_flag = TRUE;
break;
case ID_MENU_ADD1:
m_hWnd->GetTreeCtrl().InsertItem("新区域", m_hItem, m_hItem);
m_hWnd->GetTreeCtrl().Expand(m_hItem, TVE_EXPAND);
break;
case ID_MENU_DEL1:
m_hWnd->GetTreeCtrl().DeleteItem(m_hItem);
break;
case ID_MENU_ADD2:
m_hWnd->GetTreeCtrl().InsertItem("新站场",m_hItem, m_hItem);
m_hWnd->GetTreeCtrl().Expand(m_hItem, TVE_EXPAND);
break;
case ID_MENU_DEL2:
m_hWnd->GetTreeCtrl().DeleteItem(m_hItem);
break;
case ID_MENU_ADD3:
m_hWnd->GetTreeCtrl().InsertItem("新流量计",m_hItem, m_hItem);
m_hWnd->GetTreeCtrl().Expand(m_hItem, TVE_EXPAND);
break;
case ID_MENU_DEL3:
m_hWnd->GetTreeCtrl().DeleteItem(m_hItem);
break;
case ID_MENU_ADD4:
m_hWnd->GetTreeCtrl().InsertItem("新计量点",m_hItem, m_hItem);
m_hWnd->GetTreeCtrl().Expand(m_hItem, TVE_EXPAND);
break;
case ID_MENU_DEL4:
m_hWnd->GetTreeCtrl().DeleteItem(m_hItem);
break;
}
}