所以,经过一些实验,我找到了答案,并将其发布给任何需要它的人。
private void flowLayout_MouseWheel(object sender, MouseEventArgs e)
{
var currentPosition = this.flowLayoutPanel1.AutoScrollPosition.Y;
if((currentPosition == this.flowLayoutPanel1.VerticalScroll.Minimum && e.Delta>0) || (currentPosition == -1*(this.flowLayoutPanel1.VerticalScroll.Maximum + 1 - this.flowLayoutPanel1.VerticalScroll.LargeChange) && e.Delta<0))
{
Control control = this.Parent;
FlowLayoutPanel flp = control as FlowLayoutPanel;
if(flp == null) { return; }
flp.VerticalScroll.Value = Math.Min(Math.Max(flp.VerticalScroll.Value - e.Delta, 0), (flp.VerticalScroll.Maximum + 1 - flp.VerticalScroll.LargeChange));
}
}
此代码首先获取 FlowLayoutPanel 的当前位置。
var currentPosition = this.flowLayoutPanel1.AutoScrollPosition.Y;
如果 e.Delta 为负数,则用户正在向下滚动。向上是积极的。
if(
(currentPosition == this.flowLayoutPanel1.VerticalScroll.Minimum
&& e.Delta>0)
|| (currentPosition == -1*(this.flowLayoutPanel1.VerticalScroll.Maximum + 1 - this.flowLayoutPanel1.VerticalScroll.LargeChange)
&& e.Delta<0)
)
如果 Panel 滚动到顶部,并且用户向上滚动,它会获取表单的 Parent,即主面板。如果无法获取面板,则退出。
Control control = this.Parent;
FlowLayoutPanel flp = control as FlowLayoutPanel;
if(flp == null) { return; }
然后它将主面板调整相同的增量,使用 Max 和 Min 对其进行约束以避免在尝试将值设置为小于最小值或大于最大值时导致的 OutOfBoundsException。
flp.VerticalScroll.Value = Math.Min(
Math.Max(flp.VerticalScroll.Value - e.Delta, 0),
(flp.VerticalScroll.Maximum + 1 - flp.VerticalScroll.LargeChange)
);