Posts Tagged ‘OnInitDialog’

What happens before and after OnInitDialog!

Two functions to look up would be…

  • _AfxPreInitDialog
  • _AfxPostInitDialog

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!

Making a property sheet window resizable!

March 27, 2008 3 comments

Sometime back my boss asked me to write a test application for a layout manager. This test application was for testing layout manager on a property sheet!

Hmm so property sheet cannot be resized even if you modify the style of a property sheet. Well the reason why it can’t be resized is because SC_SIZE menu is missing from the property sheet system menu (Press Alt and Spacebar)!

Restore system menu by calling GetSystemMenu(TRUE), but this unfortunately doesn’t work unless we call GetSystemMenu(FALSE) first! Along with this modify the style of property sheet window to make it resizable.

Well now you might think why the heck then do we have to change the system menu if there is a style for resizing. The problem is you will see the resize mouse cursor on the borders of this property sheet but the dragging part won’t work, unless SC_SIZE is restored back to the system menu!

BOOL MyPropSheet::OnInitDialog()
   // This first call is mandatory, call with revert flag set to false
   CMenu* pMenu = GetSystemMenu( FALSE );
   ASSERT( pMenu );

   // Call once more with revert flag set to true to restore original system menu
   GetSystemMenu( TRUE ); // This reverts to original system menu
    // Also don't forget to change the style to resizable

   return CPropertySheet::OnInitDialog();
}// End OnInitDialog

Changing the tab order of a control at runtime!

November 27, 2007 3 comments

Follow these steps(Just a demo)…

  1. Add two buttons to a fresh dialog in a sequencial order, IDC_BUTTON1 and IDC_BUTTON2
  2. 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 SetWindowPos on IDC_BUTTON1 and hwndInsertAfter is IDC_BUTTON2 hence 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.

%d bloggers like this: