Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1082905
  • 博文数量: 135
  • 博客积分: 10182
  • 博客等级: 上将
  • 技术积分: 1565
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-07 16:05
文章分类

全部博文(135)

文章存档

2011年(5)

2010年(20)

2009年(3)

2008年(16)

2007年(91)

分类: C/C++

2009-05-15 15:15:52

文件: easysize_src.zip
大小: 1KB
下载: 下载
文件: easysize_demo.zip
大小: 13KB
下载: 下载
    窗口调整大小时,窗口内的控件是不会跟着调整的,自己整起来真是烦人呀!好在网上高人多,而且有share精神,便宜我这水平潮的家伙。把老外的东西贴过来备忘,呵呵。

EasySize - Dialog resizing in no time!

  1. #include EasySize.h to your stdafx.h (or put it in your include directory and #include , which I recommend)
  2. Add DECLARE_EASYSIZE anywhere in your class declaration:
    Collapse
    class CEasySizeDemoDlg : public CDialog
    {
    DECLARE_EASYSIZE
    ...
  3. Create an OnInitDialog handler if it doesn't already exist, and put this in the end of it: "INIT_EASYSIZE;" :
    Collapse
    BOOL CEasySizeDemoDlg::OnInitDialog()
    {
        CDialog::OnInitDialog();
    ...    
        INIT_EASYSIZE;
        return TRUE; // return TRUE  unless you set the focus to a control
    } 
  4. Create an OnSize handler and add the UPDATE_EASYSIZE; macro to it:
    Collapse
    void CEasySizeDemoDlg::OnSize(UINT nType, int cx, int cy) 
    {
        CDialog::OnSize(nType, cx, cy);
        UPDATE_EASYSIZE;
    } 
  5. Optional - If you want your dialog to have a minimum size, then create an OnSizing handler and add the EASYSIZE_MINSIZE macro as below:
    Collapse
    void CEasySizeDemoDlg::OnSizing(UINT fwSide, LPRECT pRect) 
    {
        CDialog::OnSizing(fwSide, pRect);
        EASYSIZE_MINSIZE(280,250,fwSide,pRect);
    }
    //(in this example, 280 is the minimum width and 250 the 
    //minimum height we want our dialog to have)
  6. Now you have to create the "EasySize Map" (or whatever you want to call it) in which you will specify the behavior of each dialog item. It can be placed anywhere inside your class implementation. The map looks like this:
    Collapse
    BEGIN_EASYSIZE_MAP(class_name)
        ...
        EASYSIZE(control,left,top,right,bottom,options)
        ...
    END_EASYSIZE_MAP
    

    The map from the demo application looks like this:

    Collapse
    ...
        //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    BEGIN_EASYSIZE_MAP(CEasySizeDemoDlg)
        EASYSIZE(IDC_TITLE,ES_BORDER,ES_BORDER,
            ES_BORDER,ES_KEEPSIZE,ES_HCENTER)
        EASYSIZE(IDC_RADIO1,ES_BORDER,ES_BORDER,
            ES_KEEPSIZE,ES_KEEPSIZE,0)
        EASYSIZE(IDC_RADIO2,ES_BORDER,ES_BORDER,
            ES_KEEPSIZE,ES_KEEPSIZE,0)
        EASYSIZE(IDC_CONTENT,ES_BORDER,ES_BORDER,
            ES_BORDER,ES_BORDER,0)
        EASYSIZE(IDC_STATUSFRAME,ES_BORDER,ES_KEEPSIZE,
            ES_BORDER,ES_BORDER,0)
        EASYSIZE(IDC_STATUS,ES_BORDER,ES_KEEPSIZE,
            ES_BORDER,ES_BORDER,0)
        EASYSIZE(IDOK,ES_KEEPSIZE,ES_KEEPSIZE,
            ES_BORDER,ES_BORDER,0)
        EASYSIZE(IDCANCEL,ES_KEEPSIZE,ES_KEEPSIZE,
            ES_BORDER,ES_BORDER,0)
        EASYSIZE(IDC_MYICON1,ES_BORDER,IDC_RADIO2,IDC_CONTENT,
            IDC_STATUSFRAME,ES_HCENTER|ES_VCENTER)
        EASYSIZE(IDC_MYICON2,ES_BORDER,ES_BORDER,IDC_TITLE,
            ES_KEEPSIZE,ES_HCENTER)
    END_EASYSIZE_MAP
    
    ///////////////////////////////////////////////////////////////
    // CEasySizeDemoDlg message handlers
    ...
    

    Looks confusing? It's not once you get the point (and I know I'm not good at explaining it) Read on.

EASYSIZE Macro

The EASYSIZE macro is used in the EasySize Map to specify what behavior your controls will have on dialog resize. It looks like this:

Collapse
EASYSIZE(control,left,top,right,bottom,options)

control is the ID of the dialog item you want re-positioned (which will be referred to as the 'current control' further on).

left, top, right and bottom can be either the ID of another control in the dialog (not the current control), or one of the special values, ES_BORDER and ES_KEEPSIZE.

Basically, if you specify an ID, the distance from the current control and the item designated by the ID will remain the same when the dialog is resized: The current control will 'stick' to the other item. ES_BORDER works the same way as if you had specified a control ID, except that it's the distance between the current control and the dialog border that will be kept constant. Specifying ES_KEEPSIZE in, let's say left, will keep the width of the current control the same, and will make the current control right-aligned to whatever you specified in right. The width (or height, if you specified ES_KEEPSIZE in top or bottom) of the current control will always remain what it is in the dialog resource. (I know this explanation sucks, but look at the demo application if you are confused or post you question in the board below). Obviously ES_KEEPSIZE cannot be specified in both "left and right" or "top and bottom".

