diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index 23b5565d..a6979eb6 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -28,44 +28,7 @@ namespace Ink_Canvas { private System.Windows.Controls.Canvas currentCanvas = null; private AutoUpdateHelper.UpdateLineGroup AvailableLatestLineGroup = null; - // Win32 API声明和常量(用于无焦点窗口) - [DllImport("user32.dll")] - private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); - [DllImport("user32.dll")] - private static extern int GetWindowLong(IntPtr hWnd, int nIndex); - private const int GWL_EXSTYLE = -20; - private const int WS_EX_NOACTIVATE = 0x08000000; - [DllImport("user32.dll")] - private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); - private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); - private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); - private const uint SWP_NOSIZE = 0x0001; - private const uint SWP_NOMOVE = 0x0002; - private const uint SWP_NOACTIVATE = 0x0010; - private const uint SWP_SHOWWINDOW = 0x0040; - - // 新增:设置窗口置顶并兼容无焦点 - private void SetTopmostWithNoActivate(bool topmost) - { - var hwnd = new WindowInteropHelper(this).Handle; - int exStyle = GetWindowLong(hwnd, GWL_EXSTYLE); - // 先移除 WS_EX_NOACTIVATE - SetWindowLong(hwnd, GWL_EXSTYLE, exStyle & ~WS_EX_NOACTIVATE); - // 设置 Topmost - this.Topmost = topmost; - // 使用SetWindowPos确保无焦点置顶 - if (topmost) - { - SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW); - } - else - { - SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW); - } - // 再加回 WS_EX_NOACTIVATE - exStyle = GetWindowLong(hwnd, GWL_EXSTYLE); - SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_NOACTIVATE); - } + #region Window Initialization @@ -206,12 +169,7 @@ namespace Ink_Canvas { }; } - protected override void OnSourceInitialized(EventArgs e) - { - base.OnSourceInitialized(e); - // 设置窗口为无焦点且置顶(不会抢占焦点且始终在最前) - SetTopmostWithNoActivate(true); - } + #endregion