Reputation: 13
I have made a class wxRectangleDrawing, which has 3 methods, 1 for catching the paint event and creating the dc object, one for drawing my desired rectangle with that dc object, and one for clearing that dc.
The only problem is I cannot clear that dc with my Clear() method, because dc is a local variable and not a class field/member. I looked up online, and as far as I understood you need to catch an EVT_PAINT in your function to create a dc in it (?) Don't know if that's correct.
Here's my .h and .cpp files
wxRectangleDrawing.h
#pragma once
#include <wx/wx.h>
class wxRectangleDrawing : public wxWindow
{
wxPoint position;
wxSize size;
wxBrush brushColor;
public:
wxRectangleDrawing(wxPanel* parent, wxPoint rectPosition, wxSize rectSize, wxColour rectColor);
void PaintEvent(wxPaintEvent& evt);
void DrawRectangle(wxDC& dc);
void Clear(wxDC& dc);
DECLARE_EVENT_TABLE()
};
and here's the .cpp one
wxRectangleDrawing.cpp
#include "wxRectangleDrawing.h"
BEGIN_EVENT_TABLE(wxRectangleDrawing, wxPanel)
// catch paint events
EVT_PAINT(wxRectangleDrawing::PaintEvent)
END_EVENT_TABLE()
wxRectangleDrawing::wxRectangleDrawing(wxPanel* parent, wxPoint rectPosition, wxSize rectSize, wxColour rectColor) : wxWindow(parent, wxID_ANY)
{
position = rectPosition;
size = rectSize;
brushColor = wxBrush(rectColor);
SetMinSize(size);
}
void wxRectangleDrawing::PaintEvent(wxPaintEvent& evt)
{
// depending on your system you may need to look at double-buffered
wxPaintDC dc(this);
DrawRectangle(dc);
}
void wxRectangleDrawing::DrawRectangle(wxDC& dc)
{
dc.SetBrush(brushColor);
dc.DrawRectangle(position.x, position.y, size.x, size.y);
}
void wxRectangleDrawing::Clear(wxDC& dc)
{
dc.Clear();
}
Upvotes: 1
Views: 91