using PDF_Office.CustomControl.ScanViewControl; using PDF_Office.ViewModels.Scan; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace PDF_Office.Views.Scan { /// /// ScanViwer.xaml 的交互逻辑 /// public partial class ScanViwer : UserControl { ScanViwerViewModel scanViwerViewModel; MenuItem item; ContextMenu contextMenu; public ScanViwer() { InitializeComponent(); } double scale = 1; private void Grid_MouseWheel(object sender, MouseWheelEventArgs e) { if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { if (e.Delta > 0) { scale += 0.1; if (scanViwerViewModel.IsShowRect) { Rect rect = new Rect(drawRect.X * scale, drawRect.Y * scale, drawRect.Width * scale, drawRect.Height * scale); BitmapPanel.SetRect(rect); BitmapPanel.RectDraw(); } ImageGrid.Width = 500 * scale; ImageGrid.Height = 500 * scale; } else { if (scale > 0.2) { scale -= 0.2; } if (scanViwerViewModel.IsShowRect) { Rect rect = new Rect(drawRect.X * scale, drawRect.Y * scale, drawRect.Width * scale, drawRect.Height * scale); BitmapPanel.SetRect(rect); BitmapPanel.RectDraw(); } ImageGrid.Width = 500 * scale; ImageGrid.Height = 500 * scale; } } RedressControl.Visibility = Visibility.Collapsed; } private Point MouseDownPos; bool dowm = false; bool CreateNewRect = false; private async void CustomPanel_MouseDown(object sender, MouseButtonEventArgs e) { RedressControl.Visibility = Visibility.Visible; MouseDownPos = e.GetPosition(BitmapPanel); HitTestResult hitResult = VisualTreeHelper.HitTest(BitmapPanel, MouseDownPos); if (hitResult != null && hitResult.VisualHit is CustomDraw) { CustomDraw hitChild = (CustomDraw)hitResult.VisualHit; BitmapPanel.DisplayContent(hitChild); RedressControl.SetValue(Canvas.LeftProperty, hitChild.PaintRect.Left + (hitChild.PaintRect.Width / 2 - RedressControl.ActualWidth / 2)); if (hitChild.PaintRect.Top - RedressControl.ActualHeight > 0) { RedressControl.SetValue(Canvas.TopProperty, hitChild.PaintRect.Top - RedressControl.ActualHeight); } else { RedressControl.SetValue(Canvas.TopProperty, hitChild.PaintRect.Bottom); } RedressControl.HitChild = hitChild; RedressControl.TextBoxContext = hitChild.PaintText; await Task.Delay(5); RedressControl.SelectAll(); return; } RedressControl.Visibility = Visibility.Collapsed; } private void RedressControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (!(bool)e.NewValue) { BitmapPanel.DisplayContent(null); } } private void RedressControl_ApplyCommandHandler(object sender, EventArgs e) { RedressControl.HitChild.PaintText = RedressControl.TextBoxContext; RedressControl.HitChild.Draw(); RedressControl.Visibility = Visibility.Collapsed; } private void BitmapPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (scanViwerViewModel.IsShowRect) { Point clickPoint = e.GetPosition(BitmapPanel); dowm = true; BitmapPanel.RectSetMaxRect(BitmapPanel.DrawRect); if (!BitmapPanel.RectMouseLeftButtonDown(clickPoint) && BitmapPanel.DrawRect.Contains(clickPoint)) { CreateNewRect = true; } } } private void BitmapPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { dowm = false; CreateNewRect = false; } private Rect drawRect; private void BitmapPanel_MouseMove(object sender, MouseEventArgs e) { if (scanViwerViewModel.IsShowRect) { Point currentPos = e.GetPosition(BitmapPanel); if (dowm && BitmapPanel.DrawRect.Contains(currentPos)) { if (CreateNewRect) { drawRect = new Rect(MouseDownPos, currentPos); BitmapPanel.SetRect(drawRect); BitmapPanel.RectDraw(); } else { BitmapPanel.RectMouseMove(currentPos); drawRect = BitmapPanel.GetClientRect(); } drawRect = new Rect(drawRect.X / scale, drawRect.Y / scale, drawRect.Width / scale, drawRect.Height / scale); } } } private void BitmapPanel_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { if (scanViwerViewModel.IsShowRect) { Point clickPoint = e.GetPosition(BitmapPanel); if (BitmapPanel.RectMouseLeftButtonDown(clickPoint)) { BitmapPanel.ContextMenu = contextMenu; } else { BitmapPanel.ContextMenu = null; } e.Handled = true; } } private void Item_Click(object sender, RoutedEventArgs e) { scanViwerViewModel.AreaProcess(BitmapPanel.GetClientRect(), BitmapPanel.DrawRect); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { scanViwerViewModel = DataContext as ScanViwerViewModel; contextMenu = new ContextMenu(); item = new MenuItem() { Header = "开始识别" }; item.Click += Item_Click; contextMenu.Items.Add(item); } private void UserControl_Unloaded(object sender, RoutedEventArgs e) { item.Click -= Item_Click; } private void UserControl_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape) { BitmapPanel.SetRect(new Rect(0,0,0,0)); BitmapPanel.RectDraw(); e.Handled = true; } } } }