wxWidgets:处理wxEVT_PAINT

我们仍然以继承于wxFrame的MyFrame作为例子。

MyFrame.h:

class MyFrame : public wxFrame
{
    ......
private:
    ......
    void OnPaint(wxPaintEvent &event);
};

MyFrame.cpp

MyFrame :: MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
              : wxFrame(NULL, wxID_ANY, title, pos, size)
{
    // 设置窗口默认背景色
    SetBackgroundColour(*wxWHITE);
    // Bind paint event
    Bind(wxEVT_PAINT, &MyFrame::OnPaint, this);
}

// paint event handler
void MyFrame :: OnPaint(wxPaintEvent &event)
{
    wxPaintDC dc(this);
    dc.SetTextBackground(*wxRED);
    dc.SetTextForeground(*wxYELLOW);
    dc.SetBackgroundMode(wxSOLID);
    dc.DrawText(L"Exercise 1", 0, 0);
}

上面的代码中,绘图前将文字背景模式设置为wxSOLID,否则无法正确显示出文字背景色。

另外,在MyFrame的构造函数中将窗口背景设置为白色。在Paint前,Windows将会用该颜色填充无效区域。

原文地址:https://www.cnblogs.com/byeyear/p/3493143.html