(在做报文编辑器时右键菜单老不出来)This article explains how to display a context menu for a tree control in MFC

On Windows platforms, the windowingsubsystem encourages the developer to handle context menu requests in top levelwindows. The traditional Windows programming paradigm therefore concentratesthe code for handling all of the context menu requests in a few window anddialog procedures. However, with the advent of object oriented programming adifferent solution can be embraced; that is to handle the WM_CONTEXTMENUmessage within the control itself thereby creating self-contained entities andobjects. This method allows reuse of the control in more than just one parentwindow and dialog.

In the case of a tree view control, the drag-and-drop functionality that itembeds requires that special steps be taken in order to obtain independencefrom the containing window. After the tree view control receives aWM_RBUTTONDOWN message, messages are not forwarded by the control until aWM_RBUTTONUP message is received. The control decides if a drag-and-dropoperation was or was not initiated.

·   Ifthe user did initiate a drag-and-drop operation, then USER32 generates aWM_CONTEXTMENU message and sends it to the tree view control. If the tree viewcontrol does not handle this message then the default window procedure forwardsit to the control's parent window.

·   Ifthe user did not initiate a drag-and-drop operation then the control's windowprocedure sends a WM_NOTIFY (code NM_RCLICK) message and then a WM_CONTEXTMENUmessage to the parent window.

Consequently, consider the following threecases:

·   WhenSHIFT+F10 is pressed and the control has the focus, a WM_CONTEXTMENU message issent to the tree view control.

·   Whenthe user performs a drag-and-drop operation a WM_CONTEXTMENU message is sent tothe tree view control.

·   Whenthe user right-clicks, a WM_NOTIFY (code NM_RCLICK) message is sent by the treecontrol to the control's parent. If you handle the reflected notification asshown below, the control displays the context menu in all situations.

Toimplement a context menu for a tree view control it is recommended that messagehandlers for both the WM_CONTEXTMENU and reflected WM_NOTIFY (NM_RCLICK)messages be implemented by the control. For example:

BEGIN_MESSAGE_MAP(CMyTreeCtrl,CTreeCtrl)

        //{{AFX_MSG_MAP(CMyTreeCtrl)

        ON_NOTIFY_REFLECT(NM_RCLICK, OnRClick)

        ON_WM_CONTEXTMENU()

        //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

voidCMyTreeCtrl::OnRClick(NMHDR* pNMHDR, LRESULT* pResult)

{

        TRACE0("CMyTreeCtrl::OnRClick()\n");

        // Send WM_CONTEXTMENU to self

        SendMessage(WM_CONTEXTMENU, (WPARAM)m_hWnd, GetMessagePos());

        // Mark message as handled and suppressdefault handling

        *pResult = 1;

}

 

voidCMyTreeCtrl::OnContextMenu(CWnd* pWnd, CPoint ptMousePos)

{

        // if Shift-F10

        if (ptMousePos.x == -1 &&ptMousePos.y == -1)

               ptMousePos = (CPoint)GetMessagePos();

 

        ScreenToClient(&ptMousePos);

 

        UINT uFlags;

        HTREEITEM htItem;

       

        htItem = HitTest( ptMousePos,&uFlags );

 

        if( htItem == NULL )

               return;

       

        m_hActiveItem = htItem;

 

        CMenu menu;

        CMenu* pPopup;

 

        // the font popup is stored in aresource

        menu.LoadMenu(IDR_TREEITEM_CONTEXTMENU);

        pPopup = menu.GetSubMenu(0);

        ClientToScreen(&ptMousePos);

        pPopup->TrackPopupMenu(TPM_LEFTALIGN, ptMousePos.x, ptMousePos.y, this );

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值