MFC对话框中的控件随着对话框大小变化而变化
2014-04-21 09:20
169 查看
转自:http://www.codeproject.com/Articles/1657/EasySize-Dialog-resizing-in-no-time
Download source files - 1.71 Kb
Download demo project - 13.9 Kb
![](http://www.codeproject.com/KB/dialog/easysize/scr1.JPG)
![](http://www.codeproject.com/KB/dialog/easysize/scr2.JPG)
![](http://www.codeproject.com/KB/dialog/easysize/scr3.JPG)
Have you ever thought of how annoying it actually was to spend a lot of time doing a basic GUI for your simple applications instead of focusing on the actual 'content'? Take for example a resizing dialog or property page. You need to write code for each
control that will tell it where to go when the thing is resized, and this can take up a lot of time. Now I know that I'm not the first one to give a solution to this (CResizableDialog),
but this article is on my approach.
Basically, all you need to do is design your dialog the way you want it to look in the resource editor (don't forget to make it resizable), and then define how the controls will behave when the dialog is resized using one single macro for each control.
Note that all this works exactly the same way with both
your stdafx.h (or put it in your include directory and
which I recommend)
Add
in your class declaration:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
Create an
this in the end of it: "
:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
Create an
to it:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
Optional - If you want your dialog to have a minimum size, then create an
and add the
as below:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
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:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
The map from the demo application looks like this:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
Looks confusing? It's not once you get the point (and I know I'm not good at explaining it) Read on.
The
is used in the EasySize Map to specify what behavior your controls will have on
dialog resize. It looks like this:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
further on).
one of the special values,
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.
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
let's say
to whatever you specified in
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
be specified in both
0 (use 0 if you don't want any of the other).
in
of those can not be
the same way, but for vertical centering (using
and where the height will remain constant).
Well I hope you figured out how this works, because it really can make your life easier. Note that using these macros will probably make your compiled code slightly bigger and slower than if you had coded the resizing routines manually, but in most cases
the change is so small not even you will notice.
Last Update - Just corrected a few typos.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
![](http://www.codeproject.com/script/Membership/ProfileImages/%7B6cf7cdb9-0369-4862-a4e7-ac9e5aaab8b4%7D.jpg)
Marc
Richarme
Engineer Nokia
Denmark
![](http://www.codeproject.com/script/Geo/Images/DK.gif)
My programming experience started a long time ago in
QBasic (on a 25MHz 486).
I'm now mainly using Java, C++, C, MFC, Perl and PHP, but have used quite a number of other languages as well for various projects.
Download source files - 1.71 Kb
Download demo project - 13.9 Kb
Introduction
Have you ever thought of how annoying it actually was to spend a lot of time doing a basic GUI for your simple applications instead of focusing on the actual 'content'? Take for example a resizing dialog or property page. You need to write code for eachcontrol that will tell it where to go when the thing is resized, and this can take up a lot of time. Now I know that I'm not the first one to give a solution to this (CResizableDialog),
but this article is on my approach.
Description
Basically, all you need to do is design your dialog the way you want it to look in the resource editor (don't forget to make it resizable), and then define how the controls will behave when the dialog is resized using one single macro for each control.
Usage
Note that all this works exactly the same way with both CDialogand
CPropertyPage
#includeEasySize.h to
your stdafx.h (or put it in your include directory and
#include<EasySize.h> ,
which I recommend)
Add
DECLARE_EASYSIZEanywhere
in your class declaration:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
class CEasySizeDemoDlg : public CDialog { DECLARE_EASYSIZE ...
Create an
OnInitDialoghandler if it doesn't already exist, and put
this in the end of it: "
INIT_EASYSIZE;"
:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
BOOL CEasySizeDemoDlg::OnInitDialog() { CDialog::OnInitDialog(); ... INIT_EASYSIZE; return TRUE; // return TRUE unless you set the focus to a control }
Create an
OnSizehandler and add the
UPDATE_EASYSIZE;macro
to it:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
void CEasySizeDemoDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); UPDATE_EASYSIZE; }
Optional - If you want your dialog to have a minimum size, then create an
OnSizinghandler
and add the
EASYSIZE_MINSIZEmacro
as below:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
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)
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:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
BEGIN_EASYSIZE_MAP(class_name) ... EASYSIZE(control,left,top,right,bottom,options) ... END_EASYSIZE_MAP
The map from the demo application looks like this:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
... //}}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 EASYSIZEmacro
is used in the EasySize Map to specify what behavior your controls will have on
dialog resize. It looks like this:
![](http://www.codeproject.com/images/minus.gif)
Collapse | Copy
Code
EASYSIZE(control,left,top,right,bottom,options)
controlis 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 bottomcan be either the ID of another control in the dialog (not the current control), or
one of the special values,
ES_BORDERand
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_BORDERworks
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_KEEPSIZEin,
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_KEEPSIZEin
topor
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_KEEPSIZEcannot
be specified in both
"leftand
right"or
"topand
bottom".
optionscan be a combination of
ES_HCENTER,
ES_VCENTERand
0 (use 0 if you don't want any of the other).
ES_HCENTERhorizontally centers the control between the two items specified
in
leftand
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_VCENTERworks
the same way, but for vertical centering (using
topand
bottom,
and where the height will remain constant).
Conclusion
Well I hope you figured out how this works, because it really can make your life easier. Note that using these macros will probably make your compiled code slightly bigger and slower than if you had coded the resizing routines manually, but in most casesthe change is so small not even you will notice.
Last Update - Just corrected a few typos.
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.A list of licenses authors might use can be found here
About the Author
![](http://www.codeproject.com/script/Membership/ProfileImages/%7B6cf7cdb9-0369-4862-a4e7-ac9e5aaab8b4%7D.jpg)
Marc
Richarme
Engineer Nokia
Denmark
![](http://www.codeproject.com/script/Geo/Images/DK.gif)
My programming experience started a long time ago in
QBasic (on a 25MHz 486).
I'm now mainly using Java, C++, C, MFC, Perl and PHP, but have used quite a number of other languages as well for various projects.
相关文章推荐
- MFC对话框内控件大小随着对话框大小变化
- MFC中的控件如何随着对话框变化而变化
- MFC如何使控件大小随着对话框大小自动调整
- 控件函数对话框上的控件的大小和位置随着对话框的大小的改变而变化
- EasySize使用笔记(对话框中的控件随着对话框大小的变化进行自动调整)
- MFC改变对话框大小,控件的大小和位置随着改变
- MFC如何使控件大小随着对话框大小自动调整
- MFC如何使控件大小随着对话框大小自动调整
- MFC如何使控件大小随着对话框大小自动调整
- MFC如何使控件大小随着对话框大小自动调整
- MFC如何使控件大小随着对话框大小自动调整
- C++ MFC控件随着窗口大小变化而自适应
- MFC对话框控件随对话框的大小变化
- 对话框控件随着对话框变化大小
- MFC如何使控件大小随着对话框大小自动调整
- MFC如何使控件大小随着对话框大小自动调整
- MFC如何使控件大小随着对话框大小自动调整
- MFC中控件大小随对话框大小动态变化
- MFC多个控件在对话框上的大小变化
- MFC如何使控件大小随着对话框大小自动调整