在子框架内创建CBCGPDialogBar

           在多文档应用程序的子框架内创建停靠对话框栏等,参考BCG示例MDIBars可以达到要求。以下测试在 BCGControlBar12版本上进行。
1.新建一个BCG多文档工程,名称:TestMDIBars;
2.选择" 基于Ribbon样式的界面",如下图所示:


3.在" 资源视图"添加一个对话框资源,名称: IDD_DIALOG_BAR,设置其属性Border为None,Clip Children为True,Clip Siblings为True,Style为Child,System Menu为False,Visible为True,在上面放置一个Button,和一个Edit;
4.在" ChildFrm.h"头文件,添加如下声明:
1
CBCGPDialogBar m_wndDialogBar;
5.重载CChildFrame的 WM_CREATE消息,添加如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#define IDW_DIALOGBAR  161 

int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
     if (CBCGPMDIChildWnd::OnCreate(lpCreateStruct) == - 1)
         return - 1;

     if (!m_wndDialogBar.Create (_T( "Dialog Bar"), 
                                 this
                                TRUE, 
                                MAKEINTRESOURCE (IDD_DIALOG_BAR), 
                                WS_VISIBLE | WS_CHILD | CBRS_RIGHT, 
                                IDW_DIALOGBAR))
    {
        TRACE0( "Failed to create dialogbar\n");
         return - 1;     
    }

    EnableDocking(CBRS_ALIGN_ANY);                 //让bar能够在任何边界停靠
    EnableAutoHideBars(CBRS_ALIGN_RIGHT);         //让右边bar能够自动隐藏
    GetDockManager()->EnableDockBarMenu(TRUE);     //让bar有下拉箭头菜单
    m_wndDialogBar.EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndDialogBar);

     return  0;
}
6.对对话框上的按钮,添加事件处理,选择添加到" CChildFrame"中,按钮点击事件代码如下:
1
2
3
4
void CChildFrame::OnBnClickedButton1() 
{
    AfxMessageBox(_T( "测试"));
}
7.编译运行,如下图所示:

当对话框栏大小不足时,会出现滚动条,如下图所示:

可以用如下代码去除滚动条:
1
    m_wndDialogBar.EnableScrolling(FALSE);
若是需要动态显示或隐藏,可以用如下代码:
1
2
    m_wndDialogBar.ShowControlBar(FALSE, FALSE, FALSE);
    m_wndDialogBar.ShowControlBar(TRUE, FALSE, FALSE);

下面简单模拟下VS的输出窗口,设置对话框编辑框的属性Multiline为True,Want Return为True,Horizontal Scroll为True,Vertical Scroll为True,界面布局如下所示:

为对话框添加类,类代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once

//
// COutputDialogBar 对话框

class COutputDialogBar :  public CBCGPDialogBar
{
    DECLARE_DYNAMIC(COutputDialogBar)

public:
    COutputDialogBar();  
     virtual ~COutputDialogBar();

// 对话框数据
     enum { IDD = IDD_DIALOG_BAR };
    CBCGPEdit m_edtOutput;
protected:
     virtual  void DoDataExchange(CDataExchange* pDX);     // DDX/DDV 支持
    afx_msg LRESULT HandleInitDialog(WPARAM, LPARAM);
    DECLARE_MESSAGE_MAP()
};
实现文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include  "stdafx.h"
#include  "TestMDIBars.h"
#include  "OutputDialogBar.h"

//
// COutputDialogBar 对话框

IMPLEMENT_DYNAMIC(COutputDialogBar, CBCGPDialogBar)

COutputDialogBar::COutputDialogBar()    
{
    EnableLayout();
    EnableScrolling(FALSE);
}

COutputDialogBar::~COutputDialogBar()
{
}

void COutputDialogBar::DoDataExchange(CDataExchange* pDX)
{
    CBCGPDialogBar::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT1, m_edtOutput);
}


BEGIN_MESSAGE_MAP(COutputDialogBar, CBCGPDialogBar)
    ON_MESSAGE(WM_INITDIALOG, HandleInitDialog)
END_MESSAGE_MAP()

LRESULT COutputDialogBar::HandleInitDialog( WPARAM wParam, LPARAM lParam )
{
    LRESULT lRes = CBCGPDialogBar::HandleInitDialog(wParam, lParam);

    CBCGPStaticLayout *pLayout = (CBCGPStaticLayout*)GetLayout();
     if (pLayout !=  NULL)
    {
        pLayout->AddAnchor(m_edtOutput, CBCGPStaticLayout::e_MoveTypeNone, CBCGPStaticLayout::e_SizeTypeBoth);
        pLayout->AddAnchor(IDC_BUTTON1, CBCGPStaticLayout::e_MoveTypeNone,  CBCGPStaticLayout::e_SizeTypeNone);
    }

     return lRes;
}
注意基类为 CBCGPDialogBar,在 ChildFrm.h文件,添加如下代码:
1
2
3
4
5
6
7
8
9
10
#include  "OutputDialogBar.h" 

