Saturday, April 18, 2009

Centralizing modeless child dialog

Normally when we call the DoModal() function of a dialog, the dialog will created and displayed at the center of the parent dialog.

But when a modless dialog is displayed, the child dialog will be displayed at the top left portion of the parent dialog.

I noticed this when a codeproject user asked how to centralize a modeless child dialog. Initially I thought that MFC might be doing some thing in the DoModal() function. So I stepped into the DoModal() function. But I couldn't find any difference in the dialog creation in CDialog::DoModal and CDialog::Create().
Another difference that I know between a modal and modeless dialog is that, in the case of the modal dialog, the parent window will be disabled. MFC does this inside the DoModal() function. To try my luck I disabled the parent window before calling the Create function of dialog. Surprisingly it worked! The modeless child dialog came at the center of parent dialog.

So if you want to centralize a modeless dialog, just disable the parent dialog before create and re-enable it after the creation.
void CDialogBased2Dlg::OnBnClickedButton1()
EnableWindow( FALSE );
m_ChildDlg.Create( ChildDialog::IDD, this );
EnableWindow( TRUE );
m_ChildDlg.ShowWindow( SW_SHOW );


  1. Dear Friend,
    I don't know this is correct place to put this message, anyhow, I'm just writing, if you are think is it's wrong please ignore.You are articles are nice and excellent concept. I would like to invite to newly launched .NET Programming website the on 1st of this June 2009. Currently CodeGain has more than 450 articles within the a month under the followings categories C#, VB.NET,ASP.NET,WPF,WCF,WFF,LINQ,SilverLight, AJAX, JQuery, JavaScript, Sql Servers , Oracle and more. To more list of categories visit the I have seen you are writing greatest article to web portal, I’m kindly asking you publish your article in also and support to grow the CodeGain share this with your friends also. I am expecting good response from you. You can contact me using

    Thank you