使用 wxScrollWindow 时自定义按钮显示在边界外

问题描述 投票:0回答:1

我正在使用

wxScrolledWindow
类做一些滚动。滚动工作正常。我也在使用
wxNotebook
在选项卡之间切换。对于这个例子,我有 2 个选项卡。第一个选项卡包含一个标题,然后是一个派生自
ScrolledWidgetsPane
wxScrolledWindow
类。第二个选项卡包含一个空白页。
现在,当我在 tab1 时,一切正常,自定义按钮隐藏在标题后面(在屏幕截图中以红色显示)。但是当我切换到 tab2 然后返回到 tab1 时,自定义按钮显示在标题顶部的边界之外。我附上了突出显示情况的三个屏幕截图。请注意,在 tab1 内部,我有一个垂直尺寸调整器。该垂直 sizer 首先包含一个高度为 40 的标题,然后在其下方包含一个
ScrolledWidgetsPane
.
此外,我注意到只有当我使用
CustomButton::onPaint(wxPaintEvent&)
方法时才会发生这种情况。如果我在
onPaint
CustomButton
方法中注释代码,那么自定义按钮不会重叠到 tab1 的标题部分。
CustomButton::onPaint
里面的代码如下:

void CustomButton::onPaint(wxPaintEvent &event)
{
        *If i comment the next three statements then code works fine but if i use them in the program then the above
        problem happens*/
        wxClientDC dc(this);
        dc.SetBrush(wxBrush(wxColour(100, 123,32), wxBRUSHSTYLE_SOLID));
        dc.DrawRectangle(0, 0, 100, 40);
}

FirstPage类中的代码(对应tab1的内容)如下:

First_Page::First_Page(wxNotebook *parent): wxPanel(parent, wxID_ANY)
{
        
        wxBoxSizer *verticalSizer = new wxBoxSizer(wxVERTICAL);
        wxPanel *headerPanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 40));
        headerPanel->SetBackgroundColour("red");

        verticalSizer->Add(headerPanel, 0, wxEXPAND, 0);
        
        
        wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
        SetBackgroundColour(wxColour(233,233,233));
        ScrolledWidgetsPane* my_image = new ScrolledWidgetsPane(this, wxID_ANY);
        sizer->Add(my_image, 1, wxEXPAND);

        verticalSizer->Add(sizer, 1, wxEXPAND, 0);
       

        
        SetSizer(verticalSizer);
}


ScrolledWidgetsPane里面的代码如下:

ScrolledWidgetsPane::ScrolledWidgetsPane(wxWindow* parent, wxWindowID id) : wxScrolledWindow(parent, id)
{
      
       wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
       SetBackgroundColour("blue"); 
        // add a series of widgets
        for (int w=1; w<=120; w++)
        {
            
            CustomButton *b = new CustomButton(this, wxID_ANY);
            sizer->Add(b, 0, wxALL, 3);
        } 
        this->SetSizer(sizer);
        this->FitInside();
        this->SetScrollRate(5, 5);

}

我该如何解决这个问题,这是什么原因?请注意,这仅在我使用 CustomButton 的 onPaint 方法时发生。

附加问题:有没有办法不显示窗口右侧可见的滚动缩略图?那就是隐藏(或不显示)拇指但仍然具有滚动功能。

下面我附上了3张截图。

c++ scroll wxwidgets onpaint
1个回答
2
投票

我该如何解决这个问题,这是什么原因?注意 只有当我使用 CustomButton 的 onPaint 方法时才会发生这种情况。

在油漆处理程序中,您需要使用

wxPaintDC
而不是
wxClientDC

void CustomButton::onPaint(wxPaintEvent &event)
{
        wxPaintDC dc(this);
...
}

附加问题:有没有办法不显示窗口右侧可见的滚动条?那就是隐藏(或不显示)拇指但仍然具有滚动功能。

ScrolledWidgetsPane::ScrolledWidgetsPane
,你应该可以添加

this->ShowScrollbars(wxSHOW_SB_NEVER,wxSHOW_SB_NEVER);

禁用显示滚动条。

© www.soinside.com 2019 - 2024. All rights reserved.