CTreeCtrl继承类部分代码 如下
void CZBTreeCtrl::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
{
CPoint point;
GetCursorPos(&point);
HTREEITEM item;
UINT flag = TVHT_ONITEM;
item = this->HitTest(point, &flag);
HTREEITEM hItem =this->GetDropHilightItem( );
if(hItem!=NULL)
this->Select(hItem,TVGN_CARET);
else
hItem=this->GetSelectedItem();
if(hItem != NULL)
{
CMenu menu;
menu.LoadMenu(IDR_POPUP_MENU);
menu.TrackPopupMenu(TPM_LEFTALIGN |
TPM_RIGHTBUTTON, point.x, point.y, this);
}
*pResult = 0;
}
效果如图1
而如下方式则可以正确显示
void CZBTreeCtrl::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
{
HTREEITEM hItem =this->GetDropHilightItem( );
if(hItem!=NULL)
this->Select(hItem,TVGN_CARET);
else
hItem=this->GetSelectedItem();
CMenu menu;
POINT point;
GetCursorPos(&point);
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING,ID_ZB_MENU1,"菜单1");
menu.AppendMenu(MF_STRING, ID_ZB_MENU2, "菜单2");
menu.AppendMenu(MF_STRING, ID_ZB_MENU3, "菜单3");
menu.AppendMenu(MF_STRING, ID_ZB_MENU4 "菜单4");
if(hItem==NULL)
{
menu.EnableMenuItem(ID_ZB_TURNOFF, MF_GRAYED);
menu.EnableMenuItem(ID_ZB_ADJUSTPOWER, MF_GRAYED);
menu.EnableMenuItem(ID_ZB_MAXPOWER, MF_GRAYED);
menu.EnableMenuItem(ID_ZB_STATE, MF_GRAYED);
}
menu.TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,point.x,point.y,this);
*pResult = 0;
}
效果如图2