Custom wxWindow paint event not called. 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
fishnet37222
Experienced Solver
Experienced Solver
Posts: 74
Joined: Sat May 06, 2017 1:40 pm

Custom wxWindow paint event not called.

Post by fishnet37222 »

Hello,

I'm just learning wxWidgets and I'm facing an issue with a custom control I'm creating that's derived from wxWindow. I've created a method to handle the wxEVT_PAINT event, but it doesn't seem to be called. I set a breakpoint on the first line of paintEvent(), but it doesn't break.

I'm using the latest stable version of wxWidgets on Windows 10.

FNDie.h:

Code: Select all

#ifndef FNDIE_H
#define FNDIE_H

#include <wx/window.h> // Base class: wxWindow
#include <wx/wx.h>

class FNDie : public wxWindow
{
	int m_value;
	bool m_selected;
	bool m_enabled;
	bool m_mouseDown;
	
public:
	void paintEvent(wxPaintEvent& evt);
	void mouseDown(wxMouseEvent& evt);
	void mouseLeftWindow(wxMouseEvent& evt);
	void mouseUp(wxMouseEvent& evt);
	FNDie(wxFrame* parent, int id);
	~FNDie();
};

#endif // FNDIE_H
FNDie.cpp:

Code: Select all

#include "FNDie.h"

FNDie::FNDie(wxFrame* parent, int id)
{
	Bind(wxEVT_PAINT, &FNDie::paintEvent, this);
}

FNDie::~FNDie()
{
}

void FNDie::mouseDown(wxMouseEvent& evt)
{
	m_mouseDown = true;
	evt.Skip(true);
}

void FNDie::mouseLeftWindow(wxMouseEvent& evt)
{
	m_mouseDown = false;
}

void FNDie::mouseUp(wxMouseEvent& evt)
{
	if (m_mouseDown)
	{
		// handle mouse click.
		m_mouseDown = false;
	}
}

void FNDie::paintEvent(wxPaintEvent& evt)
{
	wxPaintDC dc(this);
	dc.SetBrush(wxBrush(*wxBLACK, wxBRUSHSTYLE_SOLID));
	dc.SetPen(wxPen(*wxBLACK, 5, wxPENSTYLE_SOLID));
	wxSize size = this->GetClientSize();
	dc.DrawRectangle(0, 0, size.GetWidth(), size.GetHeight());
}
User avatar
doublemax
Moderator
Moderator
Posts: 19161
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Custom wxWindow paint event not called.

Post by doublemax »

You're not calling the base constructor of the custom control (wxWindow).

Unrelated: You should change the constructor to take wxWindow* as parent instead of wxFrame* as a more general approach.
Use the source, Luke!
Post Reply