Re: wxPaintDC

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
Rohit Agarwal
Earned some good credits
Earned some good credits
Posts: 119
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: wxPaintDC

Post by Rohit Agarwal »

According to the docs,
wxPaintDC should be created on the stack within the handler for wxPAINT_EVT.

In a typical app such as mine,
the user does something on the keyboard or mouse and the app reacts in response via a paint.
The handler for the input event has to determine what visual updates should be sent to the user.
It generates a refresh which shows up as a paint_evt

In the handler for my paint evt, I should therefore be able to call
some function that knows how to handle this particular refresh request.

However, I can't call a function from my paint handler passing it the wxDC
because wxDECLARE_NO_COPY_CLASS(wxDC);

What that means is I have to take all the user input cases possible for my app
and put their visual response behaviour inside the evt_paint handler.
This will make a big mess of an evt_paint handler function and the architecture of my code.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxPaintDC

Post by PB »

You just need to pass a wxPaintDC from the wxPaintEvent handler to a helper function as a reference or pointer instead by value?

Code: Select all

#include <wx/wx.h>
#include <wx/dcbuffer.h>

class MyCanvas : public wxPanel
{
public:
    MyCanvas(wxWindow* parent)
        : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
    {
        SetBackgroundStyle(wxBG_STYLE_PAINT);
        Bind(wxEVT_PAINT, &MyCanvas::OnPaint, this);
    }
private:
    size_t m_paintEventCount{0};

    void OnPaint(wxPaintEvent&)
    {
        wxAutoBufferedPaintDC dc(this);

        dc.SetBackground(*wxBLACK_BRUSH);
        dc.Clear();

        PaintText(dc);
    }

    void PaintText(wxDC& dc)
    {
        wxDCTextColourChanger textChanger(dc, *wxGREEN);

        dc.DrawLabel(wxString::Format("Paint event #%zu", ++m_paintEventCount), GetClientRect(), wxALIGN_CENTER);
    }
};


class MyApp : public wxApp
{
public:
    bool OnInit() override
    {
        wxFrame*  frame = new wxFrame(nullptr, wxID_ANY, "Test");
        MyCanvas* canvas = new MyCanvas(frame);

        frame->Show();
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);
I assume that you understand that your wxPaintEvent handler must be able to redraw everything, e.g., when the paint event is triggered by the OS.
Rohit Agarwal
Earned some good credits
Earned some good credits
Posts: 119
Joined: Wed May 19, 2021 11:17 pm
Contact:

Re: wxPaintDC

Post by Rohit Agarwal »

OK.
Since the above code works, I should be able to figure this out.
EDIT: That worked. Thanks for the expeditious response.
Post Reply