ScanViwer.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using PDF_Office.CustomControl.ScanViewControl;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace PDF_Office.Views.Scan
  17. {
  18. /// <summary>
  19. /// ScanViwer.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class ScanViwer : UserControl
  22. {
  23. public ScanViwer()
  24. {
  25. InitializeComponent();
  26. }
  27. double scale = 1;
  28. private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
  29. {
  30. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  31. {
  32. if (e.Delta > 0)
  33. {
  34. scale += 0.1;
  35. ImageGrid.Width = 500 * scale;
  36. ImageGrid.Height = 500 * scale;
  37. }
  38. else
  39. {
  40. if (scale > 0.2)
  41. {
  42. scale -= 0.2;
  43. }
  44. ImageGrid.Width = 500 * scale;
  45. ImageGrid.Height = 500 * scale;
  46. }
  47. }
  48. RedressControl.Visibility = Visibility.Collapsed;
  49. }
  50. private Point MouseDownPos;
  51. bool dowm = false;
  52. bool CreateNewRect = false;
  53. private async void CustomPanel_MouseDown(object sender, MouseButtonEventArgs e)
  54. {
  55. RedressControl.Visibility = Visibility.Visible;
  56. MouseDownPos = e.GetPosition(BitmapPanel);
  57. HitTestResult hitResult = VisualTreeHelper.HitTest(BitmapPanel, MouseDownPos);
  58. if (hitResult != null && hitResult.VisualHit is CustomDraw)
  59. {
  60. CustomDraw hitChild = (CustomDraw)hitResult.VisualHit;
  61. BitmapPanel.DisplayContent(hitChild);
  62. RedressControl.SetValue(Canvas.LeftProperty, hitChild.PaintRect.Left + (hitChild.PaintRect.Width / 2 - RedressControl.ActualWidth / 2));
  63. if (hitChild.PaintRect.Top - RedressControl.ActualHeight > 0)
  64. {
  65. RedressControl.SetValue(Canvas.TopProperty, hitChild.PaintRect.Top - RedressControl.ActualHeight);
  66. }
  67. else
  68. {
  69. RedressControl.SetValue(Canvas.TopProperty, hitChild.PaintRect.Bottom);
  70. }
  71. RedressControl.HitChild = hitChild;
  72. RedressControl.TextBoxContext = hitChild.PaintText;
  73. await Task.Delay(5);
  74. RedressControl.SelectAll();
  75. return;
  76. }
  77. RedressControl.Visibility = Visibility.Collapsed;
  78. }
  79. private void RedressControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  80. {
  81. if (!(bool)e.NewValue)
  82. {
  83. BitmapPanel.DisplayContent(null);
  84. }
  85. }
  86. private void RedressControl_ApplyCommandHandler(object sender, EventArgs e)
  87. {
  88. RedressControl.HitChild.PaintText = RedressControl.TextBoxContext;
  89. RedressControl.HitChild.Draw();
  90. RedressControl.Visibility = Visibility.Collapsed;
  91. }
  92. private void BitmapPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  93. {
  94. Point clickPoint = e.GetPosition(BitmapPanel);
  95. dowm = true;
  96. if (!BitmapPanel.RectMouseLeftButtonDown(clickPoint) && BitmapPanel.DrawRect.Contains(clickPoint))
  97. {
  98. BitmapPanel.RectSetMaxRect(BitmapPanel.DrawRect);
  99. CreateNewRect = true;
  100. }
  101. }
  102. private void BitmapPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  103. {
  104. dowm = false;
  105. CreateNewRect = false;
  106. }
  107. private void BitmapPanel_MouseMove(object sender, MouseEventArgs e)
  108. {
  109. Point currentPos = e.GetPosition(BitmapPanel);
  110. if (dowm && BitmapPanel.DrawRect.Contains(currentPos))
  111. {
  112. if (CreateNewRect)
  113. {
  114. Rect drawRect = new Rect(MouseDownPos, currentPos);
  115. BitmapPanel.SetRect(drawRect);
  116. BitmapPanel.RectDraw();
  117. }
  118. else
  119. {
  120. BitmapPanel.RectMouseMove(currentPos);
  121. }
  122. }
  123. }
  124. }
  125. }