MFC Dialog Display issue

drjackool 956 Reputation points
2021-11-17T05:14:57.047+00:00

Hi
In my app I have dialog I want when it is displayed just it activate and come on other windows (other apps) but my problem is the my application window (main frame) is also come on to other windows. How solve it?
I changes dialog style to Overlapped or Popup but not works! Also I set parent parameter to null but not works.
I use this code:

CMyDialog* pdlg = new CMyDialog(this);
if (pdlg->Create(this))
{
   pdlg->ShowWindow(SW_SHOW);
   pdlg->SetActiveWindow();
   pdlg->SetForegroundWindow();
}

Thanks

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
0 comments No comments
{count} votes

Answer accepted by question author
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2021-11-17T09:38:35.45+00:00

    Hi @drjackool ,

    I suggest you try to set the parent window of the dialog box to the computer desktop, so that the desktop is displayed when the dialog box is called. You could refer to my code.

    Mydlg = new MyDlg;  
    Mydlg->Create(IDD_DIALOG, GetDesktopWindow());  
    Mydlg->ShowWindow(SW_SHOWNORMAL);  
    

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3 additional answers

Sort by: Most helpful
  1. RLWA32 51,456 Reputation points
    2021-11-18T08:41:16.503+00:00

    When creating a modeless MFC dialog MFC sets the application's main frame window as the dialog's Owner window (not Parent). And since an owned window always appears over its owner that is why the application main frame window behaves as you have observed.

    I have tested @YujianYao-MSFT solution to pass MFC's GetDesktopWindow() function call as the second parameter to the dialog's Create function and it achieves the desired results for me.

    However, if for whatever reason you have not had success that way then here is an alternative method to change the Owner window of an MFC modeless dialog so that it is a top level window and not an owned window.

    Error checking omitted -

        CMyDialog* p = new CMyDialog;  
        p->Create(IDD_MYDIALOG);  
        ::SetWindowLongPtr(p->GetSafeHwnd(), GWLP_HWNDPARENT, NULL);  
        p->ShowWindow(SW_SHOW);  
      
    
    1 person found this answer helpful.
    0 comments No comments

  2. Guido Franzke 2,191 Reputation points
    2021-11-17T08:42:01.307+00:00

    Hello,
    setting the parent parameter to NULL makes the main frame window the parent.
    When you activate a dialog in an SDI or MDI application, the application gets active and is set to the foreground by Windows. If you don't want the main frame window to get on top, you must change the z-order of the mainframe to the bottom (AfxGetMainWnd()->SetWindowPos) or hide it (AfxGetMainWnd()->ShowWindow(SW_HIDE)).
    Regards, Guido


  3. Petrus 【KIM】 546 Reputation points
    2021-11-18T05:39:58.763+00:00

    Maybe do you want like this?

    // App command to run the dialog
    void CMFCApplication1App::OnAppAbout()
    {
        CAboutDlg aboutDlg;
    
        this->m_pMainWnd->ShowWindow(SW_HIDE);
        aboutDlg.DoModal();
        this->m_pMainWnd->ShowWindow(SW_SHOW);
    }
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.