Lehue
Lehue

Reputation: 435

Change image on StaticBitmap wxWidgets

I would like to have a window, in which a picture changes depending on what is happening during an infinite loop.

Imagine someone walking around and when he leaves a given track, the program should display an arrow towards the direction of the track. Therefore I have a program, which determines the distance between user and track, but I have no idea on how to update the image.

I use code::blocks with wxWidgets and think I have to use the wxStaticBitmap class. (If there is a better way, please tell me.)

I tried with:

while(true)
{
    updatePosition();
    if(userNotOnTrack)
    {
        if(trackRightOfUser)
        {
            StaticDirectionBitmap->SetBitmap("D:\\WindowsDgps\\WindowsDgpsGraphic\\arrow_right.png");
        }
    }
}

(Note that this snippet is mostly pseudocode, except the StaticDirectionBitmap part.)

On default the Bitmap has a "no_arrow" image. With this I get an error: error: no matching function for call to 'wxStaticBitmap::SetBitmap(const char [51])'|. I see from the documentation that this cannot work, but I have no idea what could.

If anyone knows how to handle this, I would be happy to hear. I remember a few years back, when I tried something similar in C# and failed completely because of thread safety... I hope it is not this hard in C++ with wxWidgets.

Upvotes: 1

Views: 2123

Answers (1)

New Pagodi
New Pagodi

Reputation: 3554

SetBitmap takes a wxBitmap parameter not a string. So the call should look something like:

SetBitmap(wxBitmap( "D:\\WindowsDgps\\WindowsDgpsGraphic\\arrow_right.png", wxBITMAP_TYPE_PNG) );

Make sure prior to making this call that the png handler has been added with a call like one of the following:

wxImage::AddHandler(new wxPNGHandler);

or

::wxInitAllImageHandlers();

The easiest place to do this is in the applications OnInit() method.

If you want update the static bitmap from a worker thread, you should throw a wxThreadEvent and then make the call to SetBitmap in the event handler. The sample here shows how to generate and handle these events.

Upvotes: 3

Related Questions