0

In my mainwindow, I am setting the background Image and added few buttons using the wxBoxSizer in CreateWidgets. Now on clicking one of the buttons, it should replace the background Image and hide the old wxBoxSizer and create a new one.

After setting the new image it is always calling child OnPaint function not the parent OnPaint function.

MainFrame::MainFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{

    // set the frame icon
    SetIcon(wxICON(sample));
    wxString fileName = wxT("./images/image1.png");
    LoadBitmap(fileName);

    SetSize(size);

    CreateWidgets();
}

void MainFrame::LoadBitmaps(wxString filename) {

    
    if (!wxFile::Exists(filename))
        wxLogWarning("Can't find image files");

    wxImage::AddHandler(new wxPNGHandler);
    image.LoadFile(filename);

    size.SetHeight(image.GetHeight() + 60);
    size.SetWidth(image.GetWidth() + 25);
}

void MainFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
{
    wxPaintDC dc(this);
    wxBitmap bitmap(image);
    dc.DrawBitmap(bitmap, 5, 5, false);
}

void MainFrame::CreateWidgets() {

    wxBoxSizer *main_sizer = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer *button_sizer = new wxBoxSizer(wxHORIZONTAL);

    std::vector<wxString> fileNames;
    fileNames.push_back(wxT("./images/img.png"));

    std::vector<wxString> rollover_files;
    rollover_files.push_back(wxT("./images/rollover_img.png"));

    std::vector<wxImage> images;
    wxImage image1(fileNames[0]);

    wxImage rollover_image1(rollover_files[0]);

    wxBitmap bitmap1(image1);

    wxBitmap rollover_bitmap1(rollover_image1);

    const long id = 6000;

    MWCustomBitmap *bitmapCtrl1 = new MWCustomBitmap(this, bitmap1, TetraMeshingBitMapBtn, rollover_bitmap1, NULL, wxDefaultPosition, wxDefaultSize);

    button_sizer->Add(bitmapCtrl1, 0, wxALL, 10);
    
    main_sizer->AddSpacer(350);
    main_sizer->Add(button_sizer, wxSizerFlags().Center());
    this->SetSizer(main_sizer);
}

Here on clicking button Loadfile(filename) is not working.

void MWCustomBitmap::OnBtnClicked(wxCommandEvent &evt) {
    wxString filename = wxT("./images/img3.png");
    if (!wxFile::Exists(filename))
        wxLogWarning("Can't find image files");

    wxImage::AddHandler(new wxPNGHandler);
    
    ((MainFrame*)(m_pParentwindow))->GetImage().LoadFile(filename);
}

PS: I am learning wxWidgets.

badai shaibaz
  • 47
  • 1
  • 7
  • It's not clear at all what do you mean by "child OnPaint function". Also, hiding a sizer isn't usually what you want, but, again, it's not really clear what do you want in the first place, please try to describe it better. – VZ. Dec 29 '20 at 17:28
  • I have edited, all I want is to replace whatever I had in the frame and replace it with the new widgets but on the same frame. – badai shaibaz Dec 29 '20 at 18:27
  • @badaishaibaz, how do you bind to the `OnBTnClicked` event handler? – Igor Dec 30 '20 at 00:53
  • `EVT_BUTTON(TetraMeshingBitMapBtn, MWCustomBitmap::OnTetraMeshing)` – badai shaibaz Dec 30 '20 at 03:33

0 Answers0