关闭

右键菜单的实现(类)

标签: menustringtreenull
629人阅读 评论(0) 收藏 举报
分类:
HTREEITEM m_hItem;
 CXMLView* m_hWnd;
 void DisplayMenu();
private:
 CBitmap m_AddMap;
 CBitmap m_DelMap;
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;
 }
}

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1340235次
    • 积分:20812
    • 等级:
    • 排名:第365名
    • 原创:810篇
    • 转载:12篇
    • 译文:1篇
    • 评论:227条
    最新评论
    ATL/WTL
    Big Deal
    Book