How to hide modal dialog?

Posted on 2001-08-31
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

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?
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.


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 50 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 85
withoutTen challenge 14 120
zeroMAx challenge 20 100
sumHeights  challenge 17 70
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

785 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