MFC的CDialogBar和Rollup Control

1.首先,创建对话框资源:在对话框资源编辑器内生成一个Dialog资源,并将其风格(Style)属性必须设置为Child,不能设置为Overlapped或Popup,否则运行肯定出错;至于边界属性则随用户自己喜欢,一般都是选择None。其余属性也随用户选择,一般没有特殊要求还是选择默认的好。

2. CMainFrame的头文件里添加CDialogBar m_wndDlgBar;

3. CMainFrame的实现文件里:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)函数里面添加:

BOOL bRet = m_wndDlgBar.Create(this, IDD_DIALOG1, WS_VISIBLE|WS_CHILD|CBRS_LEFT, 1);
m_wndDlgBar.EnableDocking(CBRS_ALIGN_RIGHT|CBRS_ALIGN_LEFT);
DockControlBar(&m_wndDlgBar);

就可以加入一个CDialogBar。

 

Rollup Control的来源:http://www.codeproject.com/Articles/1058/Rollup-Control

  1. RUNTIME_CLASS method

    Create a dialog box in the resource editor 'ex. IDD_PAGE1' with the WS_CHILD style. Generate the declaration and implementation files class (ex. CPage1Dlg) of your dialog box with the assistance of the MFC Classwizard. Then add DECLARE_DYNCREATE(CPage1Dlg) in the declaration of the class andIMPLEMENT_DYNCREATE(CPage1Dlg, CDialog) in the implementation.

    Then insert the page in this way pwndRollupCtrl->InsertPage("Caption", IDD_PAGE1, RUNTIME_CLASS(CPage1Dlg) );

  2. CDialog::Create method

    As RUNTIME_CLASS method, create a dialog box in the resource editor. Generate the declaration and implementation files class of your dialog box. Then create the dialog box like this

    CPage1Dlg* pwndPage1 = new CPage1Dlg;
    pwndPage1->Create(MAKEINTRESOURCE(IDD_PAGE1), pwndRollupCtrl);

    and insert the page as follows

    pwndRollupCtrl->InsertPage("Caption", pwndPage1 );

     

    改动在MainFrame.h MainFrame.cpp

     