options can be a combination of ES_HCENTER, ES_VCENTER and 0 (use 0 if you don't want any of the other). ES_HCENTER horizontally centers the control between the two items specified in left and right (both of those can not be ES_KEEPSIZE!). The width of the current control will always remain the same as in the dialog resource. ES_VCENTER works the same way, but for vertical centering (using top and bottom, and where the height will remain constant).

咱e文水平洼,好在有网呀。咱搜,搜到了下边这个中文版,不是简单翻译呦。

1,将在对话框的属性里设置 边框可调整大小的

2,在主窗体cpp添加 #include EasySize.h ,也可以按照原创的方法添加到 stdafx.h中

3,在定义主窗体的地方

以下是引用片段:
  class CEasySizeDemoDlg : public CDialog
  {
  DECLARE_EASYSIZE //添加这个,如果添加后编译错误,参考步骤2。

4,在主窗体的OnInitDialog消息函数中

以下是引用片段:
  BOOL CEasySizeDemoDlg::OnInitDialog()
  {
  CDialog::OnInitDialog();
  ...
  INIT_EASYSIZE; //添加这个
  return TRUE; // return TRUE unless you set the focus to a control
  }

5,重载消息WM_SIZE消息函数

以下是引用片段:
  void CEasySizeDemoDlg::OnSize(UINT nType, int cx, int cy)
  {
  CDialog::OnSize(nType, cx, cy);
  UPDATE_EASYSIZE; //添加这个
  }

6,如果你希望窗口在调整大小的时候限制最大不能超过多少,最小不能小于多少可以重载onsizing消息

 这个消息好象不能在类向导里直接添加,其实是可以的。当然,你要是嫌麻烦的话,就如下做1:

  直接如下做:

以下是引用片段:
  ON_WM_SIZE();下面再添加了这个
  ON_WM_SIZING() ;

  然后查找到:

以下是引用片段:
  afx_msg void OnSize(UINT nType, int cx, int cy);下面再添加了这个
  afx_msg void OnSizing(UINT fwSide, LPRECT pRect);

  再添加事件处理函数:

以下是引用片段:
  void CEasySizeDemoDlg::OnSizing(UINT fwSide, LPRECT pRect)
  {
  CDialog::OnSizing(fwSide, pRect);
  EASYSIZE_MINSIZE(280,250,fwSide,pRect); //280,250是最小尺寸,
  //如果想拥有最大尺寸功能可以修改EasySize.h,仿照EASYSIZE_MINSIZE的方法写个最大限制的。
  }

  当然,你也可以用类向导来做的:类向导—>classinfo(类信息)—>Messagefilter—>Windows

  好了,然后你再回到Message maps里面去看message是不是里面就有了你要的WM_SIZING了,^_^!

  7,最后添加 消息映射

以下是引用片段:
  BEGIN_EASYSIZE_MAP( your main dialog name) //这里修改成主窗体类名
  ...
  //这里针对每个需要在调整窗体大小时调整大小位置的控件添加处理函数
  EASYSIZE(control,left,top,right,bottom,options) //这里的control写控件的id号
  //left:如果要保持与主窗体左边位置固定可以写ES_BORDER,如果要保持大小不变,此处不加特殊处理,写ES_KEEPSIZE,如果要以其他空间的左边,作为基准可以写该控件的id号
  //top,right,bottom同上
  //options控制水平垂直居中效果,如ES_HCENTER|ES_VCENTER ,无特殊写0
  ...
  END_EASYSIZE_MAP

  具体EASYSIZE(control,left,top,right,bottom,options) 的用法:

  其中:control为对话框中的控件ID值,left,top,right,bottom四个参数为控件位置的坐标,其值可以选择ES_BOARD,ES_KEEPSIZE, 控件ID值三者之一。Options可以为ES_HCENTER, ES_VCENTER的结合,options可置0。

  1.   ES_BOARD表示控件与对话框边界(以下简称边界)的距离;
  2.   ES_KEEPSIZE表示控件水平/垂直方向上尺寸保持不变;
  3.   控件ID值表示当前控件与指定控件之间的距离;
  4.   ES_HCENTER表示缩放后控件在指定位置内水平居中;
  5.   ES_VCENTER表示缩放后控件在指定位置内垂直居中;

  例如:

  EASYSIZE(IDOK,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,0)

  表示缩放后,值为IDOK的控件,距离边界上下左右位置保持不变,水平和垂直方向尺寸拉伸;

  EASYSIZE(IDOK,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,ES_HCENTER)

  表示缩放后,值为IDOK的控件,距离边界上下位置保持不变,垂直方向尺寸拉伸,水平居中;

  EASYSIZE(IDOK,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,ES_HCENTER| ES_HCENTER)

  表示缩放后,值为IDOK的控件,在对话框内垂直居中,水平居中;

  EASYSIZE(IDOK,ES_BORDER,ES_KEEPSIZE,ES_KEEPSIZE,ES_BORDER,0)

  表示缩放后,值为IDOK的控件,距离边界左、下方位置保持不变,同时保持控件尺寸;

  EASYSIZE(IDOK,ES_BORDER,ES_KEEPSIZE, ES_BORDER,ES_BORDER,0)

  表示缩放后,值为IDOK的控件,距离边界左、右、下方位置保持不变,水平方向尺寸拉伸,垂直方向尺寸不变;

  EASYSIZE(IDOK,ES_BORDER,ES_BORDER,IDCANCEL,ES_BORDER,0)

  表示缩放后,值为IDOK的控件,距离边界上下左位置保持不变,距离ID值为IDCANCEL的右方位置距离保持不变,水平和垂直方向尺寸拉伸;(当使用指定控件作为定位参数时候,确保指定控件的EASYSIZE在该宏前面)

 

阅读(5531) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~