我正在尝试制作一个可滚动的面板,但没有滚动条,并通过鼠标垂直拖动滚动...这是某人迄今为止帮助我做到的...:
private void panel1_MouseEnter(object sender, EventArgs e)
{
panel1.AutoScroll = false;
}
private int ValidateChange(int change)
{
var padding = -1;
if (change < 0)
{
var max = (from Control control in Controls select control.Top + control.Height + padding).Concat(new[] { int.MinValue }).Max();
if (max < Height + Math.Abs(change))
{
return Height - max;
}
}
else
{
var min = (from Control control in Controls select control.Top).Concat(new[] { int.MaxValue }).Min();
if (min > padding - Math.Abs(change))
{
return padding - min;
}
}
return change;
}
private void HandleDelta(int delta)
{
var change = ValidateChange(delta);
foreach (Control control in Controls)
{
control.Top += change;
}
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
HandleDelta(e.Delta);
base.OnMouseWheel(e);
}
private Point _mouseLastPosition;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mouseLastPosition = e.Location;
}
base.OnMouseDown(e);
}
public void panel1_MouseMove(object sender, MouseEventArgs e)
{
if ((MouseButtons & MouseButtons.Left) != 0)
{
var delta = e.Y - _mouseLastPosition.Y;
HandleDelta(delta);
_mouseLastPosition = e.Location;
}
base.OnMouseMove(e);
}
但是它太快了...
编辑:用鼠标拖动看起来有点奇怪,我不知道为什么,当我尝试使用鼠标滚轮时,它会出现错误:var max = (from Control control in Controls select control.Top + control.Height + padding).Concat(new[] { int.MinValue }).Max();
,报这个错:未处理的类型'System.StackOverflowException'发生在System.Core.dll中。