Click here to Skip to main content
15,850,750 members
Articles / Desktop Programming / MFC

Control Positioner -- Repostion your child windows without code change

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
24 Jun 2003 77.5K   1.9K   28   7
This is a small class for control repositioning. It can create an INI file, which logs all aspects of your control.

Sample Image - Control_Positioner.gif


Control Positioner (Control Poser) - This is a small class for control repositioning. It can create an INI file, which logs all aspects of your controls (child windows). e.g.:

  1. Enable
  2. Visibility
  3. Default value (checked or text title)
  4. Size and screen position

So when you want to change them, you just update this INI file instead of recompiling your code. Sometimes it helps to create a highly flexible user interface, especially useful when the customer doesn't know what they want or your requirement manager has a really changing personality :-)

How to use it:

  1. Include header CtrlPositioner.h in your Dialog/Main window code.
  2. In OnInitDialog add:
    CCtrlPositioner poser;
    //Add the control you wish to control
     //poser.SavePosData(); //This creates an ini file for you first time,
                           //so latter you can just change the file.
                          //This line should be commented 
                         //out after you got the file
    poser.ReadPosData(); //Read Position from ini file
    //Organize control regarding your ini definition.


  1. INI file is called CtrlPos.ini, this can be changed easily in CtrlPositioner.cpp.
  2. This class should also work with other parent windows besides CDialog, although I never tested it :-(
  3. Currently parent window's title is used as section name in INI file, it's not good as there could be more windows with the same caption on it. I would like to improve this if you can give me any suggestions?


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

Written By
Software Developer (Senior)
Ireland Ireland
I have over 13 Years IT industry experience as Principle/Senior Programmer. I am experienced in .NET/J2EE system design and detailed implementation. I master UML modelling and OO design methodology with a strong C#/C++/Java coding background. I have been in working/managing a distributed project using Agile/Waterfall approach. My business knowledge includes Telecommunication, Financial Investment/Trading, and Banking.

Comments and Discussions

Questionideas? Pin
andyj11525-Jun-03 5:46
andyj11525-Jun-03 5:46 
AnswerRe: ideas? Pin
Ziming25-Jun-03 5:55
Ziming25-Jun-03 5:55 
GeneralRe: ideas? Pin
TW25-Jun-03 6:50
TW25-Jun-03 6:50 
GeneralRe: ideas? Pin
Ziming25-Jun-03 7:00
Ziming25-Jun-03 7:00 
GeneralRe: ideas? Pin
TW26-Jun-03 1:36
TW26-Jun-03 1:36 
GeneralRe: ideas? Pin
andyj11525-Jun-03 8:45
andyj11525-Jun-03 8:45 
Sorry Ziming Wang,

maybe for the first (1) point I was not clear; looking inside the INI file, the session name (as you mention in the article) is the caption title of the Dialog.

Sometimes I use the caption as a sort of "status bar" to give some more information to the user, so I'll not be able to use your class.

My suggestion was to do the same as the controls, use the IDD_ of the resource template of the dialog.

Maybe the simplest way is to pass the template resource IDD_ during the SetDlg(...) call, something like (from your sample):

CCtrlPositioner poser;<br />
poser.SetDlg(this, IDD_POSERDEMO_DIALOG); 

For the second (2), the point is this: I like your class because it becomes easy to customize dialogs layout at runtime, and as a lazy programmer I am ( Roll eyes | :rolleyes: ), it would be nice to have one method that maps all the controls at the same time. It's easier to make a sample:

void CCtrlPositioner::AddCtrls()<br />
{<br />
    ASSERT(m_pParentWnd);<br />
    ASSERT(IsWindow(m_pParentWnd->m_hWnd));<br />
<br />
    if(!m_pParentWnd)<br />
         return;<br />
<br />
    // first control in the dialog<br />
    CWnd* pCtrlWnd = m_pParentWnd->GetWindow(GW_CHILD);<br />
    while(pCtrlWnd)<br />
    {<br />
         UINT uID = pCtrlWnd->GetDlgCtrlID();<br />
<br />
         if (uID != IDC_STATIC)	//	static require other solution<br />
              AddCtrl(uID);<br />
<br />
         // next control in the dialog<br />
         pCtrlWnd = pCtrlWnd->GetNextWindow();<br />
    }<br />

With a function written this way, you can capture all the dialog controls with one only method call.

For the third (3), I can only say this: I find your implementation simple essential and easy, and thats why I really like it. Anyway, in big projects (with a lot of dialogs) working with a format like XML is the natural develop.

Big Grin | :-D

GeneralRe: ideas? Pin
Ziming25-Jun-03 22:59
Ziming25-Jun-03 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.