Skip to content

WxTimer

The wxTimer class allows you to execute code at specified intervals.

For example

class MyFrame : public wxFrame
{
public:
    ...
    void OnTimer(wxTimerEvent& event);
private:
    wxTimer m_timer;
    wxDECLARE_EVENT_TABLE();
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
wxEND_EVENT_TABLE()
MyFrame::MyFrame()
       : m_timer(this, TIMER_ID)
{
    m_timer.Start(1000);    // 1 second interval
}
void MyFrame::OnTimer(wxTimerEvent& event)
{
    // do whatever you want to do every second here
}

Redraw timer

일정 주기로 화면을 다시 그려야 할 경우 아래와 같이 onTimer()이벤트를 작성하면 된다.

PreviewPanel::PreviewPanel(wxWindow      * parent
                         , wxWindowID      id
                         , wxPoint const & pos
                         , wxSize  const & size)
        : wxPanel(parent, id, pos, size)
{
    _timer = new wxTimer(this, TIMER_EVENT_ID);
    _timer->Start(TIMER_INTERVAL_MILLISEC);
}

PreviewPanel::~PreviewPanel()
{
    _timer->Stop();
    delete timer;
}

void PreviewPanel::onPaint(wxPaintEvent & event)
{
    wxBufferedPaintDC dc(this);
    dc.Clear();
}

void PreviewPanel::onTimer(wxTimerEvent & event)
{
    Refresh();
    Update();
    // wxYield();
}

See also

Favorite site