方法如下:
1.?
將
scbarg.h
,
scbarg.cpp
,
sizecbar.h
,
sizecbar.cpp
,
mybar.h
,
mybar.cpp
文件
copy
到你的工程目錄下,打開
vc
,將這幾個文件添加進工程。
2.?
打開
stdafx.h
文件,在其中加入:
#include "sizecbar.h"
,
#include "scbarg.h"
。
3.?
為
CmainFrame
類添加成員變量:
CMyBar??????? m_wndMyBar
。
4.?
重載
CMainFrame::DestroyWindow()
,在其中加入
?????? CString sProfile(_T("BarState"));
?????? m_wndMyBar.SaveState(sProfile);
SaveBarState(sProfile);
5.?
修改
CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
如下:
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
?????? }
?
?????? 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
?????? }
??????
if (!m_wndMyBar.Create(_T("My Bar"), this, 123))
?????? {
???????????TRACE0("Failed to create mybar\n");
???????????return -1;????? // fail to create
?????? }
?????? // TODO: Delete these three lines if you don't want the toolbar to
?????? //? be dockable
??????
m_wndMyBar.SetBarStyle(m_wndMyBar.GetBarStyle() |
?????? CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
?????? m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
??????
m_wndMyBar.EnableDocking(CBRS_ALIGN_ANY);
?????? EnableDocking(CBRS_ALIGN_ANY);
?????? DockControlBar(&m_wndToolBar);
??????
DockControlBar(&m_wndMyBar, AFX_IDW_DOCKBAR_LEFT);
??????
CString sProfile(_T("BarState"));
???????if (VerifyBarState(sProfile))
???????{
???????????m_wndMyBar.LoadState(sProfile);
???????????LoadBarState(sProfile);
???????}
?????? return 0;
}
注:紅色字體部分為所加內容。
6.?
在“查看”菜單下添加菜單項
mybar
,設其
id
為:
ID_VIEW_MYBAR.
7.?
為其添加
CMainFrame::OnViewMybar()
,
CMainFrame::OnUpdateViewMybar(CCmdUI* pCmdUI)
函數,內容為:
void CMainFrame::OnViewMybar()
{
?????? // TODO: Add your command handler code here
?????? BOOL bShow = m_wndMyBar.IsVisible();
?????? ShowControlBar(&m_wndMyBar, !bShow, FALSE);
}
void CMainFrame::OnUpdateViewMybar(CCmdUI* pCmdUI)
{
?????? // TODO: Add your command update UI handler code here
?????? pCmdUI->Enable();
?????? pCmdUI->SetCheck(m_wndMyBar.IsVisible());
}
編譯運行即可,如果你想改變
workshop
中的
cedit
成員變量(如本例
workshop
中的樹),只需改變
mybar
中的代碼即可。
下載連接:CMyBar.rar(http://www.shnenglu.com/zerolee/admin/Files.aspx)