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

问题描述

我正在使用 wxScrolledWindow 类进行一些滚动。滚动工作正常。我也使用 wxNotebook 在选项卡之间切换。对于此示例,我有 2 个选项卡。第一个选项卡包含一个标题,然后是一个从 wxScrolledWindow 派生的 ScrolledWidgetsPane 类。第二个选项卡包含一个空白页。 现在,当我在 tab1 时一切正常,自定义按钮隐藏在标题后面(在屏幕截图中以红色显示)。但是当我切换到 tab2 然后回到 tab1 时,自定义按钮显示标题顶部的边界之外。我附上了突出情况的三个屏幕截图。请注意,在 tab1 内,我有一个垂直的 sizer。该垂直 sizer 首先包含一个高度为 40 的标题,然后在其下方包含一个 ScrolledWidgetsPane。 此外,我注意到只有当我使用 CustomButton::onPaint(wxPaintEvent&) 方法时才会发生这种情况。如果我注释 CustomButton 的 onPaint 方法内的代码,则自定义按钮不会与 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,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,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,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,wxALL,3);
        } 
        this->SetSizer(sizer);
        this->FitInside();
        this->SetScrollRate(5,5);

}

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

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

下面我附上了 3 个屏幕截图。

tab1

tab2

tab3

解决方法

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

在绘制处理程序中,您需要使用 wxPaintDC 而不是 wxClientDC

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

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

ScrolledWidgetsPane::ScrolledWidgetsPane中,您应该能够添加

this->ShowScrollbars(wxSHOW_SB_NEVER,wxSHOW_SB_NEVER);

禁止显示滚动条。