CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2009
    Posts
    16

    Angry How to make messagebox with automatic timer?

    Hi:

    So I am programming in win32. I want to make a message box that would destroy itself after a certain amount of time, even if the user does not click on it. How would I do that?

    When I searched on the web regarding this question, the only thing I was able to find was to create a new form. But when I tried doing that by going to the Solution Explorer, and clicking "add->Windows Form", it gave me some kind of errors: "Object doesn't support this property or method".

    How else could I mimic a messagebox, so it will be just like a messagebox except it will have some kind of automatic timer in it?

    PS: I don't want a dialog box because it is modal.

    Any help will be appreciated. Thanks.

    -Sheri

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to make messagebox with automatic timer?

    It's unclear what language you are programming. You state that you are programming in Win32, but then you say that you are attempting to create a new form (form implies .net).

    Btw, a dialog box doesn't have to be modal - it can be modeless.

    In general, to solve your problem, you create a dialog and in it's initialization (depending on the language used), you fire off a timer. When the timer triggers, you close the dialog (or destroy it if it's modeless).

  3. #3
    Join Date
    Jul 2009
    Posts
    16

    Re: How to make messagebox with automatic timer?

    Thank you.

    I made a modeless dialog box, and this works. But I'm still wondering, there's no way to just create a messagebox that will destroy itself after certain amount of time? I'm not doing .net, so I guess a new form is not possible.

    -Sheri

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to make messagebox with automatic timer?

    Quote Originally Posted by shericn View Post
    Thank you.

    I made a modeless dialog box, and this works. But I'm still wondering, there's no way to just create a messagebox that will destroy itself after certain amount of time? I'm not doing .net, so I guess a new form is not possible.

    -Sheri
    Of course it's possible to write a self closing dialog.

    However, we can't help you if you don't answer the questions we've asked.

    What language are you programming in?
    What development environment are you using?

  5. #5
    Join Date
    Jul 2009
    Posts
    16

    Re: How to make messagebox with automatic timer?

    I'm programming in C/C++, using Windows Mobile development environment.

    Thanks for helping.

    -Sheri

  6. #6
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    Re: How to make messagebox with automatic timer?

    Hello!

    I was thinking about you problem for some time. I'll speak in terms of WinAPI but you could find smth like this in Windows Mobile (possibly).

    Create a MessageBox() with "strange" title, say "Title ~~~my msg box to be found~~~"
    Start a tread just before you create a MessageBox(), wait a bit in a tread, then FindWindow().

    Play with it in a tread. Close the box in a tread or modify it's content (but it doesn't seem to be portable).

    Pay attention on situations when user closed your MessageBox.

    I never tried this, but it seems to be logical. If it helped make me a note.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to make messagebox with automatic timer?

    In Win32, you handle the WM_INITDIALOG message in your dialog proc. Inside the handler for this message, you create a timer using SetTimer( ) api. Set this timer to fire every 1 second.

    Add a WM_TIMER handler. Inside this handler, increment a count value (which needs to be static or global) and when you've exceeded the count value, send yourself a WM_CLOSE message to close the dialog.

    Here's an example of an auto-timeout status dialog using ATL. Sure, the actual code is a bit different than straight Win32, but the concept is the same.

    Code:
    class CStatusDlg : public CDialogImpl<CStatusDlg>
    {
    public:
    enum { IDD = IDD_STATUS };
    CStatusDlg(LPCSTR szMessage, constlong lTimeout)
      : m_sMessage( szMessage ),
        m_lTimeout( lTimeout )
    {
    }
     
      BEGIN_MSG_MAP(CStatusDlg)
        MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
        COMMAND_ID_HANDLER( IDOK, OnOk )
        MESSAGE_HANDLER( WM_CLOSE, OnClose )
        MESSAGE_HANDLER( WM_TIMER, OnTimer )
      END_MSG_MAP()
     
      LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, 
    LPARAM lParam, BOOL& bHandled)
      {
        m_nCounterIndex = m_lTimeout;
    
        /* other control initialization removed for code sample */
     
    // Start the timer
        m_nTimerID = SetTimer(TIMER_IDEVENT, 1000); // one second interval
     
        return 1;
      }
     
      LRESULT OnOk(WORD, WORD, HWND, BOOL&)
      {
        SetStatus();
    
        EndDialog(IDOK);
        return 0;
      }
     
      LRESULT OnClose( UINT, WPARAM, LPARAM, BOOL& )
      {
        EndDialog(IDCANCEL);
        return 0;
      }
     
     
      LRESULT OnTimer( UINT nID, WPARAM wParam, LPARAM lParam, BOOL& )
      {
        if(m_nCounterIndex > 1)
        {
          CString sMsg;
          sMsg.Format( _T(""This dialog will auto-close in &#37;d seconds."), m_nCounterIndex);
          m_StaticStatusCtrl.SetWindowText( sMsg );
        }
        else
        {
          KillTimer( m_nTimerID );
          EndDialog( IDOK );
        }
        m_nCounterIndex--;
        return 0;
      }
     
    private:
      constlong m_lTimeout;
      UINT m_nTimerID;
      UINT m_nCounterIndex;
    };


  8. #8
    Join Date
    Jul 2009
    Posts
    16

    Re: How to make messagebox with automatic timer?

    Hi andrey_zh and Arjay:
    Both of your methods are similar, just create a timer, and destroy the window by first using FindWindow(), and if the window exists, then destroy it, right? I tried doing this, and it works very well. Thank you.
    -Sheri

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured