CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    MFC: How to show modeless dialogs behind the main window?

    Q: In my application's main dialog, I've created a top-level modeless dialog as follows:
    Code:
    // MainDialog.h
    #include "FloatingDialog.h"
    // ...
    class CMainDialog : public CDialog
    {
       CFloatingDialog m_dlg;
       // ...
    };
    Code:
    // MainDialog.cpp
    // ...
    void CMainDialog::OnButtonShowDialog() 
    {
       // create modeless dialog if not already created
       if(NULL == m_dlg.m_hWnd)
          m_dlg.Create(IDD_FLOATING_DIALOG);
    
       // asure it's top-level (WS_CHILD style is not set)
       ASSERT(!(m_dlg.GetStyle() & WS_CHILD));
       
       // place it behind this window
       m_dlg.SetWindowPos(this, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
    
       // make it visible
       m_dlg.ShowWindow(SW_SHOW);
    }
    Although SetWindowPos has been called in order to place the new dialog behind the main dialog, it's still shown on top.
    Also, a user action like clicking in the main dialog does not change this.
    What can be done to overcome this issue?

    A: If call CDialog::Create ignoring the second parameter, which is default NULL, the main application's window becomes its owner. Every ovned window stays always on top of its owner and there is no chance to move it behind.
    To create a top-level modeless dialog that can stay behind application main window, pass a pointer to desktop window in second parameter of CDialog::Create:
    Code:
    void CMainDialog::OnButtonShowDialog() 
    {
       // create modeless dialog if not already created
       if(NULL == m_dlg.m_hWnd)
          m_dlg.Create(IDD_FLOATING_DIALOG, CWnd::GetDesktopWindow());
       //...
    Resources [MSDN]


    See also [Codeguru FAQ]
    Last edited by ovidiucucu; October 4th, 2022 at 11:35 AM. Reason: updated links
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured