Delay time after opening my frame Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
greg
Earned some good credits
Earned some good credits
Posts: 111
Joined: Fri Sep 12, 2008 7:17 am

Delay time after opening my frame

Post by greg » Fri Jul 30, 2010 11:54 am

I'm loading my frame.
How can I load wxMessageBox dialog after 1 sec.
and after next 1 sec. automatically close.

Thanks in advance.
Regards
Greg

jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche » Fri Jul 30, 2010 12:06 pm

You must not use the wxMessageBox function.

You can create a dialog like the one provided by the wxMessageBox function, and do this :

Code: Select all

wxSleep(1000);
MyMessageBox* msgBox = new MyMessageBox(...);
msgBox->Show();
wxSleep(1000);
msgBox->Destroy();
But you should put this code in a separate thread.

Or you can give a try to wxSplashScreen :

Code: Select all

MyApp::OnInit()
{
...
new wxSplashScreen(bitmap, 
    wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_TIMEOUT,
    1000, // 1000 ms
    frame, 
    wxID_ANY, 
    wxDefaultPosition, 
    wxDefaultSize,
    wxSIMPLE_BORDER|wxSTAY_ON_TOP);
  
wxYield();
...
return true;
}
Jérémie

catalin
Moderator
Moderator
Posts: 1512
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Delay time after opening my frame

Post by catalin » Fri Jul 30, 2010 12:44 pm

greg wrote:How can I load wxMessageBox dialog after 1 sec.
and after next 1 sec. automatically close.
You can derive from wx[Message]Dialog, add a wxTimer member, set it to trigger an event after a second, handle the event and show/close it.

See wxTimer, wxTimerEvent, wxMessageDialog, wxDialog.

p.s. showing a [modal] dialog from a secondary thread will crash your app. It is not supported with wxWidgets.

greg
Earned some good credits
Earned some good credits
Posts: 111
Joined: Fri Sep 12, 2008 7:17 am

Post by greg » Fri Jul 30, 2010 12:58 pm

thanks catalin
I can not understand Your solution in details.
Can you give me a little sample showing Your solution?
Regards
Greg

catalin
Moderator
Moderator
Posts: 1512
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Post by catalin » Fri Jul 30, 2010 1:05 pm

Sorry, I don't have time now.
What specific detail don't you understand?

Create your own class derived from wxDialog or wxMessageDialog.
Give it a wxTimer member variable and set that timer to fire events each second.
Handle wxTimer events and maybe keep track of them and when you consider appropriate Show it, then later close it etc.

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Fri Jul 30, 2010 1:30 pm

jfouche wrote: You can create a dialog like the one provided by the wxMessageBox function, and do this :

Code: Select all

wxSleep(1000);
MyMessageBox* msgBox = new MyMessageBox(...);
msgBox->Show();
wxSleep(1000);
msgBox->Destroy();
But you should put this code in a separate thread.
Bad advice, wxWidgets GUI classes are not thread-safe!
"Keyboard not detected. Press F1 to continue"
-- Windows

boydale1
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Jun 12, 2010 6:24 pm
Contact:

Post by boydale1 » Fri Jul 30, 2010 3:29 pm

Code: Select all

 wxDialog *dlg = new wxDialog(this, wxID_ANY, wxT("WAIT!"), wxDefaultPosition, wxDefaultSize); // could easily be changed to a wxMessageDialog
wxStaticText *text = new wxStaticText(dlg, wxID_ANY, wxT("\n\n    WAIT ONE SECOND   \n\n"), wxDefaultPosition, dlg->GetSize(), wxALIGN_CENTRE);
text->SetForegroundColour( wxColour(wxT("DARK RED")));
wxSleep(1000);       
dlg->Show();    
wxSleep(1000);
dlg->Destroy();
This is pretty much the same example. If your looking to reuse this 1 second message a lot than you should create a class with that functionality.

Code: Select all

#include <wx/wx.h>

class CTimedMessage : public wxMessageDialog
{
    public:
        enum IDs { ID_Timer }
        CTimedMessage(wxWindow *parent, int milliSeconds);
        virtual ~CTimedMessage();

        void OnTimer(wxTimerEvent& event);

        DECLARE_EVENT_TABLE()

    private:
        wxTimer *mTimer;
};

Code: Select all

#include "CTimedMessage.h"

BEGIN_EVENT_TABLE(CTimedMessage, wxMessageDialog)
    EVT_TIMER(ID_Timer, CTimedMessage::OnTimer)
END_EVENT_TABLE()

CTimedMessage::CTimedMessage(wxWindow* parent, int milliSeconds)
{
    this->Create(parent, wxT("WAIT A SECOND"), wxT("WAIT!"), wxOK);  // creates the messagebox

    mTimer = new wxTimer( this, ID_Timer );
    mTimer->Start( milliSeconds ); 
    this->ShowModal();
}

void CTimedMessage::OnTimer( wxTimerEvent& event )
{
    this->Destroy();
}
I did not test this at all, I just wrote this real quick to give you a general idea. I am not that familiar with wxTimer, but I believe the seconds you specify are how often the timer event occurs, so it will only occur once in this example.

Hope it helps

greg
Earned some good credits
Earned some good credits
Posts: 111
Joined: Fri Sep 12, 2008 7:17 am

Post by greg » Fri Jul 30, 2010 8:58 pm

Thanks guys for Your advice.
Now my problem is solved.
Regards
Greg

Post Reply