int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP /*这个参数!!!!!!*/ | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create }下面的是解释: CBRS_TOP Control bar is at the top of the frame window. CBRS_BOTTOM Control bar is at the bottom of the frame window. CBRS_NOALIGN Control bar is not repositioned when the parent is resized. CBRS_LEFT Control bar is at the left of the frame window. CBRS_RIGHT Control bar is at the right of the frame window.换成 CBRS_LEFT 不就可以了!
1.// palette.h : interface of the CPaletteBar class // // This is a part of the Microsoft Foundation Classes C++ library. // Copyright (C) 1992-1998 Microsoft Corporation // All rights reserved. // // This source code is only intended as a supplement to the // Microsoft Foundation Classes Reference and related // electronic documentation provided with the library. // See these sources for detailed information regarding the // Microsoft Foundation Classes product.class CPaletteBar : public CToolBar { // Constructor public: CPaletteBar(); void SetColumns(UINT nColumns); UINT GetColumns() { return m_nColumns; };// Attributes public:// Operations public:// Implementation public: virtual ~CPaletteBar(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endifprotected: UINT m_nColumns;// Generated message map functions protected: //{{AFX_MSG(CPaletteBar) //}}AFX_MSG DECLARE_MESSAGE_MAP() };///////////////////////////////////////////////////////////////////////////// // palette.cpp : implementation of the Floating tool palette class // // This is a part of the Microsoft Foundation Classes C++ library. // Copyright (C) 1992-1998 Microsoft Corporation // All rights reserved. // // This source code is only intended as a supplement to the // Microsoft Foundation Classes Reference and related // electronic documentation provided with the library. // See these sources for detailed information regarding the // Microsoft Foundation Classes product.#include "stdafx.h" #include "ctrlbars.h"#include "palette.h"#ifdef _DEBUG #undef THIS_FILE static char BASED_CODE THIS_FILE[] = __FILE__; #endif///////////////////////////////////////////////////////////////////////////// // CPaletteBarBEGIN_MESSAGE_MAP(CPaletteBar, CToolBar) //{{AFX_MSG_MAP(CPaletteBar) //}}AFX_MSG_MAP END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CPaletteBar construction/destructionCPaletteBar::CPaletteBar() { m_nColumns = 2; m_cxLeftBorder = 5; m_cyTopBorder = 5; m_cxRightBorder = 5; m_cyBottomBorder = 5; }CPaletteBar::~CPaletteBar() { }///////////////////////////////////////////////////////////////////////////// // CPaletteBar diagnostics#ifdef _DEBUG void CPaletteBar::AssertValid() const { CToolBar::AssertValid(); }void CPaletteBar::Dump(CDumpContext& dc) const { CToolBar::Dump(dc); }#endif //_DEBUG///////////////////////////////////////////////////////////////////////////// // CPaletteBar message handlersvoid CPaletteBar::SetColumns(UINT nColumns) { m_nColumns = nColumns; int nCount = GetToolBarCtrl().GetButtonCount(); for (int i = 0; i < nCount; i++) { UINT nStyle = GetButtonStyle(i); BOOL bWrap = (((i + 1) % nColumns) == 0); if (bWrap) nStyle |= TBBS_WRAPPED; else nStyle &= ~TBBS_WRAPPED; SetButtonStyle(i, nStyle); } Invalidate(); GetParentFrame()->RecalcLayout(); }2. BOOL CMainFrame::CreatePaletteBar() { if (!m_wndPaletteBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_SIZE_FIXED | CBRS_TOP | CBRS_TOOLTIPS, ID_PALETTEBAR) || !m_wndPaletteBar.LoadBitmap(IDB_PALETTE) || !m_wndPaletteBar.SetButtons(palette, sizeof(palette)/sizeof(UINT))) { TRACE0("Failed to create toolbar\n"); return FALSE; // fail to create } m_wndPaletteBar.SetWindowText(_T("Palette")); m_wndPaletteBar.EnableDocking(0); // Create the Palette. We are using hardcoded numbers for ease here // normally the location would be read in from an ini file. CPoint pt(GetSystemMetrics(SM_CXSCREEN) - 100, GetSystemMetrics(SM_CYSCREEN) / 3); m_wndPaletteBar.SetColumns(3); FloatControlBar(&m_wndPaletteBar, pt); return TRUE; }int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; EnableDocking(CBRS_ALIGN_ANY); ... if (!CreatePaletteBar()) return -1; ... return 0; } msdn->samples->ctlbars
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP /*这个参数!!!!!!*/
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}下面的是解释:
CBRS_TOP Control bar is at the top of the frame window.
CBRS_BOTTOM Control bar is at the bottom of the frame window.
CBRS_NOALIGN Control bar is not repositioned when the parent is resized.
CBRS_LEFT Control bar is at the left of the frame window.
CBRS_RIGHT Control bar is at the right of the frame window.换成 CBRS_LEFT 不就可以了!
1.// palette.h : interface of the CPaletteBar class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.class CPaletteBar : public CToolBar
{
// Constructor
public:
CPaletteBar();
void SetColumns(UINT nColumns);
UINT GetColumns() { return m_nColumns; };// Attributes
public:// Operations
public:// Implementation
public:
virtual ~CPaletteBar();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endifprotected:
UINT m_nColumns;// Generated message map functions
protected:
//{{AFX_MSG(CPaletteBar)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};/////////////////////////////////////////////////////////////////////////////
// palette.cpp : implementation of the Floating tool palette class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.#include "stdafx.h"
#include "ctrlbars.h"#include "palette.h"#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CPaletteBarBEGIN_MESSAGE_MAP(CPaletteBar, CToolBar)
//{{AFX_MSG_MAP(CPaletteBar)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CPaletteBar construction/destructionCPaletteBar::CPaletteBar()
{
m_nColumns = 2;
m_cxLeftBorder = 5;
m_cyTopBorder = 5;
m_cxRightBorder = 5;
m_cyBottomBorder = 5;
}CPaletteBar::~CPaletteBar()
{
}/////////////////////////////////////////////////////////////////////////////
// CPaletteBar diagnostics#ifdef _DEBUG
void CPaletteBar::AssertValid() const
{
CToolBar::AssertValid();
}void CPaletteBar::Dump(CDumpContext& dc) const
{
CToolBar::Dump(dc);
}#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CPaletteBar message handlersvoid CPaletteBar::SetColumns(UINT nColumns)
{
m_nColumns = nColumns;
int nCount = GetToolBarCtrl().GetButtonCount(); for (int i = 0; i < nCount; i++)
{
UINT nStyle = GetButtonStyle(i);
BOOL bWrap = (((i + 1) % nColumns) == 0);
if (bWrap)
nStyle |= TBBS_WRAPPED;
else
nStyle &= ~TBBS_WRAPPED;
SetButtonStyle(i, nStyle);
} Invalidate();
GetParentFrame()->RecalcLayout();
}2.
BOOL CMainFrame::CreatePaletteBar()
{
if (!m_wndPaletteBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_SIZE_FIXED |
CBRS_TOP | CBRS_TOOLTIPS, ID_PALETTEBAR) ||
!m_wndPaletteBar.LoadBitmap(IDB_PALETTE) ||
!m_wndPaletteBar.SetButtons(palette,
sizeof(palette)/sizeof(UINT)))
{
TRACE0("Failed to create toolbar\n");
return FALSE; // fail to create
} m_wndPaletteBar.SetWindowText(_T("Palette"));
m_wndPaletteBar.EnableDocking(0); // Create the Palette. We are using hardcoded numbers for ease here
// normally the location would be read in from an ini file.
CPoint pt(GetSystemMetrics(SM_CXSCREEN) - 100,
GetSystemMetrics(SM_CYSCREEN) / 3); m_wndPaletteBar.SetColumns(3);
FloatControlBar(&m_wndPaletteBar, pt); return TRUE;
}int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1; EnableDocking(CBRS_ALIGN_ANY); ...
if (!CreatePaletteBar())
return -1;
...
return 0;
}
msdn->samples->ctlbars
TBBS_WRAPPED这个参数很关键,它决定了工具栏是否换行。
TBBS_WRAPPED这个参数很关键,它决定了工具栏是否换行。