class CChildFrame :  public CBCGPMDIChildWnd
{
public:
    COutputDialogBar m_wndDialogBar;
     void SetDialogBarSize(UINT nSize);
    afx_msg  int OnCreate(LPCREATESTRUCT lpCreateStruct);
     //........
}
ChildFrm.cpp文件,添加如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
     if (CBCGPMDIChildWnd::OnCreate(lpCreateStruct) == - 1)
         return - 1;

     if (!m_wndDialogBar.Create (_T( "Dialog Bar"), 
                                 this
                                TRUE, 
                                MAKEINTRESOURCE (IDD_DIALOG_BAR), 
                                WS_VISIBLE | WS_CHILD | CBRS_BOTTOM, 
                                IDW_DIALOGBAR))
    {
        TRACE0( "Failed to create dialogbar\n");
         return - 1;     
    }

    EnableDocking(CBRS_ALIGN_BOTTOM);
    EnableAutoHideBars(CBRS_ALIGN_BOTTOM);
    GetDockManager()->EnableDockBarMenu(TRUE);
    m_wndDialogBar.EnableDocking(CBRS_ALIGN_BOTTOM);
    DockControlBar(&m_wndDialogBar);
    SetDialogBarSize( 170);
    m_wndDialogBar.ShowControlBar(FALSE, FALSE, FALSE);

     return  0;
}

void CChildFrame::SetDialogBarSize(UINT nSize)
{
    CBCGPSlider *pDefaultSlider = m_wndDialogBar.GetDefaultSlider();
     if (pDefaultSlider ==  NULL)
    {
         return;
    }

    BOOL bLeftBar = FALSE;
    CBCGPBarContainer *pContainer = pDefaultSlider->FindContainer(&m_wndDialogBar, bLeftBar);
     while (pContainer->GetParentContainer() !=  NULL)
    {
        pContainer = pContainer->GetParentContainer();
    }

    CRect rectContainer;
    pContainer->GetWindowRect(rectContainer, FALSE);

    DWORD dwSliderStyle = pDefaultSlider->GetCurrentAlignment();
    CPoint ptOffset( 00);
     switch (dwSliderStyle)
    {
     case CBRS_ALIGN_TOP:
        ptOffset.y = nSize - rectContainer.Height();
         break;
     case CBRS_ALIGN_BOTTOM:
        ptOffset.y = rectContainer.Height() - nSize;
         break;
     case CBRS_ALIGN_LEFT:
        ptOffset.x = nSize - rectContainer.Width();
         break;
     case CBRS_ALIGN_RIGHT:
        ptOffset.x = rectContainer.Width() - nSize;
         break;
    }
    pDefaultSlider->MoveSlider(ptOffset);
}
在视图类测试对Bar的显示,代码如下所示:
1
2
3
4
5
6
7
8
void CTestMDIBarsView::OnTestbutton() 
{
    CChildFrame *pParentFrame = ((CChildFrame *)GetParentFrame());
     if (pParentFrame !=  NULL)
    {
        pParentFrame->m_wndDialogBar.ShowControlBar(TRUE, FALSE, FALSE);
    }
}
运行之后,结果如下图所示:


拉动窗体大小后,如图所示:


另外也可以直接派生CBCGPDockingControlBar,类似如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once

//
// COutputViewBar

class COutputViewBar :  public CBCGPDockingControlBar
{
public:
    COutputViewBar();
     virtual ~COutputViewBar();

protected:
    CBCGPEdit m_edtOutput;
    DECLARE_MESSAGE_MAP()
    afx_msg  int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg  void OnSize(UINT nType,  int cx,  int cy);
    afx_msg  void OnPaint();
    afx_msg  void OnSetFocus(CWnd* pOldWnd);
};
实现文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include  "stdafx.h"
#include  "OutputViewBar.h"

//
// COutputViewBar

COutputViewBar::COutputViewBar()
{

}

COutputViewBar::~COutputViewBar()
{
}

BEGIN_MESSAGE_MAP(COutputViewBar, CBCGPDockingControlBar)
    ON_WM_CREATE()
    ON_WM_SIZE()
    ON_WM_PAINT()
    ON_WM_SETFOCUS()
END_MESSAGE_MAP()

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

    CRect rectClient( 00, lpCreateStruct->cx, lpCreateStruct->cy);
     const DWORD dwStyle =  ES_MULTILINE | ES_AUTOHSCROLL | ES_WANTRETURN | WS_VSCROLL | WS_HSCROLL | WS_CHILD | WS_VISIBLE;

     if (!m_edtOutput.Create(dwStyle, rectClient,  this1))
    {
        TRACE0( "Failed to create output window\n");
         return - 1;
    }
    m_edtOutput.SetFont(&globalData.fontRegular);

     return  0;
}

void COutputViewBar::OnSize(UINT nType,  int cx,  int cy)
{
    CBCGPDockingControlBar::OnSize(nType, cx, cy);

    CRect rc;
    GetClientRect(rc);
    m_edtOutput.SetWindowPos( NULL11, rc.Width() -  2, rc.Height() -  2, SWP_NOACTIVATE | SWP_NOZORDER);
}

void COutputViewBar::OnPaint()
{
    CPaintDC dc( this);
    CRect rectEdit;
    m_edtOutput.GetWindowRect(rectEdit);
    ScreenToClient(rectEdit);

    rectEdit.InflateRect( 11);
    dc.Draw3dRect(rectEdit, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
}

void COutputViewBar::OnSetFocus(CWnd* pOldWnd)
{
    CBCGPDockingControlBar::OnSetFocus(pOldWnd);
    m_edtOutput.SetFocus();
}
调用如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
     if (CBCGPMDIChildWnd::OnCreate(lpCreateStruct) == - 1)
         return - 1;

     if (!m_wndOutput.Create(_T( "输出"),  this, CRect( 00200100), TRUE, ID_OUTPUT_BAR,
        WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_BOTTOM))
    {
        TRACE(_T( "Failed to create output bar\n"));
         return FALSE;
    }

    EnableDocking(CBRS_ALIGN_ANY);
    EnableAutoHideBars(CBRS_ALIGN_ANY);
    GetDockManager()->EnableDockBarMenu(TRUE);
    m_wndOutput.EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndOutput);
    
     return  0;
}



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值