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

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
不二
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Mar 30, 2020 2:35 am

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

Post 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 16740 times
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post 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
}
Use the source, Luke!
不二
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Mar 30, 2020 2:35 am

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

Post 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)?
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax »

不二 wrote: Mon Mar 30, 2020 11:45 amBut 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.
Use the source, Luke!
不二
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Mar 30, 2020 2:35 am

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

Post by 不二 »

doublemax wrote: Mon Mar 30, 2020 11:53 am
不二 wrote: Mon Mar 30, 2020 11:45 amBut 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.
Post Reply