mfc >> Solved: How do I close a modal dialog programmatically

by Thomas Noone » Fri, 28 Nov 2003 01:42:17 GMT

This is how I solved it.

HWND hwnd = NULL;
CWnd *cwnd, *cwnd1;
cwnd = AfxGetApp()->m_pMainWnd->GetLastActivePopup();
if(cwnd != 0)
hwnd = cwnd->m_hWnd;
else
hwnd = NULL;

while(hwnd != NULL && hwnd != AfxGetApp()->m_pMainWnd->m_hWnd)
{
cwnd1 = cwnd->GetParent();
cwnd->SendMessage(WM_CLOSE, NULL, NULL);
cwnd=cwnd1;
if(cwnd != 0)
hwnd = cwnd->m_hWnd;
else
hwnd = NULL;
}

Thomas Noone < XXXX@XXXXX.COM > schrieb in im Newsbeitrag:
XXXX@XXXXX.COM ...
> Hi!
>
> I need to add a time-out feature to an existing MFC-VC++-App.
> My only problem at this point is finding any open modal dialogs, prior to
> shutting down/logging off.
>
> Using OnTimer:
>
> if(AfxGetApp()->m_pMainWnd->GetActiveWindow() != NULL)
> hwnd = AfxGetApp()->m_pMainWnd->GetActiveWindow()->m_hWnd;
> else
> hwnd = NULL;
>
> if(hwnd != NULL && hwnd != AfxGetApp()->m_pMainWnd->m_hWnd)
> {
> ::SendMessage(hwnd, WM_CLOSE, NULL, NULL);
> SetTimer(2,100,NULL);
> }
>
> works fine, if my app is running in the foreground.
>
> Once it's in the background, I haven't yet found a way to get a handle.
>
> Important:
> I do not want to get into "bookkeeping" of open dialogs etc. and I don't
> know the names of the dialogs.
>
> Any ideas?
>
> Thanks,
>
> Thomas
>
>




Similar Threads

1. How do I close a modal dialog programmatically

2. problems when a non modal dialog closes, and has an open Modal Dialog

I have a non-modal dialog box whos parent window is the main MDI window.
I then have a modal Dialog box that can popup with the nonmodal box as 
its parent.

If the Non-Modal Box gets closed (user removes its document from the MDI 
  interface.)

then A get an assert because DoModal is still funning, on a window that 
does not exist.

So My question is how do I stop domodal from running in a safe way?


thanks,

how

3. Trying to close modal dialog when app is closed

4. Modeless dialog closing a modal dialog problem

Hello!

I have a problem when a modeless CDialog is displaying a Modal dialog.
But while the modal dialog is displayed, My main window needs to close 
the modeless dialog (CMyModelessDialog) because it receives a certain 
message from the message MAP. It does so using the message 
myModelessDialog->PostMessage(WM_CLOSE);

So everytime the modeless dialog closes, the modal dialog cause a crash.
My modeless dialog is implemented the proper way (by overriding the 
PostNCDestroy , OnCancel and OnOK)

Is there anothere approach to this problem?

Thank you for any hints

Simon


MainWindow::Problem(lparam, wparam)
{
	modelessDialog->PostMessage(WM_CLOSE);
}


CMyModelessDialog::DisplayResult()
{
	if(error)
	{
	MyModalDialog d;
	d.DoModal;
	}
}

5. [Q] Modal dialogs don't appear when machine under load [SOLVED]

6. display of modal dialog behind modal dialog

I have to display one modal dialog behind the previous one in SDI
application.I have tried with SetWindowPos() But its not
working.please suggest me the right solution.

7. Closing Modal dialog - CSharp/C#

8. Closing modal dialog causes main window to flash/flicker

Hello and thanks for any help
I've noticed that my main application window has a bad Flash/Flicker sometimes when I close a modal dialog box. Sometimes it also loses focus, and gets placed behind another window that was not originally on top. I perform the dispose inside of the dialog, for instance when a user clicks on the Ok button as follows

		private void okButton_Click(object o, EventArgs e
		
			/*Perfrom some action*
			this.Dispose()
		

Does anyone know why this is happening? Thanks.