给树控件添加右键菜单

 

步骤1:

添加MM_CONTEXTMENU消息:

添加代码如下:

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

{

         // TODO: Add your message handler code here

         CPoint cPoint;

         CMenu RightMenu;

         CRect ItemRect;

         int   nImag;

        

         GetCursorPos(&cPoint);   //获取鼠标的位置

         m_DevTree.ScreenToClient(&cPoint);    //将鼠标的屏幕坐标转换为树控件的客户坐标

        

         HTREEITEM hItem = m_DevTree.HitTest(cPoint);

         if (hItem == NULL)

         {

                   return;

         }

         m_DevTree.SelectItem(hItem);

        

         m_DevTree.GetItemImage(hItem, nImag, nImag);

         if (nImag != 0)

         {

                   return;

         }

         m_DevTree.ClientToScreen(&cPoint);

        

         RightMenu.CreatePopupMenu();

         RightMenu.AppendMenu(MF_STRING, MENU_ADDDEV, "添加不同网段的设备");

       RightMenu.TrackPopupMenu(TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, cPoint.x,cPoint.y, FromHandle(m_hWnd));

}

步骤2:

手动添加相应菜单消息:

1.      在.h文件中添加消息函数声明:

afx_msg void OnPopuMenu(UINT nID);

2.      在.cpp文件中添加消息映射

ON_COMMAND_RANGE(MENU_BASE, MENU_END, OnPopuMenu)

3.      添加函数体

void CConfigurationToolDlg::OnPopuMenu(UINT nID)

{

    switch(nID)

       {

       case MENU_ADDDEV:

              CAddDevipDlg dlg;

              dlg.DoModal();

              break;

 

       }

}

4.      忘了定义消息了

#define  MENU_BASE     WM_USER+10000

#define  MENU_ADDDEV   WM_USER+10001

#define  MENU_END      WM_USER+10002

你可能感兴趣的:(MFC)