Reputation: 67
I am trying to learn WxWidgets with C++ (I am very new at this), and I have created a window with a black background color and has a big red "X" on it. I have to edit the code so that the "X" changes its size with the window as I am resizing the window. How can I properly implement the resize event handler to this code?
Here's a screenshot of what my code produces: https://i.sstatic.net/d52yY.jpg
Here's what I have so far"
#include <wx/wx.h>
#include <wx/dcbuffer.h>
class MyCanvas : public wxWindow
{
public:
MyCanvas(wxWindow* parent)
: wxWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
{
SetBackgroundStyle(wxBG_STYLE_PAINT);
Bind(wxEVT_PAINT, &MyCanvas::OnPaint, this);
}
private:
void OnPaint(wxPaintEvent&)
{
wxAutoBufferedPaintDC dc(this);
dc.SetPen(*wxRED_PEN);
dc.SetBrush(*wxBLACK_BRUSH);
dc.DrawLine(0,0,485,485);
dc.DrawLine(0, 485, 485, 0);
}
};
class MyFrame : public wxFrame
{
public:
MyFrame()
: wxFrame(NULL, wxID_ANY, _("Resizable X"), wxDefaultPosition, wxSize(500, 525))
{
wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);
bSizer->Add(new MyCanvas(this), 1, wxEXPAND);
SetSizer(bSizer);
}
};
/**** MyApp ****/
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
MyFrame* frame = new MyFrame();
frame->Show();
return true;
}
};
IMPLEMENT_APP(MyApp)
Upvotes: 0
Views: 1052
Reputation: 22688
The simplest way to implement your resize handler is to do the following in MyCanvas
ctor:
Bind(wxEVT_SIZE, [this](wxSizeEvent& event) { Refresh(); event.Skip(); });
This will fully refresh your canvas every time it is resized, i.e. will generate a wxEVT_PAINT
that will result in a call to your existing OnPaint()
handler.
Of course, for this to be actually useful, your OnPaint()
should take the current window size into account, i.e. use GetClientSize()
instead of hardcoded 485
.
Upvotes: 2