improve:窗口置顶
This commit is contained in:
@@ -5,11 +5,14 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Threading;
|
||||
using MessageBox = iNKORE.UI.WPF.Modern.Controls.MessageBox;
|
||||
|
||||
namespace Ink_Canvas
|
||||
@@ -35,6 +38,9 @@ namespace Ink_Canvas
|
||||
|
||||
// 添加窗口关闭事件处理
|
||||
Closed += RandWindow_Closed;
|
||||
|
||||
// 添加窗口显示事件处理,确保置顶
|
||||
Loaded += RandWindow_Loaded;
|
||||
}
|
||||
|
||||
private void LoadBackground(Settings settings)
|
||||
@@ -86,6 +92,9 @@ namespace Ink_Canvas
|
||||
|
||||
// 添加窗口关闭事件处理
|
||||
Closed += RandWindow_Closed;
|
||||
|
||||
// 添加窗口显示事件处理,确保置顶
|
||||
Loaded += RandWindow_Loaded;
|
||||
|
||||
new Thread(() =>
|
||||
{
|
||||
@@ -349,6 +358,51 @@ namespace Ink_Canvas
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 窗口加载事件处理
|
||||
/// </summary>
|
||||
private void RandWindow_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// 使用延迟确保窗口完全加载后再应用置顶
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// 强制激活窗口
|
||||
Activate();
|
||||
Focus();
|
||||
|
||||
// 设置置顶
|
||||
Topmost = true;
|
||||
|
||||
// 使用Win32 API强制置顶
|
||||
var hwnd = new WindowInteropHelper(this).Handle;
|
||||
if (hwnd != IntPtr.Zero)
|
||||
{
|
||||
const int WS_EX_TOPMOST = 0x00000008;
|
||||
const int GWL_EXSTYLE = -20;
|
||||
const int SWP_NOMOVE = 0x0002;
|
||||
const int SWP_NOSIZE = 0x0001;
|
||||
const int SWP_SHOWWINDOW = 0x0040;
|
||||
const int SWP_NOOWNERZORDER = 0x0200;
|
||||
var HWND_TOPMOST = new IntPtr(-1);
|
||||
|
||||
// 设置窗口样式为置顶
|
||||
int exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
|
||||
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOPMOST);
|
||||
|
||||
// 强制置顶
|
||||
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
|
||||
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOOWNERZORDER);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"RandWindow置顶失败: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
}), DispatcherPriority.Loaded);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 窗口关闭事件处理
|
||||
/// </summary>
|
||||
@@ -357,5 +411,16 @@ namespace Ink_Canvas
|
||||
// 窗口关闭时的清理工作
|
||||
// 这里可以添加必要的清理代码
|
||||
}
|
||||
|
||||
#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);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user