How to hide modal dialog?

Posted on 2001-08-31
Medium Priority
Last Modified: 2013-11-20
Hi, I have a simple task. I want to display a modal dialog invisibly. User will not be able to see/click on the OK/Cancel button. And some events will call EndDialog so the app will not hang.
My question is, how do I display the modal invisibly when I say DoModal()?
Do I do something in OnInitDialog()?
Question by:hoooi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6447144

In the OnInitDialog, u set the timer for that dialog just before that function returns TRUE as
SetTimer(100, 1, NULL);

Then using classwizard, map the WM_TIMER message to that dialog, and modify the function as

void CMyDlg::OnTimer(UINT nIDEvent)

Try it out.

Expert Comment

ID: 6447154

I forgot to mention something.
By hiding a modal dialog box, there is every possiblity that, ur app looks like hanging till the hidden dialog closes unless u have popped up this dialog in a seperate thread. May be U would like to create the dialog and launch it as modeless and then hide it???
Check it out.


Expert Comment

ID: 6447182
You can make modal dialog invisible. Just put:


in the OnPaint() handler.  But because it's modal dialog, you can't do anything except on this hidden dialog.  The question is what events do you want to call EndDialog?  Don't forget the dialog is invisible.  How can you trigger those events on the dialog that you won't be able to touch?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 6447217
I call ShowWindow(SW_HIDE) inside OnInitDialog, it failed.
I use ShowWindow(SW_HIDE) instead of DoModal to display the dialog, it failed and because of the first assert error of m_hWnd. I guess that's because I didn't call Create.
May I ask why I should use a timer and can't do it in OnInitDialog?
When I can hide it, I also use SetTimer and OnTimer to perform some operation first, and when the condition is right, then only I EndDialog(IDOK). Actually EndDialog failed on assert error too...
The reason I want this is because I want to hang the app for a while and do something before it continues.

Expert Comment

ID: 6447926

Found some decision of problem, not the best way ...

In MyDlg class definition added:
afx_msg void OnSomeCommand();
virtual BOOL OnInitDialog();

// in message queue inserted OnSomeCommand():
          // NOTE: the ClassWizard will add message map macros here
     //ON_BN_CLICKED(IDC_BUTTON1, OnButton1)

HWND hwDlg;
DLGPROC pDlgOriginalProc;
DWORD dwStyle;
RECT rc;

int CALLBACK MyDlgTmpProc(HWND hwDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
 if( uMsg==WM_COMMAND)
 return DefDlgProc(hwDlg,uMsg,wParam,lParam);
BOOL MyDlg::OnInitDialog()
 // Becuse i don't know how to get HWND (at all, at a moment )
 // here is used asm trick
 // for intel PC, Release not Debug version,
 // Debug work differently and will be error
 // because there is some spam on stack
           mov eax,[ebp+0Ch]
           mov  hwDlg,eax
          // int 3
// Dialog disapears, keyboard focus losed

//  need to test this: , works also

 return 1;

void MyDlg::OnSomeCommand()
      // To restore rect later:

Alex, ua1zcl

Expert Comment

ID: 6465564
Its actually possible to override DoModal, and just not display it at all. Here's a snip from one of my apps, look for the //IANW comments for the juciy bits, the rest is copied straight from the MFC libraries.
NOTE, if they every change the implementation,you'd have to recopy but it works great.

int CProtectionDlg::DoModal()
     // can be constructed with a resource template or InitModalIndirect
     ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
          m_lpDialogTemplate != NULL);

     // load resource as necessary
     LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
     HGLOBAL hDialogTemplate = m_hDialogTemplate;
     HINSTANCE hInst = AfxGetResourceHandle();
     if (m_lpszTemplateName != NULL)
          hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
          HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
          hDialogTemplate = LoadResource(hInst, hResource);
     if (hDialogTemplate != NULL)
          lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);

     // return -1 in case of failure to load the dialog template resource
     if (lpDialogTemplate == NULL)
          return -1;

     // disable parent (before creating dialog)
     HWND hWndParent = PreModal();
     CWnd* pParentWnd = CWnd::FromHandle(hWndParent);
     BOOL bEnableParent = FALSE;
     if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))
          ::EnableWindow(hWndParent, FALSE);
          bEnableParent = TRUE;

          // create modeless dialog
          if (CreateDlgIndirect(lpDialogTemplate, CWnd::FromHandle(hWndParent), hInst))
               if (m_nFlags & WF_CONTINUEMODAL)
                    // enter modal loop
               // IANW : FIX TO KEEP DIALOG QUIET
                    DWORD dwFlags=0;
                         dwFlags = MLF_SHOWONIDLE;
               // IANW FIX END
                    if (GetStyle() & DS_NOIDLEMSG)
                         dwFlags |= MLF_NOIDLEMSG;
                    VERIFY(RunModalLoop(dwFlags) == m_nModalResult);

               // hide the window before enabling the parent, etc.
               if (m_hWnd != NULL)
                    SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
          m_nModalResult = -1;

     if (bEnableParent)
          ::EnableWindow(hWndParent, TRUE);
     if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)

     // destroy modal window

     // unlock/free resources as necessary
     if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
     if (m_lpszTemplateName != NULL)

     return m_nModalResult;

basically don't do the dwFlags = MLF_SHOWONIDLE; if you wan't the dialog to stay hidden.

HOWEVER! I'll ask like the others did, why modal+hidden? if you want the app to freeze while its doing something, better to pop a window with a progress bar, or at least a "please wait" window, and then in your main window set a timer. (I assume in a different thread?) the timer can check the thread state, and when finished call a destroy on the "please wait" window..

Accepted Solution

VinExpert earned 150 total points
ID: 6473189

>May I ask why I should use a timer and can't do it in OnInitDialog?

I just placed the code in OnTimer, so that dialog window will be created by that time and hence ShowWindow(...) will be successfull in its job.

Yes, I too have faced prob will EndDialog(...). According to MSDN, EndDialog(...) takes some time to close the dialog. I had tried to solve that by placing some delay after calling the EndDialog(...) function!!!

But, Finally, I wrote two public functions for the dialog class which will call CDialog's OnOK() and OnCancel() functions. And I called these functions instead of EndDialog(...) call. I did not had time to dig into it. May be U can alo try this out.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question