I do not know how good they are, but you could look at those at wxWiki:
https://wiki.wxwidgets.org/Guides_%26_T ... ous_Guides
But the basics are very simple, as I said above:
1. You must always be able to draw everything that is needed from the paint event handler.
2. If you need to redraw something because the state/data changed, do not draw it e.g. from the user input handler. In the handler just update the state and force redraw, via Refresh() + Update().
3. Always create wxPaintDC (or its buffered equivalent) in the paint event handler and make sure it gets destroyed there, do not store it for future use (that goes for all wxDCs).
Simplest example of custom drawing, you may need to look up what some things (sucj as wxFULL_REPAINT_ON_RESIZE or SetBackgroundStyle()) are for in the official documentation
Code: Select all
#include <wx/wx.h>
#include <wx/dcbuffer.h>
class MyCanvas : public wxPanel
{
public:
MyCanvas(wxWindow* parent)
: wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
{
m_clicks = 0;
SetBackgroundStyle(wxBG_STYLE_PAINT);
Bind(wxEVT_PAINT, &MyCanvas::OnPaint, this);
}
void IncreaseClicks()
{
m_clicks++;
Refresh();
Update();
}
private:
int m_clicks;
void OnPaint(wxPaintEvent&)
{
wxSize size(GetClientSize());
wxPoint center(size.GetWidth() / 2, size.GetHeight() / 2);
wxAutoBufferedPaintDC dc(this);
wxDCPenChanger penChanger(dc, *wxRED_PEN);
wxDCTextColourChanger textChanger(dc, *wxBLUE);
dc.SetBackground(*wxWHITE_BRUSH);
dc.Clear();
dc.DrawLine(wxPoint(center.x, 0), wxPoint(center.x, size.GetHeight()));
dc.DrawLine(wxPoint(0, center.y), wxPoint(size.GetWidth(), center.y));
dc.DrawLabel(wxString::Format("Button clicks: %d", m_clicks),
GetClientRect(), wxALIGN_CENTER_VERTICAL | wxALIGN_CENTER);
}
};
class MyFrame : public wxFrame
{
public:
MyFrame()
: wxFrame(NULL, wxID_ANY, _("Test"))
{
wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);
m_canvas = new MyCanvas(this);
bSizer->Add(m_canvas, 1, wxEXPAND, 0);
wxButton* button = new wxButton(this, wxID_ANY, "Click me");
bSizer->Add(button, 0, wxEXPAND | wxALL, 5);
button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnButtonClicked, this);
SetSizer(bSizer);
}
private:
MyCanvas* m_canvas;
void OnButtonClicked(wxCommandEvent&)
{
m_canvas->IncreaseClicks();
}
};
class MyApp : public wxApp
{
public:
virtual bool OnInit()
{
(new MyFrame())->Show();
return true;
}
}; wxIMPLEMENT_APP(MyApp);