我添加了如下代码:
CMainFrame::OnRButtonDown(UINT nFlags,CPoint point)
{
 CMenu Menu;
 CMenu *pSubMenu;
Menu.LoadMenu(IDR_TESTMENU);
pSubMenu = Menu.GetSubMenu(0);
ASSERT(pSubMenu!=NULL);
ClientToScreen(&point);
pSubMenu->TrackPopupMenu(nFlags,point.x,point.y,this);
}但是我右键单击单文档菜单栏区域,并没有菜单弹出。

解决方案 »

  1.   

    需要将point坐标转换为客户坐标
      

  2.   

    你是说将ClientToScreen(&point);去掉,
         不行的,你去查一下TrackPopupMenu函数,参数要求是屏幕坐标的
      

  3.   

    不是的,应该是消息没到达CMainFrame
      

  4.   

    我的想法是想VC那样,在框架上右键单击,弹出一个菜单,可以选择显示哪几个工具栏,
    如standerd bar 、build bar等
      

  5.   

    那不是CMainFrame相应的吧,是ToolBar自己响应吧
      

  6.   

    在PretranslateMessage()里面可以截获么?可以的话自己转发消息。
      

  7.   

    代码没有什么问题,应该是没有执行,在PretranslateMessage可以截获除了菜单区域的ButtonDown消息。
      

  8.   

    void CLs2View::OnRButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
     CMenu Menu;
     CMenu *pSubMenu;
    Menu.LoadMenu(IDR_TESTMENU);
    pSubMenu = Menu.GetSubMenu(0);
    ASSERT(pSubMenu!=NULL);
    ClientToScreen(&point);
    pSubMenu->TrackPopupMenu(nFlags,point.x,point.y,this);

    // CView::OnRButtonDown(nFlags, point);
    }
    程序没有问题,很正确,你应该把OnRButtonDown放在View中,不能放在Frame中。
      

  9.   

    void CLs2View::OnRButtonDown(UINT nFlags, CPoint point) 只能在客户区弹出右键菜单,我需要的是在工具栏区域弹出菜单。
      

  10.   

    quote:在PretranslateMessage可以截获除了菜单区域的ButtonDown消息。     那么是不是在PretranslateMessage函数内弹出菜单呢?
      

  11.   

    自己添加一个类从CToolBar继承而来,一定要使CToolBar不能使CToolBarCtrl。然后把你程序中的工具兰成员变量的类型改成泥添加的类的类型,之后在你的新蕾中去添加响音鼠标右击消息的函数,在函数中添加你的那些代码。本人已经试过,没问题。
      

  12.   

    Non-client Right Button Down?WM_NCRBUTTONDOWN message, you can try it.
      

  13.   

    试验了一下,WM_NCRBUTTONDOWN 在MDI中只在边框上有用。
    如果是要改变工具栏,估计不是很好用
      

  14.   

    quote:自己添加一个类从CToolBar继承而来,一定要使CToolBar不能使CToolBarCtrl。然后把你程序中的工具兰成员变量的类型改成泥添加的类的类型,之后在你的新蕾中去添加响音鼠标右击消息的函数,在函数中添加你的那些代码。本人已经试过,没问题。
         但是问题是在单文档工程中AppWizard中已经为CMainFrame类添加了工具栏成员变量,假如要在这个工具栏区域弹出菜单,该怎么做?我想是不是也把这个工具栏变量的类型换成自己自定义的工具栏类型。
      

  15.   

    quote:试验了一下,WM_NCRBUTTONDOWN 在MDI中只在边框上有用。
    如果是要改变工具栏,估计不是很好用    单文档没有WM_NCRBUTTONDOWN 消息吧?
      

  16.   

    quote:自己添加一个类从CToolBar继承而来,一定要使CToolBar不能使CToolBarCtrl。然后把你程序中的工具兰成员变量的类型改成泥添加的类的类型,之后在你的新蕾中去添加响音鼠标右击消息的函数,在函数中添加你的那些代码。本人已经试过,没问题。
         但是问题是在单文档工程中AppWizard中已经为CMainFrame类添加了工具栏成员变量,假如要在这个工具栏区域弹出菜单,该怎么做?我想是不是也把这个工具栏变量的类型换成自己自定义的工具栏类型。
    ----------------------------------------------------------------
    我的意思就是把AppWizard中为CMainFrame类添加的工具栏成员变量变量类型换成自己自定义的工具栏类型。
      

  17.   

    quote:我的意思就是把AppWizard中为CMainFrame类添加的工具栏成员变量变量类型换成自己自定义的工具栏类型。      大虾,我是这样做的:因为要相应右键单击的消息,所以应该继承一个窗口类,因为利用
    classwizard添加新类中没有CToolBar类,所以我基类选择了gerenic Cwnd类。添加新类成功后,我在手工将基类改为CToolBar类。然后我在CMainFrame中添加
    MainFrame.h:
    CMyToolBar  m_MyToolBar;MainFrame.cpp:if (!m_MyToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !m_MyToolBar.LoadToolBar(IDR_MAINFRAME))
    {
    TRACE0("Failed to create toolbar\n");
    return -1;      // fail to create
    }    // TODO: Delete these three lines if you don't want the toolbar to
    //  be dockable
    m_MyToolBar.EnableDocking(CBRS_ALIGN_ANY);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar);    结果出现一个警告框,提示:Failed to create toolbar
      

  18.   

    我真晕!你怎么能选择基类是通用类?这样选择肯定会出错的,你先选择CToolBarCtrl然后改成CToolBar类型。
      

  19.   

    重载这个函数,肯定能出线右键弹出菜单,你试试吧,至于想要你自己的效果,就看着改吧!
    其中 IDR_MENU1是自定义的菜单,
    void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point)
    {
    // TODO: 在此处添加消息处理程序代码
    CMenu myPopupMenu;
    myPopupMenu.LoadMenu(IDR_MENU1);
    CMenu* pPopup=myPopupMenu.GetSubMenu(0);
    pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
    }
      

  20.   

    我知道重载void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point)可以弹出菜单。
    问题是假如我要在特定的工具栏弹出菜单呢?     我照你的办法添加了自定义的工具栏类CMyToolBar,然后添加::OnRButtonDown函数,菜单是出现了,但是是出现在屏幕的左上角。我使用ScreenToClient函数变换坐标也不行。
      

  21.   

    quote:我的意思就是把AppWizard中为CMainFrame类添加的工具栏成员变量变量类型换成自己自定义的工具栏类型。      大虾,我是这样做的:因为要相应右键单击的消息,所以应该继承一个窗口类,因为利用
    classwizard添加新类中没有CToolBar类,所以我基类选择了gerenic Cwnd类。添加新类成功后,我在手工将基类改为CToolBar类。然后我在CMainFrame中添加
    MainFrame.h:
    CMyToolBar  m_MyToolBar;MainFrame.cpp:if (!m_MyToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !m_MyToolBar.LoadToolBar(IDR_MAINFRAME))
    {
    TRACE0("Failed to create toolbar\n");
    return -1;      // fail to create
    }    // TODO: Delete these three lines if you don't want the toolbar to
    //  be dockable
    m_MyToolBar.EnableDocking(CBRS_ALIGN_ANY);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar);    结果出现一个警告框,提示:Failed to create toolbar
    ----------------------------------------------------------------------------
    我真晕!你怎么能选择基类是通用类?这样选择肯定会出错的,你先选择CToolBarCtrl然后改成CToolBar类型。
      

  22.   

    Rivest() 晕!!!!!!!个个都垃圾!!!!。
    ========================
    呵呵
      

  23.   

    下面是具体代码:
    #if !defined(AFX_TOOLBARNEW_H__3BC860FA_DA0C_49C0_BD4B_81DDBF302376__INCLUDED_)
    #define AFX_TOOLBARNEW_H__3BC860FA_DA0C_49C0_BD4B_81DDBF302376__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // ToolBarNew.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CToolBarNew windowclass CToolBarNew : public CToolBar
    {
    // Construction
    public:
    CToolBarNew();// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CToolBarNew)
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CToolBarNew(); // Generated message map functions
    protected:
    //{{AFX_MSG(CToolBarNew)
    afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };
    ---------------------------------------------------------------------------
    // ToolBarNew.cpp : implementation file
    //#include "stdafx.h"
    #include "SingleDoc.h"
    #include "ToolBarNew.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CToolBarNewCToolBarNew::CToolBarNew()
    {
    }CToolBarNew::~CToolBarNew()
    {
    }
    BEGIN_MESSAGE_MAP(CToolBarNew, CToolBar)
    //{{AFX_MSG_MAP(CToolBarNew)
    ON_WM_RBUTTONDOWN()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CToolBarNew message handlersvoid CToolBarNew::OnRButtonDown(UINT nFlags, CPoint point) 
    {
    CMenu Menu;
     CMenu *pSubMenu;
    Menu.LoadMenu(IDR_MENU1);
    pSubMenu = Menu.GetSubMenu(0);
    ASSERT(pSubMenu!=NULL);
    ClientToScreen(&point);
    pSubMenu->TrackPopupMenu(nFlags,point.x,point.y,this);
    // TODO: Add your message handler code here and/or call default

    CToolBar::OnRButtonDown(nFlags, point);
    }
    -------------------------------------------------------------------------
    // MainFrm.h : interface of the CMainFrame class
    //
    /////////////////////////////////////////////////////////////////////////////
    治理面就该架星号的部分
    #if !defined(AFX_MAINFRM_H__5826F53B_6FB0_4B4A_9EF0_807A637B7150__INCLUDED_)
    #define AFX_MAINFRM_H__5826F53B_6FB0_4B4A_9EF0_807A637B7150__INCLUDED_#include "ToolBarNew.h"*******#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000class CMainFrame : public CFrameWnd
    {

    protected: // create from serialization only
    CMainFrame();
    DECLARE_DYNCREATE(CMainFrame)// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMainFrame)
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CMainFrame();
    #ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endifprotected:  // control bar embedded members
    CStatusBar  m_wndStatusBar; 
    CToolBarNew    m_wndToolBar;**************// Generated message map functions
    protected:
    //{{AFX_MSG(CMainFrame)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnNcLButtonDown(UINT nHitTest, CPoint point);
    afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
    afx_msg UINT OnNcHitTest(CPoint point);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MAINFRM_H__5826F53B_6FB0_4B4A_9EF0_807A637B7150__INCLUDED_)
      

  24.   

    quote: classwizard添加新类中没有CToolBar类我用的是vs03 添加类的向导里面是有CToolBar类的,而且我也试过了, 用它作为基类生成类,替换掉原来的toolbar是可以的,你想要的源代码的话,我可以给你。如果要动态添加菜单可以用这样
    CMenu menu;
    menu.CreatePopupMenu();
    GeTmenu()-〉Appendmenn(MF_POPUP,(UNIT)(menu.m_hmenu)," ........");
    menu.Detach();
    动态增加的菜单的ID号要手动在Resourse.h中添加,上面是我敲得,可能有语病,你试一下了