金喜正规买球

MFC界面控件自动调节大小

转帖|其它|编辑:郝浩|2011-02-10 14:47:12.000|阅读 1242 次

概述:C++ Builder 开发WINDOWS界面非常省时,不仅仅是提供的界面控件元素多,而且界面元素控件有一项自动伸缩和固定控件边界在某个位置上的属性。MFC提供的界面元素控件时没有该属性的,如果需要,必须得手动加上一堆代码才可以实现,非常繁琐。对入门的开发人员来说这一点很头疼,其实本人也很头疼这一点,所以花了点小时间封装了一些代码来解决该问题,主要还是节省开发时间。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  C++ Builder 开发WINDOWS界面非常省时,不仅仅是提供的界面控件元素多,而且界面元素控件有一项自动伸缩和固定控件边界在某个位置上的属性。MFC提供的界面元素控件时没有该属性的,如果需要,必须得手动加上一堆代码才可以实现,非常繁琐。对入门的开发人员来说这一点很头疼,其实本人也很头疼这一点,所以花了点小时间封装了一些代码来解决该问题,主要还是节省开发时间。

  1#ifndef CTEST_AUTO_DLG_H

  2#define CTEST_AUTO_DLG_H

  3

  4#include "stdafx.h"

  5#include <map>

  6

  7using namespace std;

  8

  9namespace EFixedType

  10{

  11    enum EFixedType

  12    {

  13        NONE = 0,

  14    ; ;   TOP = 1,

  15        BOOTOM = 2,

  16 ;       LEFT = 4,

  17        RIGHT = 8

  18    };

  19}

  20

  21template<class T>

  22class CAutoSizeWnd : public T

  23{

  24public:

  25

  26    struct TFixed;

  27

  28    CAutoSizeWnd(UINT nIDTemplate, CWnd *pCWnd) : T(nIDTemplate, pCWnd)

  29    {

  30        m_fInitDlg = FALSE;

  31    }

  32

  33    virtual ~CAutoSizeWnd()

  34    {

  35

  36    }

  37

  38    //功能:该函数必须在 OnCreate 中调用

  39    //参数:

  40    //   &nbsp;    CWnd *pCWnd IN 窗口指针,不允许为NULL

  41    //        DWORD dwType IN 固定的边框,内容为    EFixedType::NONE

  42    //                   &nbsp;                         EFixedType::TOP

  43    //                                     ;  ;      EFixedType::BOOTOM

  44    //                &nbsp;                            EFixedType::LEFT

  45    //                  &nbsp;                          EFixedType::RIGHT

  46    //                       &nbsp;                    中的一个或多个,多个用 '|' 号连接

  47    //返回值:添加是否成功

  48  &nbsp; BOOL AddChildControl(CWnd *pCWnd, DWORD dwType)

  49    {

  50 &nbsp;      if (!pCWnd)

  51        {

  52     &nbsp;      return FALSE;

  53        }

  54

  55     &nbsp;  TFixed *pTFixed = new TFixed();

  56

  57      &nbsp; if (!pTFixed)

  58        {

  59&nbsp;        ;   return FALSE;

  60        }

  61

  62   &nbsp;    if (dwType != EFixedType::NONE)

  63        {

  64            if (dwType & EFixedType::TOP)

  65    &nbsp;       {

  66       &nbsp;        pTFixed->fTop = TRUE;

  67            }

  68

  69        &nbsp;   if (dwType & EFixedType::BOOTOM)

  70        ;    {

  71&nbsp;               pTFixed->fBottom = TRUE;

  72         &nbsp;  }

  73

  74         &nbsp;  if (dwType & EFixedType::LEFT)

  75 &nbsp;  &nbsp;       {

  76                pTFixed->fLeft = TRUE;

  77      &nbsp;     }

  78

  79            if (dwType & EFixedType::RIGHT)

  80 &nbsp;          {

  81 &nbsp;              pTFixed->fRight = TRUE;

  82  &nbsp;         }

  83        }

  84

  85   &nbsp;    m_CWndCollections.insert( CWndCollections::value_type(pCWnd, pTFixed));

  86

  87      &nbsp; return TRUE;

  88    }

  89

  90protected:

  91

  92    BOOL OnInitDialog()

  93    {

  94     ;   T::OnInitDialog();

  95

  96      &nbsp; CWndCollections::iterator entity = m_CWndCollections.begin();

  97   &nbsp;    CWndCollections::iterator endEntity = m_CWndCollections.end();

  98

  99        RECT rect;

  100

  101        TFixed *pTFixed = NULL;

  102

  103   &nbsp;    CRect dlgRect;

  104

  105 &nbsp; &nbsp;    this->GetClientRect(&dlgRect);

  106

  107 &nbsp;      for (; entity != endEntity; ++entity)

  108        {

  109&nbsp;           pTFixed = entity->second;

  110

  111 &nbsp;          entity->first->GetWindowRect(&rect);

  112

  113       &nbsp;    this->ScreenToClient(&rect);

  114

  115 &nbsp;       &nbsp;  pTFixed->iTop = rect.top;

  116

  117&nbsp;           pTFixed->iBottom = dlgRect.Height() - rect.bottom;

  118

  119           ; pTFixed->iLeft = rect.left;

  120

  121            pTFixed->iRight = dlgRect.Width() - rect.right;

  122        }

  123

  124        return TRUE;

  125    }

  126

  127    virtual void OnSize(UINT nType, int cx, int cy)

  128    {

  129       ; T::OnSize(nType, cx, cy);

  130

  131      &nbsp; if (m_fInitDlg == FALSE)

  132        {

  133   &nbsp;     &nbsp;  m_fInitDlg = TRUE;

  134

  135        &nbsp;   return;

  136        }

  137

  138   &nbsp;    //处理所有控件的显示大小

  139

  140     &nbsp;  CWndCollections::iterator entity = m_CWndCollections.begin();

  141        CWndCollections::iterator endEntity = m_CWndCollections.end();

  142

  143        for (; entity != endEntity; ++entity)

  144        {

  145    &nbsp;       AutoSize(entity->first, entity->second);

  146        }

  147    }

  148

  149    void AutoSize(CWnd *pCWnd, TFixed *pTFixed)

  150    {

  151  &nbsp;     if (!pCWnd || !pTFixed)

  152        {

  153&nbsp;           return;

  154        }

  155

  156        CRect dlgRect;

  157

  158   &nbsp;    this->GetClientRect(&dlgRect);

  159

  160      &nbsp; RECT rect;

  161

  162 &nbsp;      pCWnd->GetWindowRect(&rect);

  163

  164        this->ScreenToClient(&rect);

  165

  166        int width = rect.right - rect.left;

  167

  168   &nbsp;    int heght = rect.bottom - rect.top;

  169

  170        if (pTFixed->fTop && pTFixed->fBottom)

  171        {

  172     ;       rect.top = pTFixed->iTop;

  173

  174            rect.bottom = dlgRect.Height() - pTFixed->iBottom;

  175        }

  176     ; &nbsp; else if (pTFixed->fTop)

  177        {

  178

  179        }

  180   &nbsp;    else if (pTFixed->fBottom)

  181        {

  182            rect.top = dlgRect.Height() - heght - pTFixed->iBottom;

  183

  184 &nbsp;   &nbsp;      rect.bottom = dlgRect.Height() - pTFixed->iBottom;

  185        }

  186

  187        if (pTFixed->fLeft && pTFixed->fRight)

  188        {

  189     &nbsp;  &nbsp;   rect.left = pTFixed->iLeft;

  190&nbsp;           rect.right = dlgRect.Width() - pTFixed->iRight;

  191        }

  192        else if (pTFixed->fLeft)

  193        {

  194

  195        }

  196&nbsp;       else if (pTFixed->fRight)

  197        {

  198    &nbsp;       rect.left = dlgRect.Width() - width - pTFixed->iRight;

  199

  200            rect.right = dlgRect.Width() - pTFixed->iRight;

  201        }

  202

  203&nbsp;    &nbsp;  pCWnd->MoveWindow(&rect);

  204    }

  205

  206

  207private:

  208

  209    struct TFixed

  210    {

  211  &nbsp;  &nbsp;  TFixed()

  212        {

  213            fTop = FALSE;

  214            fBottom = FALSE;

  215    ;    ;    fLeft = FALSE;

  216        &nbsp;   fRight = FALSE;

  217    &nbsp;       iTop = 0;

  218          ;  iBottom = 0;

  219         &nbsp;  iLeft = 0;

  220            iRight = 0;

  221        }

  222

  223      ;  BOOL fTop;//是否固定与父窗口上边界的距离

  224   &nbsp;    BOOL fBottom;//是否固定与父窗口下边界的距离

  225  &nbsp; &nbsp;   BOOL fLeft;//是否固定与父窗口左边界的距离

  226   &nbsp;    BOOL fRight;//是否固定与父窗口右边界的距离

  227        int iTop;//与父窗口上边界的距离

  228   &nbsp;    int iBottom;//与父窗口下边界的距离

  229   ;     int iLeft;//与父窗口左边界的距离

  230  &nbsp;     int iRight;//与父窗口右边界的距离

  231    };

  232

  233    typedef map<CWnd *, TFixed *> CWndCollections;

  234

  235&nbsp;   CWndCollections m_CWndCollections;

  236

  237    BOOL m_fInitDlg;

  238};

  239

  240#endif

  该类的使用起来比较简单,最需要在生成一个对话框类的时候把继承的类CDialog 修改为 CAutoSizeWnd<CDialog>,当然还有其他一些相应的CDialog名称的部分修改一下即可。之后再在OnCreate成员函数(响应WM_CREATE事件)内添加代码: AddChildControl(控件指针, 固定的边界枚举),每一个控件加一条语句即可。这样就已经可以实现上述的功能了。

  该模板类最主要的函数是:

  1 //功能:该函数必须在 OnCreate 中调用

  2 //参数:

  3 //  CWnd *pCWnd IN 窗口指针,不允许为NULL

  4 //  DWORD dwType IN 固定的边框,内容为 EFixedType::NONE

  5 //            EFixedType::TOP

  6 //         ;   EFixedType::BOOTOM

  7 //            EFixedType::LEFT

  8 //      &nbsp;     EFixedType::RIGHT

  9 //           中的一个或多个,多个用 '|' 号连接

  10 //返回值:添加是否成功

  11 BOOL AddChildControl(CWnd *pCWnd, DWORD dwType);

  另外该类也只是一个初始品,所以现在只能支持CDialog,需要修改后才能支持CView等其他作为窗体的界面元素。


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@fc6vip.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP