improve:窗口置顶

This commit is contained in:
2025-10-18 20:45:23 +08:00
parent 729b10cce8
commit db76a11347
+104 -30
View File
@@ -6,9 +6,12 @@ using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Shapes;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using System.Windows.Threading;
namespace Ink_Canvas
{
@@ -46,6 +49,9 @@ namespace Ink_Canvas
hideTimer = new Timer(1000); // 每秒检查一次
hideTimer.Elapsed += HideTimer_Elapsed;
lastActivityTime = DateTime.Now;
// 添加窗口加载事件处理,确保置顶
Loaded += TimerWindow_Loaded;
}
@@ -331,6 +337,9 @@ namespace Ink_Canvas
minimizedWindow = new MinimizedTimerWindow(this);
minimizedWindow.Show();
// 确保最小化窗口也置顶
minimizedWindow.Topmost = true;
// 隐藏主窗口
this.Hide();
}
@@ -777,6 +786,9 @@ namespace Ink_Canvas
// 启动隐藏定时器
hideTimer.Start();
// 确保计时器窗口置顶
ApplyTimerWindowTopmost();
// 保存到最近计时记录
SaveRecentTimer();
}
@@ -879,36 +891,6 @@ namespace Ink_Canvas
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
isTimerRunning = false;
if (MainWindow.Settings != null)
{
var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow != null)
{
try
{
var currentModeField = mainWindow.GetType().GetField("currentMode",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (currentModeField != null)
{
int currentMode = (int)currentModeField.GetValue(mainWindow);
if (currentMode == 1) // 白板模式
{
mainWindow.Topmost = false; // 保持白板模式下的非置顶状态
}
else
{
mainWindow.Topmost = true; // 其他模式恢复置顶
}
}
}
catch
{
// 如果反射失败,使用默认行为
mainWindow.Topmost = true;
}
}
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
@@ -1329,6 +1311,9 @@ namespace Ink_Canvas
fullscreenWindow = new FullscreenTimerWindow(this);
fullscreenWindow.Show();
// 确保全屏窗口也置顶
fullscreenWindow.Topmost = true;
// 隐藏主窗口
this.Hide();
}
@@ -1342,6 +1327,8 @@ namespace Ink_Canvas
this.Show();
this.Activate();
this.WindowState = WindowState.Normal;
// 重新应用置顶
ApplyTimerWindowTopmost();
}
else if (fullscreenWindow != null)
{
@@ -1351,7 +1338,94 @@ namespace Ink_Canvas
this.Show();
this.Activate();
this.WindowState = WindowState.Normal;
// 重新应用置顶
ApplyTimerWindowTopmost();
}
}
#region Win32 API
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("kernel32.dll")]
private static extern uint GetCurrentProcessId();
private const int GWL_EXSTYLE = -20;
private const int WS_EX_TOPMOST = 0x00000008;
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOACTIVATE = 0x0010;
private const uint SWP_SHOWWINDOW = 0x0040;
private const uint SWP_NOOWNERZORDER = 0x0200;
/// <summary>
/// 应用计时器窗口置顶
/// </summary>
private void ApplyTimerWindowTopmost()
{
try
{
var hwnd = new WindowInteropHelper(this).Handle;
if (hwnd == IntPtr.Zero) return;
// 强制激活窗口
Activate();
Focus();
// 设置WPF的Topmost属性
Topmost = true;
// 使用Win32 API强制置顶
// 1. 设置窗口样式为置顶
int exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOPMOST);
// 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)
{
LogHelper.WriteLogToFile($"应用计时器窗口置顶失败: {ex.Message}", LogHelper.LogType.Error);
}
}
/// <summary>
/// 窗口加载事件处理,确保置顶
/// </summary>
private void TimerWindow_Loaded(object sender, RoutedEventArgs e)
{
// 使用延迟确保窗口完全加载后再应用置顶
Dispatcher.BeginInvoke(new Action(() =>
{
ApplyTimerWindowTopmost();
}), DispatcherPriority.Loaded);
}
#endregion
}
}