(在做报文编辑器时右键菜单老不出来)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 );

}

 

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