Custom control is not drawn correctly when I resize the frame

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
quanzhuo
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Nov 10, 2020 9:58 am

Custom control is not drawn correctly when I resize the frame

Post by quanzhuo »

Hi, guys:

I create a custom control which Inherited wxWindow. the control works ok when I first show it in frame:
Image
However, if I resize the frame, the custom button display incorrectly
Image
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);
}
The code to show it:

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);
    
Thank you
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Custom control is not drawn correctly when I resize the frame

Post by PB »

Such issues are usually avoided by creating the control with style flag wxFULL_REPAINT_ON_RESIZE. Your screenshot looks a bit odd though but trying it should not hurt.

BTW, you should actually implement Create() which is used together with the default ctor in two-step creation.
quanzhuo
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Nov 10, 2020 9:58 am

Re: Custom control is not drawn correctly when I resize the frame

Post by quanzhuo »

PB wrote: Fri May 07, 2021 7:53 am Such issues are usually avoided by creating the control with style flag wxFULL_REPAINT_ON_RESIZE. Your screenshot looks a bit odd though but trying it should not hurt.

BTW, you should actually implement Create() which is used together with the default ctor in two-step creation.
It worked !

Thank you
Post Reply