2017-04-11 155 views
3

我做了一個垂直滾動面板,我想使其水平滾動能夠。我的代碼通過用鼠標或使用鼠標滾輪拖動它可以使面板滾動。水平滾動面板通過鼠標拖動或使用鼠標滾輪

這是我的代碼:

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); 
    } 

我有什麼改變,使其工作,我希望它的工作方式?

更新:我改變了我的代碼,因爲你告訴我,但它不工作,我想要的方式。這是它現在的樣子(我想讓它向右滾動)。

enter image description here

回答

0

更改如下...

control.Top - >> control.Left

control.Height - >> control.Width

高度 - > > Width

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

UPDATE

這不是最好的解決辦法,但你可以試試這個?

保持inital左..

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; 
       } 
      } 
     } 
    } 
+0

檢查更新 –

+0

我更新我的回答 – caner