EVT_PAINT not repainting whole window after SetSize Topic is solved

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
Peri
Experienced Solver
Experienced Solver
Posts: 91
Joined: Wed Nov 09, 2011 10:06 pm
Location: Seattle, USA

EVT_PAINT not repainting whole window after SetSize

Post by Peri »

In my app, I'm calling SetSize to change the size of a child window. (This is happening during a EVT_MOTION while the user is dragging a sizing handle.)

The child window is initially displayed correctly. As well, during dragging, the newly exposed parts of the child window are painting correctly. However, parts already displayed are not repainted.

My code relevant to painting, for the child window, currently consists of an empty EVT_ERASE_BACKGROUND handler:

Code: Select all

void SizeableTextCtrl::onEraseBackground (wxEraseEvent& WXUNUSED(event))
{
}
The EVT_PAINT handler looks something like this (I've simplified it):

Code: Select all

void SizeableTextCtrl::onPaint (wxPaintEvent& WXUNUSED(event))
{
  wxPaintDC dc (this);

  dc.DestroyClippingRegion();
  wxRect rect = GetRect();
  dc.SetPen (*wxRED);
  dc.SetBrush (*wxYELLOW);
  dc.DrawRectangle (0, 0, rect.width, rect.height);
}
Each time onPaint is called, I think the entire window should be entirely refilled with yellow and a red border. Instead, one sees a trail of red borders left behind that are not painted over as the window grows. I've tried with and without the call to DestroyClippingRegion. No difference.

I've also tried putting this code in onEraseBackground, but I get the same effect.

Thanks.
Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: EVT_PAINT not repainting whole window after SetSize

Post by Radek »

DrawRectangle() draws a rectangle, it does not fill. Use Clear() or put FloodFill() behind the DrawRectangle().
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: EVT_PAINT not repainting whole window after SetSize

Post by PB »

I believe that DrawRectangle fills the rectangle using the current brush. I think you should add wxFULL_REPAINT_ON_RESIZE to the style of your custom control. When you do that, everything seems to work as it should, tested with code attached below (Wndows XP).

Code: Select all

#include <wx/wxprec.h>

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

class RectPanel : public wxPanel
{
public:
    RectPanel(wxFrame* parent, const wxPen& pen, const wxBrush& brush)
        : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE | wxTAB_TRAVERSAL),
        m_pen(pen), m_brush(brush)
    {
        wxASSERT(m_pen.IsOk());
        wxASSERT(m_brush.IsOk());        
        
        SetBackgroundStyle(wxBG_STYLE_PAINT);

        Bind(wxEVT_PAINT, &RectPanel::OnPaint, this);
    }
private:
    wxPen   m_pen;
    wxBrush m_brush;
    
    void OnPaint(wxPaintEvent& event)
    {
        wxPaintDC dc(this);

        dc.SetPen(m_pen);
        dc.SetBrush(m_brush);
        dc.DrawRectangle(GetClientRect());
    }
};


class MyFrame : public wxFrame
{
public:
    MyFrame()
        : wxFrame(NULL, wxID_ANY, _("Test"))
    {                        
        wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);
        
        RectPanel* rp = new RectPanel(this, wxPen(*wxRED), wxBrush(*wxYELLOW));
        bSizer->Add(rp, 1, wxEXPAND);

        SetSizer(bSizer);
        Layout();
        Centre();     
    }	
};

/**** MyApp ****/
class MyApp : public wxApp
{
public:	
	virtual bool OnInit()
	{
		if (!wxApp::OnInit())
			return false;       	

        MyFrame* frame = new MyFrame();
        frame ->Show();

        return true;
	}
};

IMPLEMENT_APP(MyApp)
Peri
Experienced Solver
Experienced Solver
Posts: 91
Joined: Wed Nov 09, 2011 10:06 pm
Location: Seattle, USA

Re: EVT_PAINT not repainting whole window after SetSize

Post by Peri »

Yea! Adding wxFULL_REPAINT_ON_RESIZE works. That makes me happy.
Post Reply