【问题标题】:Hiding the Scrollbar while allowing scrolling with the Mouse Wheel in a FlowLayoutPanel隐藏滚动条,同时允许在 FlowLayoutPanel 中使用鼠标滚轮滚动
【发布时间】:2021-06-05 15:05:31
【问题描述】:

我正在尝试创建一个动态面板,我可以在其中添加控件并在控件离开面板高度时滚动,同时还隐藏滚动条。

我正在使用 FlowLayoutPanel,并且不断向其中添加自定义面板,并将它们的 Width 设置为父容器的 Width
我还将其AutoScroll 属性设置为true

但是,仍然存在一个问题。如何隐藏该死的滚动条?他们两个。

我尝试了以下方法:

this.lobbiesPanel.AutoScroll = false;
this.lobbiesPanel.HorizontalScroll.Visible = false;
this.lobbiesPanel.VerticalScroll.Visible = false;
this.lobbiesPanel.AutoScroll = true;

令我失望的是,它没有按预期工作。滚动条仍然可见。 如何隐藏滚动条,同时仍保持使用鼠标滚轮滚动的能力?

【问题讨论】:

标签: c# .net winforms flowlayoutpanel


【解决方案1】:

由于您只需要隐藏 FlowLayoutPanel 的 ScrollBars,而不是用您自己的 Controls 替换 ScrollBars,因此您可以构建一个从 FlowLayoutPanel 派生的自定义 Control。

自定义控件需要一些祖先没有的功能:

  • 必须是可选择的
  • 必须接收鼠标输入
  • 如果鼠标指针悬停在子控件上时,如果鼠标滚轮旋转应该可以滚动,否则填充时不会滚动。

要使其可选择并接收鼠标输入,您可以将其添加到其构造函数中:

SetStyle(ControlStyles.UserMouse | ControlStyles.Selectable, true);

要使其无论鼠标指针位于何处都能滚动,它需要预先过滤WM_MOUSEWHEEL 消息,可能还有WM_LBUTTONDOWN 消息。
您可以使用IMessageFilter 接口在消息发送之前对其进行预过滤并对其采取行动(这可能很棘手,您不能贪婪,并学习何时需要放手或保留消息)。

当收到WM_MOUSEWHEEL 消息并显示它已定向到您的控件时,您可以将其发送到 FlowLayoutPanel。

现在,有一些骇人听闻的部分:一个 ScrollableControl 非常努力地显示它的 Scrollbars 而你(有点)需要它们,因为这个 Control 有一种非常奇怪的方式来计算它的 PreferredSize(整个区域子控件占用的控件),它会根据FlowDirection 更改,而且没有真正的方法来管理标准滚动条:您可以摆脱它们或隐藏它们。
或者您将它们替换为您自己设计的控件,但这完全是另一回事。

要隐藏滚动条,常用的方法是调用ShowScrollBar函数。
int wBar 参数指定隐藏/显示哪个滚动条。
bool bShow 参数指定是显示 (true) 还是隐藏 (false) 这些滚动条。

  • FlowLayoutPanel 会尝试在特定条件下显示其 ScrollBars,因此您需要捕获一些特定消息并每次调用 ShowScrollBar(您不能只调用此函数一次就忘记它)。

这是一个测试自定义控件,它实现了所有这些东西:
(它是工作代码,但不完全是生产级:你必须稍微处理一下,我想,让它在特定条件/用例中表现得像你喜欢的那样)

using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

[DesignerCategory("code")]
public class FlowLayoutPanelNoScrollbars : FlowLayoutPanel, IMessageFilter
{
    public FlowLayoutPanelNoScrollbars() {
        SetStyle(ControlStyles.UserMouse | ControlStyles.Selectable, true);
    }

    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        Application.AddMessageFilter(this);

        VerticalScroll.LargeChange = 60;
        VerticalScroll.SmallChange = 20;
        HorizontalScroll.LargeChange = 60;
        HorizontalScroll.SmallChange = 20;
    }

    protected override void OnHandleDestroyed(EventArgs e) 
    {
        Application.RemoveMessageFilter(this);
        base.OnHandleDestroyed(e);
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        switch (m.Msg) {
            case WM_PAINT:
            case WM_ERASEBKGND:
            case WM_NCCALCSIZE:
                if (DesignMode || !AutoScroll) break;
                ShowScrollBar(this.Handle, SB_SHOW_BOTH, false);
                break;
            case WM_MOUSEWHEEL:
                // Handle Mouse Wheel for other specific cases
                int delta = (int)(m.WParam.ToInt64() >> 16);
                int direction = Math.Sign(delta);
                ShowScrollBar(this.Handle, SB_SHOW_BOTH, false); 
                break;
        }
    }

    public bool PreFilterMessage(ref Message m)
    {
        switch (m.Msg) {
            case WM_MOUSEWHEEL:
            case WM_MOUSEHWHEEL:
                if (DesignMode || !AutoScroll) return false;
                if (VerticalScroll.Maximum <= ClientSize.Height) return false;
                // Should also check whether the ForegroundWindow matches the parent Form.
                if (RectangleToScreen(ClientRectangle).Contains(MousePosition)) {
                    SendMessage(this.Handle, WM_MOUSEWHEEL, m.WParam, m.LParam);
                    return true;
                }
                break;
            case WM_LBUTTONDOWN:
                // Pre-handle Left Mouse clicks for all child Controls
                //Console.WriteLine($"WM_LBUTTONDOWN");
                if (RectangleToScreen(ClientRectangle).Contains(MousePosition)) {
                    var mousePos = MousePosition;
                    if (GetForegroundWindow() != TopLevelControl.Handle) return false;
                    // The hosted Control that contains the mouse pointer 
                    var ctrl = FromHandle(ChildWindowFromPoint(this.Handle, PointToClient(mousePos)));
                    // A child Control of the hosted Control that will be clicked 
                    // If no child Controls at that position the Parent's handle
                    var child = FromHandle(WindowFromPoint(mousePos));
                }
                return false;
                // Eventually, if you don't want the message to reach the child Control
                // return true; 
        }
        return false;
    }

    private const int WM_PAINT = 0x000F;
    private const int WM_ERASEBKGND = 0x0014;
    private const int WM_NCCALCSIZE = 0x0083;
    private const int WM_LBUTTONDOWN = 0x0201;
    private const int WM_MOUSEWHEEL = 0x020A;
    private const int WM_MOUSEHWHEEL = 0x020E;
    private const int SB_SHOW_VERT = 0x1;
    private const int SB_SHOW_BOTH = 0x3; 

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);

    [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int SendMessage(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    internal static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    internal static extern IntPtr WindowFromPoint(Point point);

    [DllImport("user32.dll")]
    internal static extern IntPtr ChildWindowFromPoint(IntPtr hWndParent, Point point);
}

这就是它的工作原理:

【讨论】:

  • 效果很好。我只有一条评论。我已将调用 ShowScrollBar 时的 wBar 参数更改为 SB_SHOW_VERT,效果很好!唯一的问题是,当面板“过载”控件时,水平滚动条会暂时出现,然后添加另一个面板,它不再显示。我没有使用 SB_SHOW_BOTH,因为由于某种原因,这会弄乱内部的面板图形并破坏一些组件。有任何解决这个问题的方法吗?除此之外,很好的答案,谢谢!
  • 嗨,在您的编辑中再次调用 ShowScrollBar 正是修复它的方法。太感谢了!很好的解决方案。
猜你喜欢
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 2011-08-14
  • 2012-02-27
  • 1970-01-01
相关资源
最近更新 更多