MFC: How to show modeless dialogs behind the main window?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

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

Threaded View

  1. #1
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,057

    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; February 17th, 2012 at 05:56 AM.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center