Link to home
Start Free TrialLog in
Avatar of fredmastro
fredmastroFlag for United States of America

asked on

Auto Closing A Diaglog Box/Message Box

How do I auto close a diaglog box and a message box. Are there two differnt commands if one's a message box? I'm trying to display a bitmap in a dialog box, or message box, and I don't know how to auto close it. I'm a beginner. Thanks for the help.
Avatar of galkin
galkin

To close dialog box you need to send to it either IDCANCEL or IDOK WM_COMMAND messages SendMessage(WM_COMMAND, IDCANCEL)
To close message box the idea is the same but depends on message box style. If its style is MB_OK send IDOK if MB_YES send IDYES.
Avatar of fredmastro

ASKER

Start a timer, then when the timer has reached the limit, use PostMessage, and send a WM_CLOSE.  This will close the dialog.  We do something similar with a thread based progress bar.


The problem with a message box is that it sits and waits until the user dismisses it.  Your timer won't respond until the message box is dismissed anyway.  You really need a separate thread.

Dialog boxes are easy.. They can be written so the they will kill themselves after a certain time.

eg.
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CMyDialog)
...
ON_WM_TIMER()
...
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CMyDialog::OnInitDialog() {
  BOOL ok = CDialog::OnInitDialog();
  SetTimer((UINT)this, 10*1000, NULL);      // ten seconds
  return ok;
}

void CMyDialog::OnTimer(UINT nIDEvent) {
  if (nIDEvent == (UINT)this)      {
    CWnd* pParentFrame = GetParentFrame();
    DestroyWindow();
    if (pParentFrame) pParentFrame->SetActiveWindow();
  }
}

Perhaps you should use dialogs instead of message boxes so you can have more control over how they work.

If using post message is not what you need, then exactly what are you trying to do?  Both answers you received will solve the problem you listed, The first, if there are buttons there then second if there are not buttons.  

Can you explaine your problem a little better?
EE has stuffed up the questions and answers again !!!!!  Aaarrgghh!!

My answer has disappeared.  I had posted some source code for you and now bobplace's comments appear as my answer.

I hope Linda and the EE team can retrieve it so that I don't need to do all that work again.  If it doesn't magically re-appear, I'll post have to do some more work and post my answer as a comment again.
I'll try to be more specific. I added a WM_CLOSE as suggested to me CSplshDlg class which is the dialog I want to close after 5 seconds.
Here is my WM_CLOSE section in my CSplshDlg class.

void CSplshDlg::OnClose()
{
      // TODO: Add your message handler code here and/or
        // call default
      
      CDialog::OnClose();
}

Now I have the CSplshDlg come up first, but I want it to close after 5 seconds and then load CMCADlg.  What would I put in this code segment above to do that? Do I have to add CMCADlg to CApp class or can I load it from here.
It would be best if you add the splash to the CMCADlg ( on it's OnInitDialog() ) and not as suggested, for the following reasons:
1. add to the OnClose of the splash dialog - a bizzare solution, is might work, but do you want your splash to open the programs main dialog or the oposite? if it is importent to show the CMCADlg only after the splash ends then hide it till then or use the splash's DoModal on the CMCADlg OnInitDialog() since he is not visible there yet.
2. Add to the app - The application should have the m_pMainWnd set to a valid value if you want things to work properly, so:
it is recommended if you know exactly what to do, if you don't, then don't mess with it.  You can add to your project a splash window component through the add-to-project/components-and-controls menu, that does all the work! although the code inserted by the component is a usual Microsoft low quality one, it works.
I'm going to try that code you gave me RONSLOW.
RONSLOW I would like to grade your answer. If you can supply a second answer since the first one somehow got rejected. I couldn't see it bofore, but I can now.  When you respond could you mention how I get the timer to open another dialog when it destroys the first dialog. I put your code in but I keep getting "unkown character '0xa0'".  Do I have to #include something? I've increased the points for you to 75.
The code I gave needs to be merged into yor dialog.  And appropriate changes made to your .h file.

This code came from my splash dialog class.  If I had known that is what you wanted, I could have posted the whole thing ... Or suggested you thy the splash screen component in VC (once you fix up the bugs documented in the KB )

Let me know if you would like my splash dialog class

ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial