Skip to content

Commit

Permalink
perf: 优化默认位置实现
Browse files Browse the repository at this point in the history
perf: 移除系统窗口菜单,避免重复调用快捷键后首字母无法输入

chore: before hide
  • Loading branch information
ZGGSONG committed Jan 11, 2024
1 parent 52ef164 commit 66dca95
Showing 1 changed file with 59 additions and 12 deletions.
71 changes: 59 additions & 12 deletions STranslate/Views/MainView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -14,8 +15,6 @@ namespace STranslate.Views
/// </summary>
public partial class MainView : Window
{
private readonly MainViewModel vm = Singleton<MainViewModel>.Instance;

public MainView()
{
DataContext = vm;
Expand All @@ -32,7 +31,7 @@ private void UnLoadPosition()
//写入配置
if (!Singleton<ConfigHelper>.Instance.WriteConfig(Left, Top))
{
LogService.Logger.Debug($"保存位置({Left},{Top})失败...");
LogService.Logger.Error($"保存位置({Left},{Top})失败...");
}
}

Expand All @@ -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}");
}
}

Expand All @@ -85,16 +86,62 @@ private void Mwin_Deactivated(object sender, EventArgs e)
/// <param name="e"></param>
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<MainViewModel>.Instance;
}
}
}

0 comments on commit 66dca95

Please sign in to comment.