由于您只需要隐藏 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);
}
这就是它的工作原理: