我添加了如下代码:
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);
}但是我右键单击单文档菜单栏区域,并没有菜单弹出。
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);
}但是我右键单击单文档菜单栏区域,并没有菜单弹出。
不行的,你去查一下TrackPopupMenu函数,参数要求是屏幕坐标的
如standerd bar 、build bar等
{
// 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中。
如果是要改变工具栏,估计不是很好用
但是问题是在单文档工程中AppWizard中已经为CMainFrame类添加了工具栏成员变量,假如要在这个工具栏区域弹出菜单,该怎么做?我想是不是也把这个工具栏变量的类型换成自己自定义的工具栏类型。
如果是要改变工具栏,估计不是很好用 单文档没有WM_NCRBUTTONDOWN 消息吧?
但是问题是在单文档工程中AppWizard中已经为CMainFrame类添加了工具栏成员变量,假如要在这个工具栏区域弹出菜单,该怎么做?我想是不是也把这个工具栏变量的类型换成自己自定义的工具栏类型。
----------------------------------------------------------------
我的意思就是把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
其中 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);
}
问题是假如我要在特定的工具栏弹出菜单呢? 我照你的办法添加了自定义的工具栏类CMyToolBar,然后添加::OnRButtonDown函数,菜单是出现了,但是是出现在屏幕的左上角。我使用ScreenToClient函数变换坐标也不行。
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类型。
========================
呵呵
#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_)
CMenu menu;
menu.CreatePopupMenu();
GeTmenu()-〉Appendmenn(MF_POPUP,(UNIT)(menu.m_hmenu)," ........");
menu.Detach();
动态增加的菜单的ID号要手动在Resourse.h中添加,上面是我敲得,可能有语病,你试一下了