如何让DialogBar支持大小变动

两种方法:

重载CalcDynamicLayout或CalcFixedLayout

 

SUMMARY
  The default MFC implementation of CDialogBar does not allow for the dialog bar to be resizable when it is floating. If the dialog bar contains controls that are sensibly resized, the programmer may want to make the dialog bar resizable by using the techique described in this article.  
  To make the dialog bar resizable when using versions of MFC earlier than version 4.0:  
  Change the style of the mini frame window enclosing the floating dialog bar so that it is resizable.  
  Override CDialogBar to provide a CalcFixedLayout, which allows the dialog bar and its controls to be resized.  
  To make the dialog bar resizable when using MFC version 4.0 or later, you can use the built-in support for resizable control bars. However, dialog bars do not get this behavior by default. To make a dialog bar resizable:  
  Use the new CBRS_SIZE_DYNAMIC style when creating the dialog bar.  
  Add code to an override of the new CalcDynamicLayout() function.
MORE INFORMATION
  For versions of MFC Earlier Than Version 4.0
  CDialogBar contains an embedded member variable m_sizeDefault of type CSize. On creation, m_sizeDefault is set to the size of the dialog template embedded in the dialog bar. Whenever the dialog bar is resized or repositioned, MFC calls CDialogBar::CalcFixedLayout(). This member function is intended to return the appropriate size of the control bar; for CDialogBar, CalcFixedLayout() usually returns m_sizeDefault. This causes the dialog bar to always snap back to its default size.  
  To create a resizable dialog bar, you must ensure that the following things are handled in some manner by an application:  
  The CMiniDockFrameWnd surrounding the floating dialog bar must have its window styles changed so that it is resizable.  
  A member variable must be added to a class derived from CDialogBar to represent the appropriate floating size.  
  CalcFixedLayout() must be overridden in this class to set and return the floating size of the dialog bar when the dialog bar is being resized.  
  With the appropriate tests, both 1 and 3 can be implemented in CalcFixedLayout(). See the "Sample Code" section of this article for an example of this.  
  For MFC Version 4.0 and Later
  In MFC 4.0 and higher, control bars support the new CBRS_SIZE_DYNAMIC style. CBRS_SIZE_DYNAMIC allows a floating control bar to be dynamically resized when the user drags the control bar's border. The virtual function CControlBar::CalcDynamicLayout() has been added to determine the size that a control bar should be resized to.  
  CalcDynamicLayout() is called for CBRS_SIZE_DYNAMIC control bars when the border of a floating control bar is dragged and when the control bar is docked or floated. The default CControlBar implementation simply calls CalcFixedLayout(), which prevents control bar objects from resizing unless CalcDynamicLayout() is overridden. CDialogBar does not override CalcDynamicLayout(), so by default, dialog bars are not resizable.  
  Therefore, to make a resizable dialog bar using MFC 4.0:  
  Create a new class derived from CDialogBar and override the CalcDynamicLayout() function. Depending on the behavior you want, it may be necessary to add a member variable to the class.  
  Create an instance of this class using the CBRS_SIZE_DYNAMIC style bit. Dialog bars are typically created in CMainFrame::OnCreate():  
 
      if (!m_wndDialogBar.Create(this, IDD_DIALOGBAR, CBRS_TOP | CBRS_SIZE_DYNAMIC, 777))
      {
        TRACE0("Failed to create dialogbar/n");
        return -1;
      }
 
      m_wndDialogBar.EnableDocking(CBRS_ALIGN_ANY);
      DockControlBar(&m_wndDialogBar);  

 
  See the "Sample Code" section in this article for a examples of this.  
  SAMPLE CODE
  Sample Code for MFC versions Prior to Visual C++ 4.0
 
    /* Compile options needed: Default
    */  
 
    // RSZDLGBR.H : header file
    //  
 
    class CResizableDlgBar : public CDialogBar
    {
    // Construction
    public:
      BOOL Create( CWnd* pParentWnd, UINT nIDTemplate, UINT nStyle,
              UINT nID, BOOL = TRUE);
      BOOL Create( CWnd* pParentWnd, LPCTSTR lpszTemplateName,
            UINT nStyle, UINT nID, BOOL = TRUE);
 
    // Attributes
    public:
      CSize m_sizeDocked;
      CSize m_sizeFloating;
      BOOL m_bChangeDockedSize;   // Indicates whether to keep
                        // a default size for docking
 
    // Operations
    public:
 
    // Overrides
      virtual CSize CalcFixedLayout(BOOL bStretch, BOOL bHorz);
 
    // Implementation
    public:
    };
 
    /  
 
    // RSZDLGBR.CPP : implementation file
    //  
 
    #include "stdafx.h"
    #include <afxpriv.h>
    #include "ResizableDlgBar.h"
    #include "resource.h"
 
      
    // CResizableDlgBar Construction/Destruction
 
    BOOL CResizableDlgBar::Create( CWnd* pParentWnd, UINT nIDTemplate,
                        UINT nStyle, UINT nID, BOOL bChange)
    {
      if(!CDialogBar::Create(pParentWnd,nIDTemplate,nStyle,nID))
          return FALSE;
 
      m_bChangeDockedSize = bChange;
      m_sizeFloating = m_sizeDocked = m_sizeDefault;
      return TRUE;
    }
 
    BOOL CResizableDlgBar::Create( CWnd* pParentWnd,
                        LPCTSTR lpszTemplateName,
                        UINT nStyle, UINT nID, BOOL bChange)
    {
      if (!CDialogBar::Create( pParentWnd, lpszTemplateName,
                      nStyle, nID))
          return FALSE;
 
      m_bChangeDockedSize = bChange;
      m_sizeFloating = m_sizeDocked = m_sizeDefault;
      return TRUE;
    }
 
      
    // Overloaded functions
 
 
    CSize CResizableDlgBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
    {
      if (IsFloating())
      {
        // Get the parent mini frame wnd
        CMiniDockFrameWnd* pFrame =
          ((CMiniDockFrameWnd*)m_pDockBar->GetParent());
        CRect rcFrame;
        pFrame->GetClientRect(&rcFrame);
        // Is its size (0,0)? If so, it was just floated
        CSize sizerc(rcFrame.right - rcFrame.left,
                  rcFrame.bottom - rcFrame.top);
        if (!((sizerc.cx <= 0) && (sizerc.cy <= 0)))
            return ((m_bChangeDockedSize) ?
                    m_sizeDocked = m_sizeFloating = sizerc :
                    m_sizeFloating = sizerc);
        else
        {
            // Modify Style when dialog bar is first floated
            pFrame->ModifyStyle(MFS_MOVEFRAME, WS_THICKFRAME, 0);
            m_dwStyle |= MFS_THICKFRAME;
            m_dwStyle |= WS_THICKFRAME;
            m_dwStyle &= ~MFS_MOVEFRAME;
            // Return last floated size
            return m_sizeFloating;
        }
      }
 
      if (bStretch) // if not docked stretch to fit
          return CSize(bHorz ? 32767 : m_sizeDocked.cx,
              bHorz ? m_sizeDocked.cy : 32767);
      else
          return m_sizeDocked;
    }  
  Sample Code for MFC Included with Visual C++ 4.0 and above
 
    /* Compile options needed: Default
    */  
 
    // ResizableDlgBar.h : header file
    //  
 
    class CResizableDlgBar : public CDialogBar
    {
    // Construction
    public:
      BOOL Create( CWnd* pParentWnd, UINT nIDTemplate, UINT nStyle,
                UINT nID, BOOL = TRUE);
      BOOL Create( CWnd* pParentWnd, LPCTSTR lpszTemplateName,
                UINT nStyle, UINT nID, BOOL = TRUE);
 
    // Attributes
    public:
      CSize m_sizeDocked;
      CSize m_sizeFloating;
      BOOL m_bChangeDockedSize;   // Indicates whether to keep
                          // a default size for docking
 
    // Operations
    public:
 
    // Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CResizableDlgBar)
      //}}AFX_VIRTUAL
      virtual CSize CalcDynamicLayout( int nLength, DWORD dwMode );
 
    // Implementation
    public:
 
    // Generated message map functions
    protected:
      //{{AFX_MSG(CResizableDlgBar)
      // NOTE - the ClassWizard will add and remove member functions here.
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
    };
 
    /  
 
 
    // ResizableDlgBar.cpp : implementation file
    //  
 
    #include "stdafx.h"
    #include "ResizableDlgBar.h"
 
      
    // CResizableDlgBar Construction/Destruction
 
    BOOL CResizableDlgBar::Create( CWnd* pParentWnd, UINT nIDTemplate,
                        UINT nStyle, UINT nID, BOOL bChange)
    {
      if(!CDialogBar::Create(pParentWnd,nIDTemplate,nStyle,nID))
          return FALSE;
 
      m_bChangeDockedSize = bChange;
      m_sizeFloating = m_sizeDocked = m_sizeDefault;
      return TRUE;
    }
 
    BOOL CResizableDlgBar::Create( CWnd* pParentWnd,
                        LPCTSTR lpszTemplateName, UINT nStyle,
                        UINT nID, BOOL bChange)
    {
      if (!CDialogBar::Create( pParentWnd, lpszTemplateName,
                                  nStyle, nID))
        return FALSE;
 
      m_bChangeDockedSize = bChange;
      m_sizeFloating = m_sizeDocked = m_sizeDefault;
      return TRUE;
    }
 
      
    // Overloaded functions
 
    CSize CResizableDlgBar::CalcDynamicLayout(int nLength, DWORD dwMode)
    {
      // Return default if it is being docked or floated
      if ((dwMode & LM_VERTDOCK) || (dwMode & LM_HORZDOCK))
      {
        if (dwMode & LM_STRETCH) // if not docked stretch to fit
            return CSize((dwMode & LM_HORZ) ? 32767 : m_sizeDocked.cx,
                    (dwMode & LM_HORZ) ? m_sizeDocked.cy : 32767);
          else
            return m_sizeDocked;
      }
      if (dwMode & LM_MRUWIDTH)
        return m_sizeFloating;
      // In all other cases, accept the dynamic length
      if (dwMode & LM_LENGTHY)
        return CSize(m_sizeFloating.cx, (m_bChangeDockedSize) ?
                  m_sizeFloating.cy = m_sizeDocked.cy = nLength :
                  m_sizeFloating.cy = nLength);
      else
        return CSize((m_bChangeDockedSize) ?
                  m_sizeFloating.cx = m_sizeDocked.cx = nLength :
                  m_sizeFloating.cx = nLength, m_sizeFloating.cy);
    }
 
    BEGIN_MESSAGE_MAP(CResizableDlgBar, CDialogBar)
      //{{AFX_MSG_MAP(CResizableDlgBar)
        // NOTE - the ClassWizard will add and remove mapping macros
  here.
      //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
 

