简体   繁体   中英

wxWidgets: Periodically redraw image in wxFrame

I have a MainFrame and to set a background image I have implemented my own OnPaint() function.

OnPaint(wxPaintEvent& roEvent)
{
    if (!m_oBackgorund1.IsOk() || !m_oBackgorund2.IsOk())
        return;
    wxPaintDC paintDC(this);

    switch (roEvent.GetId())
    {
    case 0:
        paintDC.DrawBitmap(m_oBackgorund1, 0, 0);
        break;
    default:
        paintDC.DrawBitmap(m_oBackgorund2, 0, 0);
        break;
    }
}

The two bitmaps are correctly loaded previously. When starting my application the image m_oBackgorund2 is set as a background because the event's GetId() returns -201.

After that I start a thread that fires an event with either 0 or 1 as Id

{
    nId = !nId;
    wxPaintEvent oPaintEvent;
    oPaintEvent.SetId(nId);
    wxPostEvent(GetParent(), oPaintEvent);
}

The OnPaint is called correnty by that event and the bitmap is set according to the Id. However, the changes do not show in the UI. If I call Refresh() in the OnPaint() the background image is painted voer everything else.

How can I update the Backgorund image of the wxFrame without having it paint over all other UI elements?

Thanks!

You need to change your approach because what you're doing is not going to work.

The basic problem is that you can't post paint events (or any other event processed by the underlying GUI layer) from your own code. These events are generated by the GUI library you use itself (ie Windows, GTK+ or Cocoa) and the windows have to be repainted when they get them, but artificially creating such events doesn't make the window repaint itself. The only way to do this is to call Refresh() on it and Refresh() , like all the GUI functions, can only be called from the main GUI thread.

So if you really want to use threads (and for what you're doing a timer would seem like a much more natural choice), you must post a custom event to the main thread which would then refresh the window which would then repaint itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM