加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

MFC 在对话框中使用CDialogBar

(2012-09-05 23:01:40)
标签:

mfc

cdialogbar

分类: VC
Technorati 标签: MFC,CDialogBar

在程序中使用CDialogBar能够将控件分组。对话框栏类似于一个容器,其中可以放置各种控件,就像一个面板。下面通过一个实例介绍CDialogBar的创建方法。

1.新建一个基于对话框的应用程序。

2.在资源视图中添加一个窗体资源。

窗体资源属性设置如图,与默认属性对比可知更改位置。(左边为默认属性,右边为更改过的)其中最重要的是Style要改为Child,否则会出错。

http://s2/middle/84024a4a4c8f5a6eb5a31&690

3.为改窗口添加一个新类。

基类选择CDialog,这是因为没有CDialogBar这个选项。添加好后,将头文件和源文件的CDialog替换为CDialogbar。构造函数也要更改,结果如下:

头文件:

#pragma once

// CCustomBar dialog

class CCustomBar : public CDialogBar
{
    DECLARE_DYNAMIC(CCustomBar)

public:
    CCustomBar(CWnd* pParent = NULL);   // standard constructor
    virtual ~CCustomBar();

// Dialog Data
    enum { IDD = IDD_DIALOG1 };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    DECLARE_MESSAGE_MAP()
};

 

源文件:

// CustomBar.cpp : implementation file
//

#include "stdafx.h"
#include "ex0725_03.h"
#include "CustomBar.h"

// CCustomBar dialog

IMPLEMENT_DYNAMIC(CCustomBar, CDialogBar)

CCustomBar::CCustomBar(CWnd* pParent )
{

}

CCustomBar::~CCustomBar()
{
}

void CCustomBar::DoDataExchange(CDataExchange* pDX)
{
    CDialogBar::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CCustomBar, CDialogBar)
END_MESSAGE_MAP()

// CCustomBar message handlers

 

5.在主窗口的OnInitDialog方法中添加CDialogBar的显示代码,代码如下:

    m_CustomBar.Create(this,IDD_DIALOG1,WS_CHILD  ,IDD_DIALOG1);

    CRect wndRC;
    GetClientRect(wndRC);
    m_CustomBar.MoveWindow(CRect(0,0,300,wndRC.Height()));

    return TRUE;  // return TRUE  unless you set the focus to a control

 

6.当主窗体改变大小时,对话框栏也改变大小

void Cex0725_03Dlg::OnSize(UINT nType, int cx, int cy)
{
    CDialog::OnSize(nType, cx, cy);

    // TODO: Add your message handler code here
    if (IsWindow(m_CustomBar.m_hWnd))
    {
        CRect wndRC,selfRC;
        GetClientRect(wndRC);
        m_CustomBar.GetWindowRect(selfRC);
        m_CustomBar.MoveWindow(CRect(0,0,selfRC.Width(),wndRC.Height()));
    }
}

 

注:主窗口默认是没法改变大小的,需要将属性栏的MaxmizeBox和MinmizeBox设置为True。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有