|



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 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.
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 CDialog and
CPropertyPage
#include EasySize.h to your stdafx.h
(or put it in your include directory and
#include <EasySize.h> , which I recommend)
- Add
DECLARE_EASYSIZE anywhere in your class declaration:
class CEasySizeDemoDlg : public CDialog
{
DECLARE_EASYSIZE
...
- Create an
OnInitDialog handler if it doesn't already exist,
and put this in the end of it: "INIT_EASYSIZE;" :
BOOL CEasySizeDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
...
INIT_EASYSIZE;
return TRUE;
}
- Create an
OnSize handler and add the UPDATE_EASYSIZE; macro to it:
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
OnSizing handler and add the EASYSIZE_MINSIZE macro as below:
void CEasySizeDemoDlg::OnSizing(UINT fwSide, LPRECT pRect)
{
CDialog::OnSizing(fwSide, pRect);
EASYSIZE_MINSIZE(280,250,fwSide,pRect);
}
- 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:
BEGIN_EASYSIZE_MAP(class_name)
...
EASYSIZE(control,left,top,right,bottom,options)
...
END_EASYSIZE_MAP
The map from the demo application looks like this:
...
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
...
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:
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).
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 cases the change is so small not even you will notice.
Last Update - Just corrected a few typos.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 105 (Total in Forum: 105) (Refresh) | FirstPrevNext |
|
 |
|
|
 |
|
|
Hi!
I can't use EASYSIZE_MINSIZE in OnSizing event handler.
Here my piece of code goes:
BEGIN_MESSAGE_MAP(MyView, CFormView) ON_WM_ERASEBKGND() ON_WM_SIZE() ON_WM_SIZING() ON_WM_CREATE() END_MESSAGE_MAP() ... and ...
void MyView::OnSizing(UINT fwSide, LPRECT pRect) { CFormView::OnSizing(fwSide, pRect);
EASYSIZE_MINSIZE(100, 100, fwSide, pRect); }
Is there somthing wrong with that?
[]'s
modified on Tuesday, September 30, 2008 9:03 AM
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
this code is very useful to resize controls. I would like to align 4 icons in center. i tried but i cannot align icons in center. Is it possible with this code?
EASYSIZE(IDC_MYICON1,ES_KEEPSIZE,ES_BORDER,ES_BORDER,ES_BORDER,0) EASYSIZE(IDC_MYICON2,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,0) EASYSIZE(IDC_MYICON3,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,0) EASYSIZE(IDC_MYICON4,ES_BORDER,ES_BORDER,ES_BORDER,ES_BORDER,0)
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
thanks a lot for sharing this excellent job! but i found 2 bugs when using the macro: 1?if u project is based on dialog and u put a RichEdit control in it.it could be compiled well but even couldn't be executed!
2?if u project is a multiple document application, u design a dialog in it and put a RichEdit control in the dialog. add the macro in the dialog class, use the dialog somewhere. it could be comiled and executed. but when resizing the dialog, the controls in it do not resize with it until u swith to another window and return!
i think there is something special about richedit but what is it? Is anybody here could help me, thanks in advance
|
| Sign In·View Thread·PermaLink | 1.25/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
I appreciate your hard work on this. I found it extremely handy when used to resize my application. It does take a bit of learning, but once you pick it up it's easy to follow. I've given all credit due to the original programmer. One additional note. I discovered that the order in which you reference your objects is important. For instance I had a set of 3 buttons across the top of my form. Unless I centered the middle button first, and then set the others relative, it wouldn't space properly(evenly).
Danke
J.W.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I use it in a cfromview,but it doesn't work normally,the Group box doesn't shrink when the Resolution is 800*600.The dialog contains the List box,the Group box and so on.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
Marvelous piece of work. Thanks
Best Regards, Mushq
Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan
"English is my second language; please excuse any grammatical or spelling mistakes"
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, first of all: thank you for this - very easy to use and implement.
In the text of the article, you state that it works on CPropertyPages too, but I can't get that to work.
OnSize() of the sheet is called only on creation of the Page, and so resizing the PropertySheet does not send the WM_SIZE to the PropertyPage. Adding a call to the PropertyPage::OnSize() in the PropertySheet::OnSize() function gives no effect. (PropertySheet does not have EasySize). It seems to be redrawing as there is some flicker, but the controls don't size or move at all.
Am I doing something wrong? (Of course I am but what is it?) How can I get EasySize to work in a PropertyPage?
Thanks for any help you can give me.
Wouter
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Suppose i have given a button id in the left or in the right. Now the control resizes till the buttons edge. There should be some gap between those two controls. How can i provide the gap (offset) in between those controls? Please help...
|
| Sign In·View Thread·PermaLink | 1.33/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
I have been using this macro now in many projects and it does a really good job. Can you detail a little what got updated? (I would run diffs myself but I have modified your original slightly years ago to better suite my application.)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
now u said that to specify a minimum size for ur dialog u must impement the OnSizing Function what is exaclty the Message that fire this function since WM_SIZE fire the OnSize() fucntion wat msg will fire the OnSizing() function ? thnx alot for ur time and concen.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
I noticed something... well actually took my a long time testing to figur it out:
When your Dialog class is derived from another dialog, you need to include INIT_EASYSIZE before calling the OnInitDialog of the base class else you will get unpredicable results. I have tested this on Windows CE, don't know if the same problem occurs on a 'normal' Windows environment.
BOOL CMyDlg::OnInitDialog() { INIT_EASYSIZE;
CMyBaseDlg::OnInitDialog();
// TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
Many thanks for this really useful code!
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
In the first place, good job! it's nice and simple.
A nice thing to do would be to make it possible to work on the last found possitions (if the possitions were modified since the last resize).
The problem I'm having is that I'm using a splitter control, that resizes the controls by itself. When the dialog gets resized the controls possitions/sizes are reseted to their original ratios by EasySize.
Thanks! the code is great.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
for this incredibly useful code.
I have uploaded a new version here: http://www.codeproject.com/script/profile/upload/15759/EasySizeHD2.zip
I based my changes on the version by boulwiss. The changes I made:
- Added automatic support for
DeferWindowPos() - this required changing END_EASYSIZE_MAP macro to END_EASYSIZE_MAP(theClass)
- Merged gripper code by gniemcew
- Added macro
EASYSIZE_MINSIZE2 for OnGetMinMaxInfo() handler
- Merged modifications by Jubjub to add support for
ES_KEEPRATIO. See this message for details.
- Initialized some local variables and removed others to eliminate compiler warnings at level 4
Best wishes. Hans
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
hi . thanks you code
if I have 4 control , like this ,this 4 control is same size .
|----------------------| | #### #### #### #### | |----------------------|
when i resize the dialog horizontally . can i use ES_KEEPRATIO to keep the 4 control all has equal size?
thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Tout OK en 5mn, alors que c'est un problème récurent des MFC. Un grand merci A great Frensh Thank You
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
 |
|
|
it do not work if there is a menu on the dialog
BOOL CWLDlg::OnInitDialog() { ....... CMenu mymenu; mymenu.LoadMenu(IDR_MAINMENU); SetMenu(&mymenu); .......... return TRUE; }
aaaaaaaaaaaaaaaaaaa
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
INIT_EASYSIZE;
CMenu mymenu; mymenu.LoadMenu(IDR_MAINMENU); SetMenu(&mymenu);
INIT_EASYSIZE before init menu is ok
aaaaaaaaaaaaaaaaaaa
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|