Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过鼠标拖动或使用鼠标滚轮水平滚动面板_C#_.net_Winforms - Fatal编程技术网

C# 通过鼠标拖动或使用鼠标滚轮水平滚动面板

C# 通过鼠标拖动或使用鼠标滚轮水平滚动面板,c#,.net,winforms,C#,.net,Winforms,我做了一个垂直滚动面板,我想让它水平滚动。我的代码通过鼠标拖动或使用鼠标滚轮使面板可以滚动 这是我的代码: private Point _mouseLastPosition; protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { _mouseLastPosition = e.Location;

我做了一个垂直滚动面板,我想让它水平滚动。我的代码通过鼠标拖动或使用鼠标滚轮使面板可以滚动

这是我的代码:

private Point _mouseLastPosition;

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _mouseLastPosition = e.Location;
        }
        base.OnMouseDown(e);
    }

    private int ValidateChange(int change)
    {
        var padding = -1;
        if (change < 0)
        {
            var max = (from Control control in Controls select control.Left + control.Width + padding).Concat(new[] { int.MinValue }).Max();

            if (max < Width + Math.Abs(change))
            {
                return Width - max;
            }
        }
        else
        {
            var min = (from Control control in Controls select control.Left).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.Left += change;
        }

    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if ((MouseButtons & MouseButtons.Left) != 0)
        {
            var delta = _mouseLastPosition.X - e.X;
            HandleDelta(delta);
            _mouseLastPosition = e.Location;
        }
        base.OnMouseMove(e);
    }

    protected override void OnMouseWheel(MouseEventArgs e)
    {
        HandleDelta(e.Delta);
        base.OnMouseWheel(e);
    }
private Point\u mouseLastPosition;
MouseEventArgs e上的受保护覆盖无效(MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
_鼠标位置=e.位置;
}
base.OnMouseDown(e);
}
私有内部验证更改(内部更改)
{
变量填充=-1;
如果(更改<0)
{
var max=(从控件中的控件选择Control.Left+Control.Width+padding).Concat(new[]{int.MinValue}).max();
如果(最大<宽度+数学绝对值(变化))
{
返回宽度-最大值;
}
}
其他的
{
var min=(从控件中的控件选择Control.Left).Concat(new[]{int.MaxValue}).min();
if(min>padding-Math.Abs(更改))
{
返回填充-分钟;
}
}
回报变化;
}
私人空间HandleDelta(内部三角洲)
{
var变化=ValidateChange(增量);
foreach(控件中的控件)
{
控制。左+=改变;
}
}
MouseMove上的受保护覆盖无效(MouseEventArgs e)
{
如果((鼠标按钮和鼠标按钮.左)!=0)
{
var delta=_mouseLastPosition.X-e.X;
HandleDelta(三角洲);
_鼠标位置=e.位置;
}
基地移动(e);
}
MouseWheel上的受保护覆盖无效(MouseEventArgs e)
{
HandleDelta(东三角洲);
底轮(e);
}
我需要改变什么才能让它按照我想要的方式工作

更新:我按照你的要求更改了代码,但它没有按照我希望的方式工作。这就是它现在的样子(我想让它向右滚动)


更改以下内容

控件。顶部->> 控制,左边

控件。高度->> 控制宽度

高度->> 宽度

var delta=e.Y-_mouseLastPosition.Y;->> var delta=_mouseLastPosition.X-e.X

更新

这不是最好的解决方案,但你能试试吗

保持原来的左边

    private Dictionary<string, int> dicControls;

    private bool isOverFlow;

    protected override void InitLayout()
    {
        base.InitLayout();

        if (dicControls == null)
        {
            dicControls = new Dictionary<string, int>();

            foreach (Control control in Controls)
            {
                dicControls.Add(control.Name, control.Left);
            }

            var max = (from Control control in Controls select control.Left + control.Width + -1).Concat(new[] { int.MinValue }).Max();

            isOverFlow = (Width - max) < 0;
        }
    }

    private void HandleDelta(int delta)
    {
        var change = ValidateChange(delta);

        foreach (Control control in Controls)
        {
            var initalLeft = dicControls[control.Name];

            var tempLeft = control.Left + change;

            if(isOverFlow)
            {
                if (tempLeft > initalLeft || delta > 0)
                {
                    control.Left = initalLeft;
                }
                else
                {
                    control.Left += change;
                }
            }
            else
            {
                if (tempLeft < initalLeft)
                {
                    control.Left = initalLeft;
                }
                else
                {
                    control.Left += change;
                }
            }
        }
    }
专用字典控件;
私有布尔均衡流;
受保护的覆盖void InitLayout()
{
base.InitLayout();
如果(dicControls==null)
{
dicControls=新字典();
foreach(控件中的控件)
{
添加(control.Name,control.Left);
}
var max=(从控件中的控件选择Control.Left+Control.Width+-1).Concat(new[]{int.MinValue}).max();
等溢流=(宽度-最大值)<0;
}
}
私人空间HandleDelta(内部三角洲)
{
var变化=ValidateChange(增量);
foreach(控件中的控件)
{
var initalLeft=dicControls[control.Name];
var tempLeft=control.Left+change;
if(等溢流)
{
if(tempLeft>initalLeft | | delta>0)
{
control.Left=initalLeft;
}
其他的
{
控制。左+=改变;
}
}
其他的
{
if(tempLeft