ScanViwer.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using PDF_Office.CustomControl.ScanViewControl;
  2. using PDF_Office.ViewModels.Scan;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace PDF_Office.Views.Scan
  18. {
  19. /// <summary>
  20. /// ScanViwer.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class ScanViwer : UserControl
  23. {
  24. ScanViwerViewModel scanViwerViewModel;
  25. MenuItem item;
  26. ContextMenu contextMenu;
  27. public ScanViwer()
  28. {
  29. InitializeComponent();
  30. }
  31. double scale = 1;
  32. private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
  33. {
  34. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  35. {
  36. if (e.Delta > 0)
  37. {
  38. scale += 0.1;
  39. if (scanViwerViewModel.IsShowRect)
  40. {
  41. Rect rect = new Rect(drawRect.X * scale, drawRect.Y * scale, drawRect.Width * scale, drawRect.Height * scale);
  42. BitmapPanel.SetRect(rect);
  43. BitmapPanel.RectDraw();
  44. }
  45. ImageGrid.Width = 500 * scale;
  46. ImageGrid.Height = 500 * scale;
  47. }
  48. else
  49. {
  50. if (scale > 0.2)
  51. {
  52. scale -= 0.2;
  53. }
  54. if (scanViwerViewModel.IsShowRect)
  55. {
  56. Rect rect = new Rect(drawRect.X * scale, drawRect.Y * scale, drawRect.Width * scale, drawRect.Height * scale);
  57. BitmapPanel.SetRect(rect);
  58. BitmapPanel.RectDraw();
  59. }
  60. ImageGrid.Width = 500 * scale;
  61. ImageGrid.Height = 500 * scale;
  62. }
  63. }
  64. RedressControl.Visibility = Visibility.Collapsed;
  65. }
  66. private Point MouseDownPos;
  67. bool dowm = false;
  68. bool CreateNewRect = false;
  69. private async void CustomPanel_MouseDown(object sender, MouseButtonEventArgs e)
  70. {
  71. RedressControl.Visibility = Visibility.Visible;
  72. MouseDownPos = e.GetPosition(BitmapPanel);
  73. HitTestResult hitResult = VisualTreeHelper.HitTest(BitmapPanel, MouseDownPos);
  74. if (hitResult != null && hitResult.VisualHit is CustomDraw)
  75. {
  76. CustomDraw hitChild = (CustomDraw)hitResult.VisualHit;
  77. BitmapPanel.DisplayContent(hitChild);
  78. RedressControl.SetValue(Canvas.LeftProperty, hitChild.PaintRect.Left + (hitChild.PaintRect.Width / 2 - RedressControl.ActualWidth / 2));
  79. if (hitChild.PaintRect.Top - RedressControl.ActualHeight > 0)
  80. {
  81. RedressControl.SetValue(Canvas.TopProperty, hitChild.PaintRect.Top - RedressControl.ActualHeight);
  82. }
  83. else
  84. {
  85. RedressControl.SetValue(Canvas.TopProperty, hitChild.PaintRect.Bottom);
  86. }
  87. RedressControl.HitChild = hitChild;
  88. RedressControl.TextBoxContext = hitChild.PaintText;
  89. await Task.Delay(5);
  90. RedressControl.SelectAll();
  91. return;
  92. }
  93. RedressControl.Visibility = Visibility.Collapsed;
  94. }
  95. private void RedressControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  96. {
  97. if (!(bool)e.NewValue)
  98. {
  99. BitmapPanel.DisplayContent(null);
  100. }
  101. }
  102. private void RedressControl_ApplyCommandHandler(object sender, EventArgs e)
  103. {
  104. RedressControl.HitChild.PaintText = RedressControl.TextBoxContext;
  105. RedressControl.HitChild.Draw();
  106. RedressControl.Visibility = Visibility.Collapsed;
  107. }
  108. private void BitmapPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  109. {
  110. if (scanViwerViewModel.IsShowRect)
  111. {
  112. Point clickPoint = e.GetPosition(BitmapPanel);
  113. dowm = true;
  114. BitmapPanel.RectSetMaxRect(BitmapPanel.DrawRect);
  115. if (!BitmapPanel.RectMouseLeftButtonDown(clickPoint) && BitmapPanel.DrawRect.Contains(clickPoint))
  116. {
  117. CreateNewRect = true;
  118. }
  119. }
  120. }
  121. private void BitmapPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  122. {
  123. dowm = false;
  124. CreateNewRect = false;
  125. }
  126. private Rect drawRect;
  127. private void BitmapPanel_MouseMove(object sender, MouseEventArgs e)
  128. {
  129. if (scanViwerViewModel.IsShowRect)
  130. {
  131. Point currentPos = e.GetPosition(BitmapPanel);
  132. if (dowm && BitmapPanel.DrawRect.Contains(currentPos))
  133. {
  134. if (CreateNewRect)
  135. {
  136. drawRect = new Rect(MouseDownPos, currentPos);
  137. BitmapPanel.SetRect(drawRect);
  138. BitmapPanel.RectDraw();
  139. }
  140. else
  141. {
  142. BitmapPanel.RectMouseMove(currentPos);
  143. drawRect = BitmapPanel.GetClientRect();
  144. }
  145. drawRect = new Rect(drawRect.X / scale, drawRect.Y / scale, drawRect.Width / scale, drawRect.Height / scale);
  146. }
  147. }
  148. }
  149. private void BitmapPanel_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  150. {
  151. if (scanViwerViewModel.IsShowRect)
  152. {
  153. Point clickPoint = e.GetPosition(BitmapPanel);
  154. if (BitmapPanel.RectMouseLeftButtonDown(clickPoint))
  155. {
  156. BitmapPanel.ContextMenu = contextMenu;
  157. }
  158. else
  159. {
  160. BitmapPanel.ContextMenu = null;
  161. }
  162. e.Handled = true;
  163. }
  164. }
  165. private void Item_Click(object sender, RoutedEventArgs e)
  166. {
  167. scanViwerViewModel.AreaProcess(BitmapPanel.GetClientRect(), BitmapPanel.DrawRect);
  168. }
  169. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  170. {
  171. scanViwerViewModel = DataContext as ScanViwerViewModel;
  172. contextMenu = new ContextMenu();
  173. item = new MenuItem() { Header = "开始识别" };
  174. item.Click += Item_Click;
  175. contextMenu.Items.Add(item);
  176. }
  177. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  178. {
  179. item.Click -= Item_Click;
  180. }
  181. private void UserControl_KeyDown(object sender, KeyEventArgs e)
  182. {
  183. if (e.Key == Key.Escape)
  184. {
  185. BitmapPanel.SetRect(new Rect(0,0,0,0));
  186. BitmapPanel.RectDraw();
  187. e.Handled = true;
  188. }
  189. }
  190. }
  191. }