Two functions to look up would be…
As the name suggests _AfxPreInitDialog gets called before OnInitDialog is called and _AfxPostInitDialog gets called after OnInitDialog.
One of the things that happens in _AfxPostInitDialog is centering of a dialog if user hasn’t changed position of the dialog in OnInitDialog, so how is this done? _AfxPreInitDialog stores co-ordinates of the dialog before OnInitDialog was called and if these co-ordinates hasn’t changed then _AfxPostInitDialog calls CenterWindow MFC function.
All of this is taking place in AfxCallWndProc! WM_INITDIALOG is handled as a special case. Also HandleInitDialog function calls PreInitDialog and OnInitDialog functions!
So have fun looking up these functions!
Follow these steps(Just a demo)…
- Add two buttons to a fresh dialog in a sequencial order, IDC_BUTTON1 and IDC_BUTTON2
- Goto OnInitDialog(you are not restricted to OnInitDialog, you can also do this on a button click) and paste this line of code
GetDlgItem( IDC_BUTTON1 )->SetWindowPos(GetDlgItem( IDC_BUTTON2 ), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE );
So now run the dialog and you will see that
the focus is on the second button i.e. IDC_BUTTON2, I am invoking
IDC_BUTTON1 is moved down in the Z-Order after
IDC_BUTTON2. So this is how we dynamically change the Z-Order or tab order.
The key to all this behavior is because SWP_NOZORDER is not given when SetWindowPos is called, so it changes Z-Order of the control on which this method is invoked.