Page 1 of 1
subclass panel : custom draw and controls
Posted: Fri Sep 15, 2017 1:01 pm
by murena
Hello to everyone,
I have a problem :
I want to subclass wxPanel as my template for custom panel. In this derived wxPanel I can add normal controls as wxButton etc but I don't know how to add my custom draws (e.g. a border). In summary, I would mix normal wxControl and custom drawing in a wxPanel.
Can you help me?
Thanks
Stefano
Re: subclass panel : custom draw and controls
Posted: Fri Sep 15, 2017 1:27 pm
by doublemax
Basically, you need to catch the wxEVT_PAINT event and do your drawing in the event handler.
https://wiki.wxwidgets.org/Drawing_on_a_panel_with_a_DC
Re: subclass panel : custom draw and controls
Posted: Mon Sep 25, 2017 8:34 am
by murena
thank you for your reply.
i will try to explain better.
For a single panel, how to draw in the render context it is ok.
The problem is fired when I try to detect EV_PAINT in a super class. I Have a set of panel, each of them extends a super class custompanel.
I would write a piece of generic code for all subclasses in only one place (the superclass) e.g. to draw a common border.
More in details I have ( only a portion of code ):
//SUPERCLASS
class CustomPanel : public wxPanel, public Observer {
public:
void OnPaint(wxPaintEvent& event);
DECLARE_EVENT_TABLE()
};
// Event table for CustomPanel
BEGIN_EVENT_TABLE(CustomPanel, wxPanel)
EVT_PAINT(CustomPanel::OnPaint)
END_EVENT_TABLE()
//SPECIALIZED AND CONCRETE CLASS
class SystemDateTimePanel : public CustomPanel
{
}
//END OF CODE
In this real example, hence the my problem, the evt_paint of superclass is never fired.
thank you
Stefano
Re: subclass panel : custom draw and controls
Posted: Mon Sep 25, 2017 1:17 pm
by ONEEYEMAN
Hi,
Are you catching EVT_PAINT in the derived class?
Thank you.
Re: subclass panel : custom draw and controls
Posted: Mon Sep 25, 2017 2:24 pm
by murena
Hi,
no I am not.
So I have to catch the event in the derived class, and then? Could I catch the event in the derived class and then call prior the superclass method (common to all derived class) ? I will try this.
thank you.
Re: subclass panel : custom draw and controls
Posted: Mon Sep 25, 2017 4:36 pm
by doublemax
I would suggest to catch the paint event only in the base class.
Then add a virtual method that does the actual painting. That can be overriden by the subclasses.
Code: Select all
void CustomPanel::OnPaint( wxPaintEvent &evt )
{
wxPaintDC dc(this);
DoDraw( dc );
}
virtual void CustomPanel::DoDraw( wxDC &dc )
{
// do actual drawing here.
}
Re: subclass panel : custom draw and controls
Posted: Mon Sep 25, 2017 8:48 pm
by ONEEYEMAN
Hi,
Also, make sure you call Refresh()/Update() to send the paint event.
Thank you.