From 12eeb79e9fed5fd2c530dc00c02d4508e794721c Mon Sep 17 00:00:00 2001 From: CJK_mkp <113243675+CJKmkp@users.noreply.github.com> Date: Thu, 30 Oct 2025 12:02:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E6=BB=9A=E4=B8=80=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Windows/QuickDrawFloatingButton.cs | 83 +------------------ 1 file changed, 3 insertions(+), 80 deletions(-) diff --git a/Ink Canvas/Windows/QuickDrawFloatingButton.cs b/Ink Canvas/Windows/QuickDrawFloatingButton.cs index 9b3ab5f6..50d96931 100644 --- a/Ink Canvas/Windows/QuickDrawFloatingButton.cs +++ b/Ink Canvas/Windows/QuickDrawFloatingButton.cs @@ -14,9 +14,6 @@ namespace Ink_Canvas /// public partial class QuickDrawFloatingButton : Window { - // 定期维护置顶的定时器 - private DispatcherTimer _topmostTimer; - public QuickDrawFloatingButton() { InitializeComponent(); @@ -24,10 +21,6 @@ namespace Ink_Canvas // 设置无焦点状态 this.Focusable = false; this.ShowInTaskbar = false; - - // 停止定时器并清理 - this.Closed += QuickDrawFloatingButton_Closed; - this.Unloaded += QuickDrawFloatingButton_Unloaded; } @@ -36,10 +29,7 @@ namespace Ink_Canvas // 设置位置到屏幕右下角稍微靠近中部 SetPositionToBottomRight(); - // 启动定时维护置顶 - StartTopmostTimer(); - - // 保留立即应用置顶以避免短时间内失效 + // 应用置顶 ApplyFloatingButtonTopmost(); } @@ -75,76 +65,7 @@ namespace Ink_Canvas } } - private void QuickDrawFloatingButton_Unloaded(object sender, RoutedEventArgs e) - { - StopTopmostTimer(); - } - private void QuickDrawFloatingButton_Closed(object sender, EventArgs e) - { - StopTopmostTimer(); - } - - private void StartTopmostTimer() - { - try - { - if (_topmostTimer != null) return; - - _topmostTimer = new DispatcherTimer(DispatcherPriority.Normal) - { - Interval = TimeSpan.FromSeconds(1.5) - }; - _topmostTimer.Tick += TopmostTimer_Tick; - _topmostTimer.Start(); - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"启动置顶维护定时器失败: {ex.Message}", LogHelper.LogType.Error); - } - } - - private void StopTopmostTimer() - { - try - { - if (_topmostTimer == null) return; - _topmostTimer.Stop(); - _topmostTimer.Tick -= TopmostTimer_Tick; - _topmostTimer = null; - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"停止置顶维护定时器失败: {ex.Message}", LogHelper.LogType.Error); - } - } - - private void TopmostTimer_Tick(object sender, EventArgs e) - { - try - { - var main = Application.Current?.MainWindow as Window; - if (main == null) return; - - // 保持与主窗口一致的 Topmost 状态 - if (this.Topmost != main.Topmost) - { - this.Topmost = main.Topmost; - - // 若需要强制置顶,尝试使用 Win32 - var hwnd = new WindowInteropHelper(this).Handle; - if (hwnd != IntPtr.Zero && this.Topmost) - { - SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, - SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOOWNERZORDER); - } - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"置顶维护Tick失败: {ex.Message}", LogHelper.LogType.Error); - } - } #region Win32 API 声明和置顶管理 @@ -191,6 +112,8 @@ namespace Ink_Canvas // 2. 使用SetWindowPos确保窗口在最顶层 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOOWNERZORDER); + + LogHelper.WriteLogToFile("快抽悬浮按钮已应用置顶", LogHelper.LogType.Trace); } catch (Exception ex) {