Page 1 of 1

绘图问题,窗口在OnSizing时,OnPaint中绘制内容没有正常更新到窗口中。

Posted: Mon Mar 30, 2020 4:27 am
by 不二
一个简单的绘图例子,绘制了两个矩形
1、使用当前窗口的ClientRect范围,收缩50后绘制矩形;
2、使用固定位置及宽高绘制矩形;

在拉动窗口调整范围时,第1个绘制的矩形没有正常更新,第2个绘制矩形正常更新。
问题总结,使用固定值绘制的矩形可以正常显示,动态变化的值则不能,原因何在?

Code: Select all

class TestDrawFrame : public wxFrame {
public:
    TestDrawFrame();
    ~TestDrawFrame();

public:
    void OnPaint(wxPaintEvent& event);

private:
    wxDECLARE_EVENT_TABLE();
};

wxBEGIN_EVENT_TABLE(TestDrawFrame, wxFrame)
    EVT_PAINT(TestDrawFrame::OnPaint)
wxEND_EVENT_TABLE()

TestDrawFrame::TestDrawFrame()
    : wxFrame(nullptr, wxID_ANY, wxT("Draw Test"), wxDefaultPosition, wxSize(500, 300), wxDEFAULT_FRAME_STYLE|wxCLIP_CHILDREN)
{
    SetMinSize(wxSize(100, 100));
}

TestDrawFrame::~TestDrawFrame()
{
}

void TestDrawFrame::OnPaint(wxPaintEvent& event)
{
    wxBufferedPaintDC bufDC(this);

    PrepareDC(bufDC);

    bufDC.SetLogicalOrigin(0, 0);
    bufDC.SetAxisOrientation(true, false);
    bufDC.SetUserScale(1.0, 1.0);
    bufDC.SetMapMode(wxMM_TEXT);
    bufDC.SetBackgroundMode(wxBRUSHSTYLE_SOLID);
    bufDC.Clear();

    bufDC.SetBrush(*wxTRANSPARENT_BRUSH);
    bufDC.SetPen(*wxRED_PEN);

    /// draw rectangle with client rect.
    wxRect rc = GetClientRect();
    rc.Deflate(50, 50);
    bufDC.DrawRectangle(rc);

    /// draw rectangle with fixed position and size.
    bufDC.SetPen(*wxBLUE_PEN);
    bufDC.DrawRectangle(100, 100, 500, 400);
}
draw_test.png
draw_test.png (8.76 KiB) Viewed 6385 times

Re: 绘图问题,窗口在OnSizing时,OnPaint中绘制内容没有正常更新到窗口中。

Posted: Mon Mar 30, 2020 10:26 am
by doublemax
Add the wxFULL_REPAINT_ON_RESIZE style flag:

Code: Select all

TestDrawFrame::TestDrawFrame()
    : wxFrame(nullptr, wxID_ANY, wxT("Draw Test"), wxDefaultPosition, wxSize(500, 300), wxDEFAULT_FRAME_STYLE|wxCLIP_CHILDREN|wxFULL_REPAINT_ON_RESIZE)
{
    SetMinSize(wxSize(100, 100));
    SetBackgroundStyle(wxBG_STYLE_PAINT);    // avoid flicker
}

Re: 绘图问题,窗口在OnSizing时,OnPaint中绘制内容没有正常更新到窗口中。

Posted: Mon Mar 30, 2020 11:45 am
by 不二
Thanks for your help, it's working! :D
But I'm still confused about drawing with fixed value(blue rectangle) is working, and the dynamic value not(red rectangle)?

Re: 绘图问题,窗口在OnSizing时,OnPaint中绘制内容没有正常更新到窗口中。

Posted: Mon Mar 30, 2020 11:53 am
by doublemax
不二 wrote:
Mon Mar 30, 2020 11:45 am
But I'm still confused about drawing with fixed value(blue rectangle) is working, and the dynamic value not(red rectangle)?
When you increase the window size, the OS will only generate update rectangles for the small, new parts that become visible, not the whole window.

Re: 绘图问题,窗口在OnSizing时,OnPaint中绘制内容没有正常更新到窗口中。

Posted: Mon Mar 30, 2020 12:32 pm
by 不二
doublemax wrote:
Mon Mar 30, 2020 11:53 am
不二 wrote:
Mon Mar 30, 2020 11:45 am
But I'm still confused about drawing with fixed value(blue rectangle) is working, and the dynamic value not(red rectangle)?
When you increase the window size, the OS will only generate update rectangles for the small, new parts that become visible, not the whole window.
some small, new parts rectangles will be repainted after window size changed, but I think the two DrawRectangle() methods should have the same result, but that's not.
DrawRectangle(rc) -- with client rect, dynamic value, not fixed.
DrawRectangle(100, 100, 500, 400) -- with fixed value.