From 66dca9565906d64090afe31a5b3c2bc8df1d5174 Mon Sep 17 00:00:00 2001 From: zggsong Date: Thu, 11 Jan 2024 20:50:43 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit perf: 移除系统窗口菜单,避免重复调用快捷键后首字母无法输入 chore: before hide --- STranslate/Views/MainView.xaml.cs | 71 +++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/STranslate/Views/MainView.xaml.cs b/STranslate/Views/MainView.xaml.cs index e30d9f81..fcefc943 100644 --- a/STranslate/Views/MainView.xaml.cs +++ b/STranslate/Views/MainView.xaml.cs @@ -2,10 +2,11 @@ using STranslate.Util; using STranslate.ViewModels; using System; -using System.Linq; +using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Input; +using System.Windows.Interop; namespace STranslate.Views { @@ -14,8 +15,6 @@ namespace STranslate.Views /// public partial class MainView : Window { - private readonly MainViewModel vm = Singleton.Instance; - public MainView() { DataContext = vm; @@ -32,7 +31,7 @@ private void UnLoadPosition() //写入配置 if (!Singleton.Instance.WriteConfig(Left, Top)) { - LogService.Logger.Debug($"保存位置({Left},{Top})失败..."); + LogService.Logger.Error($"保存位置({Left},{Top})失败..."); } } @@ -50,17 +49,19 @@ private void LoadPosition() bool ret = true; ret &= double.TryParse(args[0], out var left); ret &= double.TryParse(args[1], out var top); - if (!ret) throw new Exception(); + if (!ret || left > SystemParameters.WorkArea.Width || top > SystemParameters.WorkArea.Height) + { + throw new Exception($"当前({SystemParameters.WorkArea.Width}x{SystemParameters.WorkArea.Height})"); + } Left = left; Top = top; } - catch (Exception) + catch (Exception ex) { - Top = (SystemParameters.WorkArea.Height - Height) / 2; - Left = (SystemParameters.WorkArea.Width - Width) / 2; + WindowStartupLocation = WindowStartupLocation.CenterOwner; - LogService.Logger.Warn($"加载上次窗口位置({position})失败,启用默认位置"); + LogService.Logger.Warn($"加载上次窗口位置({position})失败,启用默认位置 {ex.Message}"); } } @@ -85,16 +86,62 @@ private void Mwin_Deactivated(object sender, EventArgs e) /// private void Mwin_Activated(object sender, EventArgs e) { - if (InputView.FindName("InputTB") is TextBox tb) + if (InputView.FindName("InputTB") is TextBox tb && Visibility == Visibility.Visible) { // 执行激活控件的操作,例如设置焦点 tb.Focus(); - //光标移动至末尾 + // 光标移动至末尾 tb.CaretIndex = tb.Text?.Length ?? 0; + // 全选 //tb?.SelectAll(); } } + + #region 隐藏系统窗口菜单 + + //方法来自于 Lindexi + //https://blog.lindexi.com/post/WPF-%E9%9A%90%E8%97%8F%E7%B3%BB%E7%BB%9F%E7%AA%97%E5%8F%A3%E8%8F%9C%E5%8D%95.html + + protected override void OnSourceInitialized(EventArgs e) + { + base.OnSourceInitialized(e); + + var windowInteropHelper = new WindowInteropHelper(this); + var hwnd = windowInteropHelper.Handle; + + var windowLong = GetWindowLong(hwnd, GWL_STYLE); + windowLong &= ~WS_SYSMENU; + + SetWindowLongPtr(hwnd, GWL_STYLE, new IntPtr(windowLong)); + } + + public const int WS_SYSMENU = 0x00080000; + + [DllImport("user32.dll", SetLastError = true)] + public static extern int GetWindowLong(IntPtr hWnd, int nIndex); + + public const int GWL_STYLE = -16; + + public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong) + { + if (Environment.Is64BitProcess) + { + return SetWindowLongPtr64(hWnd, nIndex, dwNewLong); + } + + return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32())); + } + + [DllImport("user32.dll", EntryPoint = "SetWindowLong")] + private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong); + + [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")] + private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong); + + #endregion 隐藏系统窗口菜单 + + private readonly MainViewModel vm = Singleton.Instance; } -} +} \ No newline at end of file