CBCGPDockingControlBar停靠窗口的创建及其位置关系

在MS office、Visual Studio等各类应用中我们都能见到停靠窗口的身影。本文将介绍如何使用CBCGPDockingControlBar创建停靠窗口以及不同停靠窗口之间的位置关系调整。

1.根据BCGControlBar Pro 应用程序向导创建一个程序

  • 如何创建一个基于BCG的应用程序,本文不再赘述,详情请参考:Application Wizard

2.添加一个类

  • 使用类向导或手动添加一个继承CBCGPDockingControlBar的类,并添加WN_CREATE、WM_SIZE消息处理函数。下面的代码创建了一个包含ListCtrl的停靠窗口。
//CompileInfoBar.h
class CCompileInfoBar : public CBCGPDockingControlBar
{
public:
    CCompileInfoBar();
// Attributes
protected:
    CBCGPListCtrl m_wndList;
// Operations
public:
// Overrides
// Implementation
public:
    virtual ~CCompileInfoBar();
// Generated message map functions
protected:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnSize(UINT nType, int cx, int cy);
    DECLARE_MESSAGE_MAP()
};
//CompileInfoBar .cpp
/////////////////////////////////////////////////////////////////////////////
// CCompileInfoBar

BEGIN_MESSAGE_MAP(CCompileInfoBar, CBCGPDockingControlBar)
    ON_WM_CREATE()
    ON_WM_SIZE()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompileInfoBar construction/destruction

CCompileInfoBar::CCompileInfoBar()
{
    // TODO: add one-time construction code here
}

CCompileInfoBar::~CCompileInfoBar()
{
}

/////////////////////////////////////////////////////////////////////////////
// CCompileInfoBar message handlers

int CCompileInfoBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CBCGPDockingControlBar::OnCreate(lpCreateStruct) == -1)
        return -1;

        CRect rectDummy;
    rectDummy.SetRectEmpty();
    // Create list windows:
    const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL;

    m_wndList.Create(dwViewStyle, rectDummy, this, 1);

    return 0;
}

void CCompileInfoBar::OnSize(UINT nType, int cx, int cy) 
{
    CBCGPDockingControlBar::OnSize(nType, cx, cy);
    m_wndList.SetWindowPos(NULL, -1, -1, cx, cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}

3. 在MainFram中创建

  • 首先在CMainFrame中添加成员变量CCompileInfoBar m_wndCompileInfo;,接着在CMainFrame::OnCreate中创建:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CBCGPMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    //其余初始化代码省略.....
        const int nOutputPaneSize = globalUtils.ScaleByDPI(170);
    //CBRS_BOTTOM指定了停靠窗口将出现在View的下方。同理如设置为CBRS_LEFT、CBRS_RIGHT、CBRS_TOP将分别出现在左、右、上。
    if (!m_wndCompileInfo.Create(_T("编译信息"), this, CSize(nOutputPaneSize, nOutputPaneSize),
        TRUE /* Has gripper */, ID_VIEW_OUTPUT,
        WS_CHILD | WS_VISIBLE | CBRS_BOTTOM))
    {
        TRACE0("Failed to create Compile Info bar\n");
        return -1;      // fail to create
    }
    //允许其他窗口停靠
    m_wndCompileInfo.EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndCompileInfo);
}

4.多停靠窗口之间位置调整

  • 在实际应用中,往往存在多个停靠窗口,各个停靠窗口之间又存在多种位置关系。

4.1 将停靠窗口附加到另一个停靠窗口

  • 代码如下
//...省略创建代码
m_wndCompileInfo.EnableDocking(CBRS_ALIGN_ANY);
m_wndRunInfo.EnableDocking(CBRS_ALIGN_ANY);
m_wndMoitor.EnableDocking(CBRS_ALIGN_ANY);
m_wndSearchResult.EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndCompileInfo);
m_wndRunInfo.AttachToTabWnd(&m_wndCompileInfo, BCGP_DM_STANDARD, FALSE, NULL);
m_wndMoitor.AttachToTabWnd(&m_wndCompileInfo, BCGP_DM_STANDARD, FALSE, NULL);
m_wndSearchResult.AttachToTabWnd(&m_wndCompileInfo, BCGP_DM_STANDARD, FALSE, NULL);
  • 效果图,四个窗口将附加在一起
    附加显示

4.2 停靠窗口水平/垂直并行显示

  • 代码如下:
    m_wndRunInfo.EnableDocking(CBRS_ALIGN_ANY);
    m_wndCompileInfo.EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndCompileInfo);
    m_wndRunInfo.DockToWindow(&m_wndCompileInfo, CBRS_RIGHT, 0);
  • 效果如下,两个窗口水平并行显示;调整CBRS_RIGHT为CBRS_BOTTOM将垂直方向并行显示。
    并行显示

最后欢迎大家在评论区批评指正。

扫描关注程序区

有任何问题,请联系邮箱:support@progdomain.com

THE END
分享
二维码
打赏
海报
CBCGPDockingControlBar停靠窗口的创建及其位置关系
在MS office、Visual Studio等各类应用中我们都能见到停靠窗口的身影。本文将介绍如何使用CBCGPDockingControlBar创建停靠窗口以及不同停靠窗口之间的位置……
<<上一篇
下一篇>>
文章目录
关闭
目 录