转载于:https://www.cnblogs.com/kex1n/archive/2012/03/02/2377252.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是实现代码: 首先,我们需要创建一个自定义的对话框栏类,派生自CDialogBar: ``` class CMyDialogBar : public CDialogBar { DECLARE_DYNAMIC(CMyDialogBar) public: CMyDialogBar(); virtual ~CMyDialogBar(); protected: DECLARE_MESSAGE_MAP() private: // 用于存储按钮信息的结构体 struct ButtonInfo { CString text; // 按钮文本 COLORREF color; // 按钮背景颜色 }; std::vector<ButtonInfo> m_buttons; // 存储所有按钮信息的容器 public: // 添加一个按钮 void AddButton(const CString& text, COLORREF color); protected: virtual BOOL OnInitDialogBar(); virtual void DoDataExchange(CDataExchange* pDX); virtual void OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler); afx_msg void OnPaint(); }; ``` 在该类中,我们使用了一个vector容器m_buttons来存储所有按钮的信息,每个按钮信息包括文本和背景颜色。对外提供一个AddButton方法,用于添加一个按钮。 在类的实现文件中,我们需要实现AddButton方法: ``` void CMyDialogBar::AddButton(const CString& text, COLORREF color) { ButtonInfo info = { text, color }; m_buttons.push_back(info); } ``` 接下来,我们需要实现OnInitDialogBar方法,该方法在对话框栏创建时被调用。在该方法中,我们需要根据传入的按钮数量和信息,创建对应数量的按钮,并设置按钮的文本和背景颜色: ``` BOOL CMyDialogBar::OnInitDialogBar() { if (!CDialogBar::OnInitDialogBar()) return FALSE; // 计算按钮宽度 CRect rect; GetClientRect(&rect); int buttonWidth = rect.Width() / m_buttons.size(); // 创建每个按钮 for (int i = 0; i < m_buttons.size(); ++i) { CButton* button = new CButton(); button->Create(m_buttons[i].text, WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_OWNERDRAW, CRect(i * buttonWidth, 0, (i + 1) * buttonWidth, rect.Height()), this, 0); // 设置按钮背景颜色 CBrush brush(m_buttons[i].color); button->SetFace(&brush, TRUE); } return TRUE; } ``` 在该方法中,我们首先计算每个按钮的宽度,然后循环创建每个按钮,设置按钮的位置、文本和背景颜色。 接下来,我们需要实现OnPaint方法,该方法在对话框栏需要重绘时被调用。在该方法中,我们需要绘制每个按钮的边框: ``` void CMyDialogBar::OnPaint() { CPaintDC dc(this); // device context for painting // 绘制按钮边框 CRect rect; GetClientRect(&rect); int buttonWidth = rect.Width() / m_buttons.size(); for (int i = 0; i < m_buttons.size() - 1; ++i) { dc.MoveTo((i + 1) * buttonWidth, 0); dc.LineTo((i + 1) * buttonWidth, rect.Height()); } } ``` 最后,我们需要实现DoDataExchange和OnUpdateCmdUI方法,以便在需要更新控件状态时正确响应: ``` void CMyDialogBar::DoDataExchange(CDataExchange* pDX) { CDialogBar::DoDataExchange(pDX); } void CMyDialogBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler) { CDialogBar::OnUpdateCmdUI(pTarget, bDisableIfNoHndler); } ``` 至此,我们的自定义对话框栏类CMyDialogBar就完成了。完整代码如下: ``` // MyDialogBar.h #pragma once #include <vector> class CMyDialogBar : public CDialogBar { DECLARE_DYNAMIC(CMyDialogBar) public: CMyDialogBar(); virtual ~CMyDialogBar(); protected: DECLARE_MESSAGE_MAP() private: // 用于存储按钮信息的结构体 struct ButtonInfo { CString text; // 按钮文本 COLORREF color; // 按钮背景颜色 }; std::vector<ButtonInfo> m_buttons; // 存储所有按钮信息的容器 public: // 添加一个按钮 void AddButton(const CString& text, COLORREF color); protected: virtual BOOL OnInitDialogBar(); virtual void DoDataExchange(CDataExchange* pDX); virtual void OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler); afx_msg void OnPaint(); }; // MyDialogBar.cpp #include "stdafx.h" #include "MyDialogBar.h" IMPLEMENT_DYNAMIC(CMyDialogBar, CDialogBar) CMyDialogBar::CMyDialogBar() { } CMyDialogBar::~CMyDialogBar() { } BEGIN_MESSAGE_MAP(CMyDialogBar, CDialogBar) ON_WM_PAINT() END_MESSAGE_MAP() void CMyDialogBar::AddButton(const CString& text, COLORREF color) { ButtonInfo info = { text, color }; m_buttons.push_back(info); } BOOL CMyDialogBar::OnInitDialogBar() { if (!CDialogBar::OnInitDialogBar()) return FALSE; // 计算按钮宽度 CRect rect; GetClientRect(&rect); int buttonWidth = rect.Width() / m_buttons.size(); // 创建每个按钮 for (int i = 0; i < m_buttons.size(); ++i) { CButton* button = new CButton(); button->Create(m_buttons[i].text, WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_OWNERDRAW, CRect(i * buttonWidth, 0, (i + 1) * buttonWidth, rect.Height()), this, 0); // 设置按钮背景颜色 CBrush brush(m_buttons[i].color); button->SetFace(&brush, TRUE); } return TRUE; } void CMyDialogBar::OnPaint() { CPaintDC dc(this); // device context for painting // 绘制按钮边框 CRect rect; GetClientRect(&rect); int buttonWidth = rect.Width() / m_buttons.size(); for (int i = 0; i < m_buttons.size() - 1; ++i) { dc.MoveTo((i + 1) * buttonWidth, 0); dc.LineTo((i + 1) * buttonWidth, rect.Height()); } } void CMyDialogBar::DoDataExchange(CDataExchange* pDX) { CDialogBar::DoDataExchange(pDX); } void CMyDialogBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler) { CDialogBar::OnUpdateCmdUI(pTarget, bDisableIfNoHndler); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值