DiagramScrollView.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Windows.Controls;
  3. using System.Windows.Input;
  4. using System.Windows.Threading;
  5. namespace SHJX.Service.WorkFlowEdit
  6. {
  7. public class DiagramScrollView : ScrollViewer
  8. {
  9. double _dx = 0;
  10. double _dy = 0;
  11. private DispatcherTimer _timer;
  12. public double Sensitivity { get; set; }
  13. public double ScrollStep { get; set; }
  14. public double Delay
  15. {
  16. get { return _timer.Interval.TotalMilliseconds; }
  17. set { _timer.Interval = TimeSpan.FromMilliseconds(value); }
  18. }
  19. public DiagramScrollView()
  20. {
  21. _timer = new DispatcherTimer();
  22. _timer.Tick += new EventHandler(Tick);
  23. HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  24. VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  25. Focusable = false;
  26. Sensitivity = 20;
  27. ScrollStep = 16;
  28. Delay = 50;
  29. }
  30. private void Tick(object sender, EventArgs e)
  31. {
  32. if (!(Content is DiagramView) || !((DiagramView)Content).IsDragging)
  33. return;
  34. if (_dx != 0)
  35. this.ScrollToHorizontalOffset(this.HorizontalOffset + _dx);
  36. if (_dy != 0)
  37. this.ScrollToVerticalOffset(this.VerticalOffset + _dy);
  38. }
  39. protected override void OnPreviewMouseMove(MouseEventArgs e)
  40. {
  41. if (!(Content is DiagramView) || !((DiagramView)Content).IsDragging)
  42. {
  43. _timer.IsEnabled = false;
  44. }
  45. else
  46. {
  47. _timer.IsEnabled = true;
  48. var point = e.GetPosition(this);
  49. _dx = _dy = 0;
  50. if (point.X < Sensitivity)
  51. _dx = -ScrollStep;
  52. else if (point.X > this.ActualWidth - Sensitivity)
  53. _dx = +ScrollStep;
  54. if (point.Y < Sensitivity)
  55. _dy = -ScrollStep;
  56. else if (point.Y > this.ActualHeight - Sensitivity)
  57. _dy = +ScrollStep;
  58. }
  59. base.OnPreviewMouseMove(e);
  60. }
  61. }
  62. }