You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

wxScrolledWindow缩放滚动后绘制异常,如何正确绘制内容?

问题:wxWidgets滚动窗口缩放后滚动绘制错乱

我使用wxWidgets开发一款动画时间轴控件,该控件包含一个wxFrame及其子控件wxScrolledWindow。我希望通过鼠标滚轮修改时间轴窗口的虚拟尺寸以实现水平缩放,无滚动时缩放功能正常,但滚动后绘制内容出现错乱。

为排查问题,我进行了简单测试:绘制一条从窗口左上角到右下角的线条,结果显示无滚动时测试正常,缩放后向右滚动、再滚回左侧时测试均失败。

代码实现

头文件(.H)

class AnimationFrame : public wxFrame
{
public:
    AnimationFrame();

private:
    void onTimelineMouseWheel(wxMouseEvent& ev);
    void onTimelinePaint(wxPaintEvent& ev);

    // The scrolled window.
    wxScrolledWindow* m_timelineWindow;
};

实现文件(.CPP)

AnimationFrame::AnimationFrame()
{
    assert(wxXmlResource::Get()->LoadFrame(this, NULL, "AnimationFrame"));

    m_timelineWindow = (wxScrolledWindow*)wxWindow::FindWindowByName("TimelineScrolledWindow", this);
    assert(m_timelineWindow);

    wxPanel* headerPanel = (wxPanel*)wxWindow::FindWindowByName("HeaderPanel", this);
    assert(headerPanel);

    // Adjust panel sizes.
    {
        headerPanel->SetMaxSize(wxSize(headerPanel->GetMaxSize().x, 40));
        m_timelineWindow->SetMaxSize(wxSize(m_timelineWindow->GetMaxSize().x, 100));
        this->SetMinSize(wxSize(this->GetSize().x, 250));
        this->SetSize(wxSize(this->GetSize().x, 250));
        this->SetMaxSize(wxSize(this->GetSize().x, 250));
    }

    // Timeline events from the scrolled window.
    m_timelineWindow->Bind(wxEVT_MOUSEWHEEL, &AnimationFrame::onTimelineMouseWheel, this);
    m_timelineWindow->Bind(wxEVT_PAINT, std::bind(&AnimationFrame::onTimelinePaint, this, std::placeholders::_1));
}


void AnimationFrame::onTimelineMouseWheel(wxMouseEvent& ev)
{
    static const nbFloat32 scrollIntensity = 1.20f;
    const nbFloat32 delta = scrollIntensity * (nbFloat32)(ev.GetWheelRotation() / ev.GetWheelDelta());

    const nbFloat32 sizeFactor = delta >= 0.0 ? delta : 1.0f / -delta;

    const wxSize currentVirtualSize = m_timelineWindow->GetVirtualSize();
    wxSize targetVirtualSize = wxSize((nbUint32)((nbFloat32)currentVirtualSize.x * sizeFactor), currentVirtualSize.y);
    targetVirtualSize.x = std::min(targetVirtualSize.x, 5000);
    targetVirtualSize.x = std::max(targetVirtualSize.x, m_timelineWindow->GetSize().x);

    m_timelineWindow->SetVirtualSize(targetVirtualSize);

    this->Refresh();
}


void AnimationFrame::onTimelinePaint(wxPaintEvent& ev)
{
    // Init the DC.
    wxPaintDC dc(m_timelineWindow);
    PrepareDC(dc);// from the scrolling sample. Doesnt seems to do anything.

    /*
    wxFont font(12, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
    dc.SetFont(font);
    dc.SetBackgroundMode(wxTRANSPARENT);
    dc.SetTextForeground(*wxBLACK);
    dc.SetTextBackground(*wxWHITE);
    */

    // Now draw line.
    dc.DrawLine(wxPoint(0, 0), wxPoint(m_timelineWindow->GetSize().x, m_timelineWindow->GetSize().y));
}

附加信息

该控件通过XRC格式从C++加载,资源由wxFormBuilder生成。


内容的提问来源于stack exchange,提问作者TheChamp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 13:35:45