首页 > 解决方案 > 使用 wxScrollWindow 时自定义按钮显示超出边界

问题描述

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

选项卡1 选项卡2 选项卡3

标签: c++scrollwxwidgetsonpaint

解决方案


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

在油漆处理程序中,您需要使用 awxPaintDC而不是wxClientDC

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

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

ScrolledWidgetsPane::ScrolledWidgetsPane中,您应该可以添加

this->ShowScrollbars(wxSHOW_SB_NEVER,wxSHOW_SB_NEVER);

禁用显示滚动条。


推荐阅读