Question

I have the following code given in the book "Cross-Platform GUI Programming with wxWidgets" which I'm reading:

BEGIN_EVENT_TABLE(MyWindow, wxWindow)
  EVT_ERASE_BACKGROUND(MyWindow::OnErase)
END_EVENT_TABLE()

void MyWindow::OnErase(wxEraseEvent& event)
{
    wxClientDC* clientDC = NULL;
    if (!event.GetDC())
        clientDC = new wxClientDC(this);

    wxDC* dc = clientDC ? clientDC : event.GetDC() ;

    wxSize sz = GetClientSize();
    wxEffects effects;
    effects.TileBitmap(wxRect(0, 0, sz.x, sz.y), *dc, m_bitmap);

    if (clientDC)
        delete clientDC;
}

This code doesn't show how do I load a bitmap so after some searching on google I came up with:

wxBitmap m_bitmap;
bool result = m_bitmap.LoadFile("D:\image.png", wxBITMAP_TYPE_PNG);

But this returns a boolean result of false which means the function LoadFile was failed to load the file. I have also tried to load a BMP file which fails too.

One another problem I'm having is that wxEffects is flagged deprecated by the complier warning.

Was it helpful?

Solution

You need to register the PNG image handler to be able to load PNG bitmaps. The simplest way to do is to call wxInitAllImageHandlers() function at some point during your application initialization, e.g. in your overridden MyApp::OnInit().

As for wxEffects being deprecated, this is just because the book is rather old and quite a few things have changed since then. I strongly recommend you to read the overview of important changes if you are using wxWidgets 3.0. And if you don't, I recommend you even stronger to use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top