目 录 译者序 前言 第一部分 基础 第1章 概述 1 1.1 Windows基础 1 1.1.1 窗口类结构 2 1.1.2 消息 2 1.1.3 客户区和非客户区 2 1.1.4 重叠窗口、弹出窗口和子窗口 2 1.1.5 父窗口和宿主窗口 3 1.2 Windows消息 3 1.2.1 发送或寄送消息 4 1.2.2 消息类型 4 1.2.3 接收消息 4 1.2.4 窗口处理函数的子类化 5 1.3 窗口绘图 5 1.3.1 设备环境 5 1.3.2 绘图工具 6 1.3.3 映射模式 6 1.3.4 窗口视和视口视 6 1.3.5 逻辑单位和设备单位 7 1.3.6 绘图函数 7 1.3.7 抖动和非抖动颜色 7 1.3.8 设备无关位图 8 1.3.9 元文件 8 1.3.10 何时绘图 8 1.4 MFC基础 8 1.5 Developer Studio基础 9 1.6 Windows和MFC总结 10 1.7 基本类 10 1.8 应用类 11 1.8.1 文档视 11 1.8.2 CWinApp(OC) 11 1.8.3 文档模板 12 1.8.4 线程 12 1.8.5 CFrameWnd(OCW) 12 1.8.6 CDocument(OC) 12 1.8.7 CView(OCW) 13 1.8.8 对话框应用程序 13 1.8.9 SDI应用程序 13 1.8.10 MDI应用程序 13 1.9 其余用户界面类 13 1.9.1 通用控件类 13 1.9.2 菜单类(O) 14 1.9.3 对话框类 15 1.9.4 通用对话框MFC类 15 1.9.5 控件条类 (OCW) 15 1.9.6 属性类 15 1.10 绘图类 16 1.11 其他MFC类 16 1.11.1 文件类 16 1.11.2 CArchive和序列化 16 1.11.3 数据库类 17 1.11.4 ODBC类 17 1.11.5 DAO类 17 1.11.6 数据集合类 17 1.11.7 通信类 18 1.12 类的消息机制 18 1.12.1 MFC如何接收一个寄送消息 18 1.12.2 MFC如何处理接收的消息 18 1.12.3 UI对象 20 1.13 小 结 20 第2章 控制条 21 2.1 通用控制条 21 2.2 用API创建控制条 22 2.3 用MFC创建控制条 24 2.3.1 CToolBarCtrl和CStatusBarCtrl 24 2.3.2 CToolBar和CStatusBar 24 2.3.3 CControlBar 26 2.4 停靠栏 27 2.4.1 设置停靠功能 28 2.4.2 自动改变大小和移动 30 2.4.3 停靠栏小结 30 2.5 浮动条 31 2.6 MFC的高级控制条类小结 32 2.7 视和控制条如何共享客户区 32 2.7.1 CFrameWnd::RecalcLayout() 32 2.7.2 CWnd::RepositionBars() 33 2.7.3 CControlBar::OnSizeParent() 33 2.7.4 CalcDynamicLayout()和 CalcFixedLayout () 34 2.7.5 CToolBar::CalcFixedLayout()和CTool Bar:: CalcDynamicLayout() 35 2.7.6 工具栏布局 35 2.7.7 CStatusBar::CalcFixedLayout() 36 2.7.8 CDockBar::CalcFixedLayout() 36 2.7.9 共享客户区小结 36 2.8 对话条 37 2.9 伸缩条 38 2.9.1 CReBar和CReBarCtrl 39 2.9.2 CReBar::CalcFixedLayout() 39 2.10 命令条 39 2.11 控制条窗口小部件风格 40 2.11.1 工具栏按钮风格 40 2.11.2 状态栏窗格风格 40 2.11.3 伸缩条段风格 40 2.12 设计自己的控制条 41 2.12.1 重载CControlBar::CalcDynamic-Layout() 41 2.12.2 增加WM_SIZEPARENT消息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值