关于CSizeControlBar的使用
在项目中遇到了CSizeControlBar停靠的使用。于是准备记录一下,以备后面复习时可以回顾。
一、下面来介绍一下如何使用:
1.首先需要去这个博客下载源码,里面还有两个Demo可以参考:源码
下面我记录一下学习demo2过程:
在stdafx.h头文件中,添加如下头文件.注意:如果自己新建工程一定要注意添加的顺序,一定要按照demo里面添加的顺序一样,否则会造成有点类没有定义。
#define _SCB_REPLACE_MINIFRAME
//#define _SCB_MINIFRAME_CAPTION
#include "..\src\sizecbar.h"
#include "..\src\scbarg.h"
#include "..\src\scbarcf.h"
#define baseCMyBar CSizingControlBarCF
2.接下来自定义一个类CMyBar继承自:baseCMyBar
MyBar.h头文件如下所示:
#ifndef baseCMyBar
#define baseCMyBar CSizingControlBarG
#endif
class CMyBar : public baseCMyBar
{
// Construction
public:
CMyBar();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyBar)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMyBar();
protected:
CEdit m_wndChild;
CFont m_font;
// Generated message map functions
protected:
//{{AFX_MSG(CMyBar)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(__MYBAR_H__)
其实现文件CMyBar.cpp如下所示:
#include "stdafx.h"
#include "mybar.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CMyBar
CMyBar::CMyBar()
{
}
CMyBar::~CMyBar()
{
}
BEGIN_MESSAGE_MAP(CMyBar, baseCMyBar)
//{{AFX_MSG_MAP(CMyBar)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CMyBar message handlers
int CMyBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (baseCMyBar::OnCreate(lpCreateStruct) == -1)
return -1;
SetSCBStyle(GetSCBStyle() | SCBS_SIZECHILD);
if (!m_wndChild.Create(WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_WANTRETURN|ES_AUTOVSCROLL,
CRect(0,0,0,0), this, 123))
return -1;
m_wndChild.ModifyStyleEx(0, WS_EX_CLIENTEDGE);
// older versions of Windows* (NT 3.51 for instance)
// fail with DEFAULT_GUI_FONT
if (!m_font.CreateStockObject(DEFAULT_GUI_FONT))
if (!m_font.CreatePointFont(80, "MS Sans Serif"))
return -1;
m_wndChild.SetFont(&m_font);
return 0;
}
3.在CMainFram.h头文件中添加如下代码:
#include "MyBar.h"
class 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);
virtual BOOL DestroyWindow();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
BOOL VerifyBarState(LPCTSTR lpszProfileName);
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CToolBar m_wndToolBar2;
CMyBar m_wndMyBars[4];
baseCMyBar m_wndInstantBar; // instant bar
CTreeCtrl m_wndIBTree; // instant bar child
// Generated message map functions
protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnViewMytoolbar();
afx_msg void OnUpdateViewMytoolbar(CCmdUI* pCmdUI);
afx_msg void OnViewInstant();
afx_msg void OnUpdateViewInstant(CCmdUI* pCmdUI);
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
afx_msg void OnViewMy(UINT nID);
afx_msg void OnUpdateViewMy(CCmdUI* pCmdUI);
DECLARE_MESSAGE_MAP()
};
4.CMainFram.h的实现文件如下:
#include "stdafx.h"
#include "Demo2.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
ON_COMMAND(ID_VIEW_MYTOOLBAR, OnViewMytoolbar)
ON_UPDATE_COMMAND_UI(ID_VIEW_MYTOOLBAR, OnUpdateViewMytoolbar)
ON_COMMAND(ID_VIEW_INSTANT, OnViewInstant)
ON_UPDATE_COMMAND_UI(ID_VIEW_INSTANT, OnUpdateViewInstant)
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(ID_VIEW_1, ID_VIEW_4, OnViewMy)
ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_1, ID_VIEW_4, OnUpdateViewMy)
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()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
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
}
// second toolbar must have different ID
if (!m_wndToolBar2.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC,
CRect(0, 0, 0, 0), 122) ||
!m_wndToolBar2.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
m_wndToolBar2.SetWindowText(_T("Toolbar 2"));
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
}
int i;
for (i = 0; i < 4; i++)
{
CString sTitle;
sTitle.Format(_T("My Bar %d"), i + 1);
if (!m_wndMyBars[i].Create(sTitle, this, 123 + i))
{
TRACE0("Failed to create mybar\n");
return -1; // fail to create
}
}
// --- instant bar ---
if (!m_wndInstantBar.Create(_T("Instant Bar"), this, 127))
{
TRACE0("Failed to create instant bar\n");
return -1; // fail to create
}
m_wndInstantBar.SetSCBStyle(m_wndInstantBar.GetSCBStyle() |
SCBS_SIZECHILD);
if (!m_wndIBTree.Create(WS_CHILD|WS_VISIBLE|
TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT,
CRect(0, 0, 0, 0), &m_wndInstantBar, 100))
{
TRACE0("Failed to create instant bar child\n");
return -1; // fail to create
}
m_wndIBTree.ModifyStyleEx(0, WS_EX_CLIENTEDGE);
// bring the tooltips to front
CWnd* pTT = FromHandle((HWND) m_wndIBTree.SendMessage(TVM_GETTOOLTIPS));
if (pTT != NULL)
pTT->SetWindowPos(&wndTopMost, 0, 0, 0, 0,
SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
// populate the treectrl
HTREEITEM hti = m_wndIBTree.InsertItem(_T("Node 1"));
m_wndIBTree.InsertItem(_T("Node 2"));
m_wndIBTree.InsertItem(_T("SubNode 11"), hti);
m_wndIBTree.InsertItem(_T("SubNode 12"), hti);
// --- end instant bar creation and child setup ---
for (i = 0; i < 4; i++)
m_wndMyBars[i].SetBarStyle(m_wndMyBars[i].GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndInstantBar.SetBarStyle(m_wndInstantBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
m_wndToolBar2.EnableDocking(CBRS_ALIGN_ANY);
for (i = 0; i < 4; i++)
m_wndMyBars[i].EnableDocking(CBRS_ALIGN_ANY);
// enable docking only on the left of right sides (or floating)
m_wndInstantBar.EnableDocking(CBRS_ALIGN_LEFT|CBRS_ALIGN_RIGHT);
EnableDocking(CBRS_ALIGN_ANY);
#ifdef _SCB_REPLACE_MINIFRAME
m_pFloatingFrameClass = RUNTIME_CLASS(CSCBMiniDockFrameWnd);
#endif //_SCB_REPLACE_MINIFRAME
DockControlBar(&m_wndToolBar);
DockControlBar(&m_wndToolBar2, AFX_IDW_DOCKBAR_RIGHT);
DockControlBar(&m_wndMyBars[0], AFX_IDW_DOCKBAR_RIGHT);
// dock mybar2 on the same row with mybar1
RecalcLayout();
CRect rBar;
m_wndMyBars[0].GetWindowRect(rBar);
rBar.OffsetRect(0, 1);
DockControlBar(&m_wndMyBars[1], AFX_IDW_DOCKBAR_RIGHT, rBar);
DockControlBar(&m_wndMyBars[2], AFX_IDW_DOCKBAR_BOTTOM);
FloatControlBar(&m_wndMyBars[3], CPoint(300, 200));
DockControlBar(&m_wndInstantBar, AFX_IDW_DOCKBAR_LEFT);
CString sProfile = _T("BarState");
if (VerifyBarState(sProfile))
{
CSizingControlBar::GlobalLoadState(this, sProfile);
LoadBarState(sProfile);
}
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::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
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/
// CMainFrame message handlers
BOOL CMainFrame::DestroyWindow()
{
CString sProfile = _T("BarState");
CSizingControlBar::GlobalSaveState(this, sProfile);
SaveBarState(sProfile);
return CFrameWnd::DestroyWindow();
}
void CMainFrame::OnViewMytoolbar()
{
ShowControlBar(&m_wndToolBar2, !m_wndToolBar2.IsVisible(), FALSE);
}
void CMainFrame::OnUpdateViewMytoolbar(CCmdUI* pCmdUI)
{
pCmdUI->Enable();
pCmdUI->SetCheck(m_wndToolBar2.IsVisible());
}
void CMainFrame::OnViewMy(UINT nID)
{
int nBar = nID - ID_VIEW_1;
BOOL bShow = m_wndMyBars[nBar].IsVisible();
ShowControlBar(&m_wndMyBars[nBar], !bShow, FALSE);
}
void CMainFrame::OnUpdateViewMy(CCmdUI* pCmdUI)
{
int nBar = pCmdUI->m_nID - ID_VIEW_1;
pCmdUI->Enable();
pCmdUI->SetCheck(m_wndMyBars[nBar].IsVisible());
}
void CMainFrame::OnViewInstant()
{
ShowControlBar(&m_wndInstantBar, !m_wndInstantBar.IsVisible(), FALSE);
}
void CMainFrame::OnUpdateViewInstant(CCmdUI* pCmdUI)
{
pCmdUI->Enable();
pCmdUI->SetCheck(m_wndInstantBar.IsVisible());
}
// This function is Copyright (c) 2000, Cristi Posea.
// See www.datamekanix.com for more control bars tips&tricks.
BOOL CMainFrame::VerifyBarState(LPCTSTR lpszProfileName)
{
// 在永久性内存(文件)中加载、卸载或清除一个或多个停靠控件条状态的序列化 CObject 类。
CDockState state;
state.LoadState(lpszProfileName);
for (int i = 0; i < state.m_arrBarInfo.GetSize(); i++)
{
CControlBarInfo* pInfo = (CControlBarInfo*)state.m_arrBarInfo[i];
ASSERT(pInfo != NULL);
int nDockedCount = pInfo->m_arrBarID.GetSize();
if (nDockedCount > 0)
{
// dockbar
for (int j = 0; j < nDockedCount; j++)
{
UINT nID = (UINT) pInfo->m_arrBarID[j];
if (nID == 0) continue; // row separator
if (nID > 0xFFFF)
nID &= 0xFFFF; // placeholder - get the ID
if (GetControlBar(nID) == NULL)
return FALSE;
}
}
if (!pInfo->m_bFloating) // floating dockbars can be created later
if (GetControlBar(pInfo->m_nBarID) == NULL)
return FALSE; // invalid bar ID
}
return TRUE;
}
以上代码主要是在MainFram.cpp的OnCreate函数中操作,其中有一些是菜单项的事件响应函数。
其中的类的说明如下:
CDockState:// 在永久性内存(文件)中加载、卸载或清除一个或多个停靠控件条状态的序列化 CObject 类。
详细信息参考微软官方文档:CDockState
CControlBarInfo:CControlBarInfo - used for docking serialization(在源码中写的,我也不太清楚)
项目源码:源码