使用BCGToolBar制作个性工具栏,步骤如下:
(1)MainFrame.h文件中,定义:#define CBCGFrameWnd CBCGPFrameWnd
(2)在MainFrame.h中,把CFrameWnd改为CBCGFrameWnd,定义变量:CBCGPToolBar m_XXX 修改后的头文件如下:
#define CBCGFrameWnd CBCGPFrameWnd
class CMainFrame : public CBCGFrameWnd
{
protected: // create from serialization only
CMainFrame();
DECLARE_DYNCREATE(CMainFrame)
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMainFrame)
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
// virtual void ActivateFrame(int nCmdShow = -1);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CBCGPToolBar m_myToolBar;
// Generated message map functions
protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
(3)在MainFrame.CPP中,主要修改oncreate()函数
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CBCGFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
CBCGPVisualManager::SetDefaultManager (RUNTIME_CLASS (CBCGPVisualManagerXP));
CBCGPToolBar::EnableQuickCustomization ();
// CBCGPToolBar::SetMenuSizes (CSize (22, 22), CSize (16, 16));
if (!m_myToolBar.CreateEx(
this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC,CRect(1,1,1,1),10002) ||
!m_myToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_myToolBar.SetWindowText( _T("工具条"));
m_myToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_myToolBar);
// m_myToolBar.ShowControlBar(TRUE,TRUE,TRUE);
return 0;
}
还要修改其他部分,就是凡是从CMainFrame继承的都要改为CBCGFrameWnd。修改后,如下:
#include "stdafx.h"
#include "MyDoc.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CBCGFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CBCGFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_ACTIVATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// m_myToolBar = NULL;
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CBCGFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
CBCGPVisualManager::SetDefaultManager (RUNTIME_CLASS (CBCGPVisualManagerXP));
CBCGPToolBar::EnableQuickCustomization ();
// CBCGPToolBar::SetMenuSizes (CSize (22, 22), CSize (16, 16));
if (!m_myToolBar.CreateEx(
this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC,CRect(1,1,1,1),10002) ||
!m_myToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_myToolBar.SetWindowText( _T("工具条"));
m_myToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_myToolBar);
// m_myToolBar.ShowControlBar(TRUE,TRUE,TRUE);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CBCGFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CBCGFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CBCGFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers