我试过可以实现 step1: 在资源编辑器中插入工具条资源,并为每个按钮创建ID。将它命名为IDC_TOOLBAR1 step2: 在对话框变量中添加一个工具条变量。 CToolBar m_wndToolBar;step3: 在CDialog::OnInitDialog中添加如下代码: // 创建工具条并调入资源 if(!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1)) { TRACE0("Failed to Create Dialog Toolbar\n"); EndDialog(IDCANCEL); }CRect rcClientOld; // 久客户区RECT CRect rcClientNew; // 加入TOOLBAR后的CLIENT RECT GetClientRect(rcClientOld); // // Called to reposition and resize control bars in the client area of a window // The reposQuery FLAG does not really traw the Toolbar. It only does the calculations. // And puts the new ClientRect values in rcClientNew so we can do the rest of the Math. //重新计算RECT大小 RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rcClientNew);// All of the Child Windows (Controls) now need to be moved so the Tollbar does not cover them up. //所有的子窗口将被移动,以免被TOOLBAR覆盖 // Offest to move all child controls after adding Tollbar //计算移动的距离 CPoint ptOffset(rcClientNew.left-rcClientOld.left, rcClientNew.top-rcClientOld.top);CRect rcChild; CWnd* pwndChild = GetWindow(GW_CHILD); //得到子窗口 while(pwndChild) // 处理所有子窗口 {//移动所有子窗口 pwndChild->GetWindowRect(rcChild); ScreenToClient(rcChild); rcChild.OffsetRect(ptOffset); pwndChild->MoveWindow(rcChild,FALSE); pwndChild = pwndChild->GetNextWindow(); }CRect rcWindow; GetWindowRect(rcWindow); // 得到对话框RECT rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // 修改对话框尺寸 rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height(); MoveWindow(rcWindow,FALSE); // Redraw WindowRepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
http://www.vckbase.com/code/downcode.asp?id=1750
step1:
在资源编辑器中插入工具条资源,并为每个按钮创建ID。将它命名为IDC_TOOLBAR1 step2:
在对话框变量中添加一个工具条变量。
CToolBar m_wndToolBar;step3:
在CDialog::OnInitDialog中添加如下代码:
// 创建工具条并调入资源
if(!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to Create Dialog Toolbar\n");
EndDialog(IDCANCEL);
}CRect rcClientOld; // 久客户区RECT
CRect rcClientNew; // 加入TOOLBAR后的CLIENT RECT
GetClientRect(rcClientOld); //
// Called to reposition and resize control bars in the client area of a window
// The reposQuery FLAG does not really traw the Toolbar. It only does the calculations.
// And puts the new ClientRect values in rcClientNew so we can do the rest of the Math.
//重新计算RECT大小
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rcClientNew);// All of the Child Windows (Controls) now need to be moved so the Tollbar does not cover them up.
//所有的子窗口将被移动,以免被TOOLBAR覆盖
// Offest to move all child controls after adding Tollbar
//计算移动的距离
CPoint ptOffset(rcClientNew.left-rcClientOld.left,
rcClientNew.top-rcClientOld.top);CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD); //得到子窗口
while(pwndChild) // 处理所有子窗口
{//移动所有子窗口
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild,FALSE);
pwndChild = pwndChild->GetNextWindow();
}CRect rcWindow;
GetWindowRect(rcWindow); // 得到对话框RECT
rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // 修改对话框尺寸
rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height();
MoveWindow(rcWindow,FALSE); // Redraw WindowRepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
用 ClassWizard 生成基于 CMiniFrameWnd的类,基于Cstatic的类,便可实现工具栏停靠之类的操作
相关代码:#if !defined(AFX_MYTOOLBAR_H__A2B92720_2EC8_42BC_A8C4_B300AA6C93D2__INCLUDED_)
#define AFX_MYTOOLBAR_H__A2B92720_2EC8_42BC_A8C4_B300AA6C93D2__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyToolBar.h : header file
///////////////////////////////////////////////////////////////////////////////
// CMyToolBar window
#include "afxext.h"class CMyToolBar : public CToolBar
{
// Construction
public:
CMyToolBar();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyToolBar)
//}}AFX_VIRTUAL// Implementation
public:
void SetState(void);
virtual ~CMyToolBar(); // Generated message map functions
protected:
//{{AFX_MSG(CMyToolBar)
afx_msg void OnWindowPosChanged(WINDOWPOS FAR* lpwndpos);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif// MyToolBar.cpp : implementation file
//#include "stdafx.h"
#include "DockBar.h"
#include "MyToolBar.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMyToolBarCMyToolBar::CMyToolBar()
{
}CMyToolBar::~CMyToolBar()
{
}
BEGIN_MESSAGE_MAP(CMyToolBar, CToolBar)
//{{AFX_MSG_MAP(CMyToolBar)
ON_WM_WINDOWPOSCHANGED()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyToolBar message handlersvoid CMyToolBar::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos)
{
CToolBar::OnWindowPosChanged(lpwndpos); // TODO: Add your message handler code here
SetState();
}void CMyToolBar::SetState()
{
UINT nDockState = GetParent()->GetDlgCtrlID();
this->GetToolBarCtrl().CheckButton(ID_FLOAT,nDockState == AFX_IDW_DOCKBAR_FLOAT);
this->GetToolBarCtrl().CheckButton(ID_UPDOCK,nDockState == AFX_IDW_DOCKBAR_TOP);
this->GetToolBarCtrl().CheckButton(ID_DOWNDOCK,nDockState == AFX_IDW_DOCKBAR_BOTTOM);
this->GetToolBarCtrl().CheckButton(ID_LEFTDOCK,nDockState == AFX_IDW_DOCKBAR_LEFT);
this->GetToolBarCtrl().CheckButton(ID_RIGHTDOCK,nDockState == AFX_IDW_DOCKBAR_RIGHT);
}
#define AFX_TOOLFRM_H__41192A15_AFA3_4621_8FDB_9FBFC455721B__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ToolFrm.h : header file
///////////////////////////////////////////////////////////////////////////////
// CToolFrm frame
#include "MyToolBar.h"class CToolFrm : public CMiniFrameWnd
{
DECLARE_DYNCREATE(CToolFrm)
protected:// Attributes
public:
CToolFrm(); // protected constructor used by dynamic creation
virtual ~CToolFrm();// Operations
public:
BOOL IsFloating(void);
void ShowToolBar(BOOL bShow);// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CToolFrm)
public:
//}}AFX_VIRTUAL// Implementation
protected: // Generated message map functions
//{{AFX_MSG(CToolFrm)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnFloat();
afx_msg void OnLeftdock();
afx_msg void OnUpdock();
afx_msg void OnRightdock();
afx_msg void OnDowndock();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CMyToolBar m_wndToolBar;
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif
// ToolFrm.cpp : implementation file
//#include "stdafx.h"
#include "DockBar.h"
#include "ToolFrm.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CToolFrmIMPLEMENT_DYNCREATE(CToolFrm, CMiniFrameWnd)CToolFrm::CToolFrm()
{
}CToolFrm::~CToolFrm()
{
}
BEGIN_MESSAGE_MAP(CToolFrm, CMiniFrameWnd)
//{{AFX_MSG_MAP(CToolFrm)
ON_WM_CREATE()
ON_COMMAND(ID_FLOAT, OnFloat)
ON_COMMAND(ID_LEFTDOCK, OnLeftdock)
ON_COMMAND(ID_UPDOCK, OnUpdock)
ON_COMMAND(ID_RIGHTDOCK, OnRightdock)
ON_COMMAND(ID_DOWNDOCK, OnDowndock)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CToolFrm message handlersint CToolFrm::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMiniFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
if (!m_wndToolBar.Create(this) ||
!m_wndToolBar.LoadToolBar(IDR_TOOLBAR))
{
AfxMessageBox(_T("创建工具条失败!"));
return -1;
}
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_BORDER_ANY |
CBRS_SIZE_DYNAMIC | CBRS_GRIPPER); m_wndToolBar.ShowWindow(SW_SHOW);
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar,AFX_IDW_DOCKBAR_TOP);
RecalcLayout(); return 0;
}void CToolFrm::ShowToolBar(BOOL bShow)
{
ShowControlBar(&m_wndToolBar,bShow,FALSE);
}BOOL CToolFrm::IsFloating()
{
return m_wndToolBar.IsFloating();
}void CToolFrm::OnFloat()
{
// TODO: Add your command handler code here
this->FloatControlBar(&m_wndToolBar,CPoint(400,400));
}void CToolFrm::OnLeftdock()
{
// TODO: Add your command handler code here
this->DockControlBar(&m_wndToolBar,AFX_IDW_DOCKBAR_LEFT);
this->RecalcLayout();
}void CToolFrm::OnUpdock()
{
// TODO: Add your command handler code here
this->DockControlBar(&m_wndToolBar,AFX_IDW_DOCKBAR_TOP);
this->RecalcLayout();
}void CToolFrm::OnRightdock()
{
// TODO: Add your command handler code here
this->DockControlBar(&m_wndToolBar,AFX_IDW_DOCKBAR_RIGHT);
this->RecalcLayout();
}void CToolFrm::OnDowndock()
{
// TODO: Add your command handler code here
this->DockControlBar(&m_wndToolBar,AFX_IDW_DOCKBAR_BOTTOM);
this->RecalcLayout();
}
#define AFX_DOCKSTATIC_H__C08511E5_1A0F_4FA9_B4CB_2957858B641A__INCLUDED_#include "ToolFrm.h" // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// DockStatic.h : header file
///////////////////////////////////////////////////////////////////////////////
// CDockStatic windowclass CDockStatic : public CStatic
{
// Construction
public:
CDockStatic();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDockStatic)
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL// Implementation
public:
CToolFrm *GetToolFrame();
virtual ~CDockStatic(); // Generated message map functions
protected:
//{{AFX_MSG(CDockStatic)
//}}AFX_MSG DECLARE_MESSAGE_MAP()
private:
CToolFrm *m_pToolFrm;
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif
// DockStatic.cpp : implementation file
//#include "stdafx.h"
#include "DockBar.h"
#include "DockStatic.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CDockStaticCDockStatic::CDockStatic()
{
m_pToolFrm = new CToolFrm;
}CDockStatic::~CDockStatic()
{
}
BEGIN_MESSAGE_MAP(CDockStatic, CStatic)
//{{AFX_MSG_MAP(CDockStatic)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CDockStatic message handlersvoid CDockStatic::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CStatic::PreSubclassWindow(); // 隐藏占位控件
this->ShowWindow(SW_HIDE);
RECT rect;
this->GetWindowRect(&rect);
this->ScreenToClient(&rect);
// 创建工具栏停靠的框架,它的父窗体是对话框而不是这个静态控件
m_pToolFrm->Create(AfxRegisterWndClass(0),_T(""),
WS_VISIBLE|WS_CHILD,rect,this->GetParent());
}CToolFrm * CDockStatic::GetToolFrame()
{
return m_pToolFrm;
}
一。mediaplayer没有分隔线
二。mediaplayer没有那个可以拖动的竖线它是怎么实现的呢?