弹出菜单TrackPopupMenu与Ctree结合

0在stdafx.h中进行准备工作

#define _UNIT_ONE 0
#define _UNIT_TWO 1
#define _UNIT_THREE 2
#define _UNIT_FORE 3


typedef struct tag_tree
{
unsigned long nType;
char t_name[20];
int  t_id;


}TREEDATA,*LPTREEDATA;

1 在OnInitDialog初始化函数

HTREEITEM hItem = m_tree.InsertItem("目录", NULL, NULL, TVI_ROOT);
HTREEITEM hItem1 =m_tree.InsertItem("镜头",NULL, NULL,hItem);
if(hItem1)
{
LPTREEDATA lpTd=new TREEDATA;
lpTd->nType=_UNIT_ONE;
m_tree.SetItemData(hItem1,(DWORD)lpTd);
}
HTREEITEM hItem2 =m_tree.InsertItem("场景",NULL, NULL,hItem);
if(hItem2)
{
LPTREEDATA lpTd=new TREEDATA;
lpTd->nType=_UNIT_TWO;
m_tree.SetItemData(hItem2,(DWORD)lpTd);
}
HTREEITEM hItem3 =m_tree.InsertItem("电视强",NULL, NULL,hItem);
if(hItem3)
{
LPTREEDATA lpTd=new TREEDATA;
lpTd->nType=_UNIT_THREE;
m_tree.SetItemData(hItem3,(DWORD)lpTd);
}
HTREEITEM hItem4 =m_tree.InsertItem("视频",NULL, NULL,hItem);
if(hItem4)
{
LPTREEDATA lpTd=new TREEDATA;
lpTd->nType=_UNIT_FORE;
m_tree.SetItemData(hItem4,(DWORD)lpTd);
}

2 在OnContextMenu(CWnd* pWnd, CPoint point) 该函数中进行处理

void CEmnuWIDNSSDlg::OnContextMenu(CWnd* pWnd, CPoint point) 

{
UINT nFlags;
CPoint pt=point;


m_tree.ScreenToClient(&pt);

HTREEITEM hItem = m_tree.HitTest(pt,&nFlags);
UINT nMenuResuse ;


if(hItem==NULL ||!(nFlags & TVHT_ONITEM))
return;
else
{
LPTREEDATA lpTd = (LPTREEDATA)m_tree.GetItemData(hItem);
if(lpTd == NULL) return;
switch(lpTd->nType)
{
case _UNIT_ONE:
case _UNIT_TWO:
nMenuResuse = IDR_MENU1;
break;
case _UNIT_THREE:
case _UNIT_FORE:
nMenuResuse = IDR_MENU2;
break;
default:
break;
}
CMenu menu;
menu.LoadMenu(nMenuResuse);
CMenu *pMenu = menu.GetSubMenu(0);
if (pMenu)
{
pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTALIGN ,point.x,point.y,this);
}
}
}

你可能感兴趣的:(弹出菜单TrackPopupMenu与Ctree结合)