Make focused modeless dialog topmost

1k views Asked by At

So I have my main dialog that calls modeless dialogs through this function (this is the legacy code on the project):

void MyClass::ShowDialog(CDialog* dialog)
{
    if (!IsWindow(dialog->m_hWnd))
    {
        return;
    }

    int nCmdshow1 = dialog->IsWindowVisible() ? SW_HIDE : SW_SHOW;
    dialog->ShowWindow( nCmdshow1 );
}

Problem: all sub dialogs stay on top of my main dialog.
Desired behavior: whichever's focused (they are all modeless), be it the main dialog, or sub dialogs, I want it to be the topmost dialog. Thank you!

Note: I already tried on my main dialog's OnInitDialog() these but didn't work:
1. SetWindowPos(&this->wndTop,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
2.SetWindowPos(&this->wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);

EDIT
Also, sub dialogs are created this way:
m_subDlg1->Create( SubDlg1::IDD, this );

1

There are 1 answers

8
xMRi On BEST ANSWER

As long as there is an owner relation between two windows. the owner of a window can never be on top of the owned window.

Windows in an owner, parent, child relation always behave the same. The owned/child window is always on top of the parent/owner.

If you want to break this, you have to break the owner/child relation. Let all dialog windows have no owner... than they may float freely.

But: I will expect the you program doesn't behave better. Even worse. User might search windows that are deep below covered under other windows. And they will never get in front, when your program gets active.

See the description about parent/child/owned windows here. Also this article might be helpful.

Edit: The problem is that internally the MFC sets the main window as an owner if no parent is given. Only the call to BOOL Wnd::CreateDlgIndirect(LPCDLGTEMPLATE lpDialogTemplate, CWnd* pParentWnd, HINSTANCE hInst) allows to leave pParentWnd NULL.

So you may create the window as normal, but use SetParent(NULL) after it was created. Again the MFC ASSERTs this. SO you may use the API function and the handle of your dialog.