[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Expert Comment

by:Vinayak Kumbar
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

by:Vinayak Kumbar
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?

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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

Vinayak Kumbar 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 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