Class to Auto-Position Controls on Window Resize
Posted
by Paul Wendt
on November 3rd, 2000
Introduction
Have you ever wanted to resize a window but NOT have to deal with all of that annoying control-repositioning? What a hassle! Here is an easy-to-use class that should provide a good foundation for anyone wishing to easily reposition controls when a user resizes the parent window.Usage
In order to use this class, simply follow these steps:- Create a CControlPos member variable in your CDialog (used in example) or CView derived class.
- In the OnInitDialog (or InitialUpdate) function, set the object's parent window
- After the call to CControlPos::SetParent, Add the controls you want to resize.
- Finally, on your WM_SIZE handler, call the object's MoveControls to specify runtime behaviour. move all of the controls. Controls can be moved horizontally and vertically and resized horizontally and vertically.
class CYourDialog : public CDialog
{
...
CControlPos m_cControlPos;
BOOL CYourDialog::OnInitDialog() ... m_cControlPos.SetParent(this); ...
m_cControlPos.AddControl(IDOK, CP_MOVE_HORIZONTAL); m_cControlPos.AddControl(IDCANCEL, CP_MOVE_HORIZONTAL);
void CYourDialog::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
m_cControlPos.MoveControls();
}
Downloads
Download source - 4 KbDownload demo project - 17 Kb

Comments
One small modification...
Posted by adinelt on 03/27/2006 02:26pmThanks for the great class Paul. I did make one small change to it though. I found that when I was resizing a window, the controls would not always refresh properly. As an example, when a button would redraw, it would still show portions of the previous location.
To remedy this, I did the following:
Near the bottom of the MoveControl function, I added a line of code...
m_pParent->ScreenToClient(rcBounds);
pControl->MoveWindow(rcBounds);
pControl->Invalidate (); // new line of code
}
UpdateParentSize();
}
}
Hope this helps...
ReplyAl
GREAT
Posted by Marcel.Kummer on 05/23/2005 05:26amHi Paul! What an honest piece of code! It took me 4 minutes to add your class to my project, to append my controls using m_cControlPos.AddControl and to enable negative moves - and it worked well immediately! Thanks for this contribution! Marcel
Replyi want to create a main window that do not move
Posted by Legacy on 01/27/2004 12:00amOriginally posted by: hector
help
Replyhelp!!!
Posted by Legacy on 05/14/2003 12:00amOriginally posted by: garfield
ReplyLittle problem in ControlPos constructor
Posted by Legacy on 05/23/2002 12:00amOriginally posted by: GeSta
This example is really great. THANX
Just while implementing i find out
that constructor ControlPos(*Parent) doesn't
do everything correctly m_nOldParentHeight isn't set
or smth...
using standart
CP = new ControlPos();
CP.SetParent(myParent);
everything goes OK.
That's it... thax again
GeSta
Replyweak code
Posted by Legacy on 01/04/2002 12:00amOriginally posted by: sherl
if set the one CListCtrl under another CListCtrl is that when
Replyyou resize dialog, one CListCtrl overlapped with other CListCtrl
Fix for resizing group boxes
Posted by Legacy on 12/12/2001 12:00amOriginally posted by: Andy Saul
ReplyMuch appreciation
Posted by Legacy on 11/22/2001 12:00amOriginally posted by: Ian
I just had to bash in this kind of thing into an urgent prototype and this code was a godsend.
Very fast to hook up, which was especially useful because I had to cascade resizing through three or four generations of child windows.
And it works. Praise the lord, it works.
A butt saved, and it was mine. Thanks, mate.
Kia ora
ReplyIan
Nice and Easy
Posted by Legacy on 08/08/2001 12:00amOriginally posted by: Dave
30 minutes I had my CDialogBar controls auto-sizing and moving. Class is easy to use - worked first time and integrates easily into your app.
Reply