From 1ac7c152e2aa8d912f4a11a4935eba4866f598cd Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Tue, 10 Jan 2023 09:54:39 +0800 Subject: [PATCH] feat: add screenshot translate --- STranslate/Util/Util.cs | 2 +- STranslate/View/MainWindow.xaml.cs | 6 +-- STranslate/View/ScreenShotWindow.xaml | 8 ++-- STranslate/ViewModel/MainVM.cs | 53 +++++++++++++++++---------- STranslate/ViewModel/ScreenShotVM.cs | 6 +-- 5 files changed, 45 insertions(+), 30 deletions(-) diff --git a/STranslate/Util/Util.cs b/STranslate/Util/Util.cs index a3efbfc..7b0f417 100644 --- a/STranslate/Util/Util.cs +++ b/STranslate/Util/Util.cs @@ -279,7 +279,7 @@ namespace STranslate.Util Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); brush.ImageSource = wpfBitmap; - //TODO: flush Memory + FlushMemory(); return brush; } /// diff --git a/STranslate/View/MainWindow.xaml.cs b/STranslate/View/MainWindow.xaml.cs index 64a7d1c..9b30028 100644 --- a/STranslate/View/MainWindow.xaml.cs +++ b/STranslate/View/MainWindow.xaml.cs @@ -11,13 +11,13 @@ namespace STranslate.View /// public partial class MainWindow : Window { - private MainVM vm; + private MainVM vm = MainVM.Instance; public MainWindow() { //InitializeComponent(); - DataContext = new MainVM(this); - vm = (MainVM)DataContext; + DataContext = vm; + vm.Mainwin = this; //if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict) //{ diff --git a/STranslate/View/ScreenShotWindow.xaml b/STranslate/View/ScreenShotWindow.xaml index 9f1664e..2ac3e20 100644 --- a/STranslate/View/ScreenShotWindow.xaml +++ b/STranslate/View/ScreenShotWindow.xaml @@ -40,9 +40,9 @@ - - - - + + + + diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 3fde51c..40caf95 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -14,11 +14,8 @@ namespace STranslate.ViewModel { public class MainVM : BaseVM { - - public MainVM(Window win) + public MainVM() { - _Mainwin = (MainWindow)win; - if (!ReadConfig()) { ExitApp(-1); @@ -68,12 +65,12 @@ namespace STranslate.ViewModel { if (IsTopmost) { - (_Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button) + (Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button) .SetResourceReference(System.Windows.Controls.Control.TemplateProperty, _UnTopmostTemplateName); } else { - (_Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button) + (Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button) .SetResourceReference(System.Windows.Controls.Control.TemplateProperty, _TopmostTemplateName); } IsTopmost = !IsTopmost; @@ -82,12 +79,12 @@ namespace STranslate.ViewModel //ESC EscCmd = new RelayCommand((_) => true, (_) => { - _Mainwin.Hide(); + Mainwin.Hide(); //取消置顶 if (IsTopmost) { - (_Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button) + (Mainwin.FindName("TopmostBtn") as System.Windows.Controls.Button) .SetResourceReference(System.Windows.Controls.Control.TemplateProperty, _UnTopmostTemplateName); IsTopmost = !IsTopmost; } @@ -109,7 +106,7 @@ namespace STranslate.ViewModel //移动 MouseLeftDownCmd = new RelayCommand((_) => true, (_) => { - _Mainwin.DragMove(); + Mainwin.DragMove(); }); //失去焦点 DeactivatedCmd = new RelayCommand((_) => true, (_) => @@ -117,7 +114,7 @@ namespace STranslate.ViewModel if (!IsTopmost) { Speech.SpeakAsyncCancelAll(); - _Mainwin.Hide(); + Mainwin.Hide(); } }); //source speak @@ -194,9 +191,9 @@ namespace STranslate.ViewModel /// public void OpenMainWin() { - _Mainwin.Show(); - _Mainwin.Activate(); - (_Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox).Focus(); + Mainwin.Show(); + Mainwin.Activate(); + (Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox).Focus(); } /// /// 输入翻译 @@ -214,7 +211,7 @@ namespace STranslate.ViewModel ClearAll(); var sentence = GetWordsHelper.Get(); OpenMainWin(); - (_Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox) + (Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox) .Text = sentence.Trim(); _ = Translate(); } @@ -223,10 +220,18 @@ namespace STranslate.ViewModel /// public void ScreenShotTranslate() { - //MessageBox.Show("aaa"); - var xx = new ScreenShotWindow(); - xx.Show(); - xx.Activate(); + var screen = new ScreenShotWindow(); + screen.Show(); + screen.Activate(); + } + /// + /// 截屏翻译Ex + /// + public void ScreenShotTranslateEx(string text) + { + InputTranslate(); + (Mainwin.FindName("TextBoxInput") as System.Windows.Controls.TextBox).Text = text; + _ = Translate(); } /// @@ -434,10 +439,20 @@ namespace STranslate.ViewModel /// private bool _IsStartup; public bool IsStartup { get => _IsStartup; set => UpdateProperty(ref _IsStartup, value); } + /// + /// 托盘图标可见 + /// private bool _IsVisibility = true; public bool IsVisibility { get => _IsVisibility; set => UpdateProperty(ref _IsVisibility, value); } - private MainWindow _Mainwin; + /// + /// view传递至viewmodel + /// + public MainWindow Mainwin; + + private static MainVM _Instance; + public static MainVM Instance => _Instance ?? (_Instance = new MainVM()); + public bool IsTopmost { get; set; } private readonly string _TopmostTemplateName = "ButtonTemplateTopmost"; private readonly string _UnTopmostTemplateName = "ButtonTemplateUnTopmost"; diff --git a/STranslate/ViewModel/ScreenShotVM.cs b/STranslate/ViewModel/ScreenShotVM.cs index fe20a40..62caf12 100644 --- a/STranslate/ViewModel/ScreenShotVM.cs +++ b/STranslate/ViewModel/ScreenShotVM.cs @@ -86,9 +86,9 @@ namespace STranslate.ViewModel //TestSaveBmp(bmpOut); - var xx = Util.Util.TesseractGetText(bmpOut).Trim(); - MessageBox.Show(xx); - System.Diagnostics.Debug.Print(xx); + var getText = Util.Util.TesseractGetText(bmpOut).Trim(); + + MainVM.Instance.ScreenShotTranslateEx(getText); }); ClosedCmd = new RelayCommand((_) => true, (_) =>