I create a custom control which Inherited wxWindow. the control works ok when I first show it in frame:
However, if I resize the frame, the custom button display incorrectly
note the text is not center in button, and there are white lines.
the control header:
Code: Select all
#ifndef WXDEMO_SCOREPAD_H
#define WXDEMO_SCOREPAD_H
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
class ScorePad : public wxWindow
{
public:
ScorePad(wxWindow *parent, wxWindowID id = wxID_ANY, const wxString &label="");
ScorePad();
bool Create(wxWindow *parent, wxWindowID id = wxID_ANY, const wxString &label="");
virtual ~ScorePad();
protected:
virtual wxSize DoGetBestSize() const;
void OnPaint(wxPaintEvent &event);
void render(wxDC &dc);
private:
void Init();
};
#endif //WXDEMO_SCOREPAD_H
The control cpp file:
Code: Select all
#include "ScorePad.h"
ScorePad::ScorePad(wxWindow *parent, wxWindowID id, const wxString &label):wxWindow(parent, id)
{
Init();
Create(parent, id, label);
}
ScorePad::ScorePad()
{
Init();
}
bool ScorePad::Create(wxWindow *parent, wxWindowID id, const wxString &label)
{
return true;
}
wxSize ScorePad::DoGetBestSize() const
{
// we need to calculate and return the best size of the widget...
return wxSize(120, 120);
}
void ScorePad::OnPaint(wxPaintEvent &event)
{
// draw the widget on a wxDC...
wxPaintDC dc(this);
render(dc);
}
void ScorePad::Init()
{
Bind(wxEVT_PAINT, &ScorePad::OnPaint, this);
}
ScorePad::~ScorePad()
{
}
void ScorePad::render(wxDC &dc)
{
wxSize size = GetSize();
dc.SetBrush(*wxRED_BRUSH);
dc.SetPen(*wxTRANSPARENT_PEN);
dc.DrawRoundedRectangle(0, 0, size.GetWidth(), size.GetHeight(), 5);
wxFont font(16, wxFONTFAMILY_ROMAN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
dc.SetFont(font);
dc.SetTextForeground(*wxWHITE);
dc.DrawLabel("2048", wxRect(0,0, size.GetWidth(), size.GetHeight()), wxALIGN_CENTER);
}
Code: Select all
wxPanel *panel = new wxPanel(this, wxID_ANY);
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
ScorePad *score_pad = new ScorePad(panel);
sizer->Add(score_pad, wxSizerFlags(1).Expand().Border(wxALL, 20));
panel->SetSizer(sizer);
sizer->SetSizeHints(this);