简体   繁体   English

wxWidgets:定期在wxFrame中重绘图像

[英]wxWidgets: Periodically redraw image in wxFrame

I have a MainFrame and to set a background image I have implemented my own OnPaint() function. 我有一个MainFrame,并设置了背景图像,我已经实现了自己的OnPaint()函数。

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. 启动我的应用程序时,由于事件的GetId()返回-201,因此将图像m_oBackgorund2设置为背景。

After that I start a thread that fires an event with either 0 or 1 as Id 之后,我启动了一个线程,该线程引发一个以01作为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. 该事件将OnPaint称为Correnty,并且根据ID设置位图。 However, the changes do not show in the UI. 但是,更改不会显示在UI中。 If I call Refresh() in the OnPaint() the background image is painted voer everything else. 如果我在OnPaint()调用Refresh() ,则将其他所有背景画成背景图像。

How can I update the Backgorund image of the wxFrame without having it paint over all other UI elements? 如何在不覆盖所有其他UI元素的情况下更新wxFrame的Backgorund图像?

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. 基本问题是您无法通过自己的代码发布绘画事件(或由基础GUI层处理的任何其他事件)。 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. 这些事件是由您自己使用的GUI库(即Windows,GTK +或Cocoa)生成的,并且窗口在获得它们后必须重新绘制,但是人为地创建此类事件不会使窗口自身重新绘制。 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. 要做到这一点的唯一方法是调用Refresh()它并Refresh()像所有的GUI功能,只能从主界面线程调用。

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. 因此,如果您真的想使用线程(对于计时器,似乎更自然的选择),则必须在主线程上发布自定义事件,该事件将刷新窗口,然后重新绘制窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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