Click here to Skip to main content
15,886,724 members
Articles / Programming Languages / C#

Centralizing modeless child dialog

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Apr 2009CPOL 16.9K   3   1
Describes how to display a modeless child dialog at the center of the parent 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 );
}
This article was originally posted at http://0memory.blogspot.com/feeds/posts/default?alt=rss

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCenter Pin
BOFH14-Sep-21 23:16
BOFH14-Sep-21 23:16 

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.