fix:窗口置顶异常

This commit is contained in:
PrefacedCorg
2026-04-17 13:14:23 +08:00
parent 7003bb8426
commit 231b850f74
5 changed files with 134 additions and 13 deletions
+3
View File
@@ -2896,6 +2896,7 @@ namespace Ink_Canvas
BorderSettings.Visibility = Visibility.Hidden; BorderSettings.Visibility = Visibility.Hidden;
BorderSettingsMask.Visibility = Visibility.Hidden; BorderSettingsMask.Visibility = Visibility.Hidden;
var win = new HistoryRollbackWindow(Settings.Startup.UpdateChannel); var win = new HistoryRollbackWindow(Settings.Startup.UpdateChannel);
win.Owner = this;
win.ShowDialog(); win.ShowDialog();
// 可选:回滚窗口关闭后恢复设置面板显示 // 可选:回滚窗口关闭后恢复设置面板显示
BorderSettings.Visibility = Visibility.Visible; BorderSettings.Visibility = Visibility.Visible;
@@ -3349,6 +3350,7 @@ namespace Ink_Canvas
// 创建快捷键设置窗口 // 创建快捷键设置窗口
var hotkeySettingsWindow = new HotkeySettingsWindow(this, _globalHotkeyManager); var hotkeySettingsWindow = new HotkeySettingsWindow(this, _globalHotkeyManager);
hotkeySettingsWindow.Owner = this;
// 设置窗口关闭事件,用于在快捷键设置窗口关闭后恢复设置面板 // 设置窗口关闭事件,用于在快捷键设置窗口关闭后恢复设置面板
hotkeySettingsWindow.Closed += (s, e) => hotkeySettingsWindow.Closed += (s, e) =>
@@ -4692,6 +4694,7 @@ namespace Ink_Canvas
return; return;
var quickDrawWindow = new QuickDrawWindow(); var quickDrawWindow = new QuickDrawWindow();
quickDrawWindow.Owner = this;
quickDrawWindow.ShowDialog(); quickDrawWindow.ShowDialog();
} }
catch (Exception ex) catch (Exception ex)
@@ -1215,7 +1215,9 @@ namespace Ink_Canvas
if (Settings.RandSettings.UseNewRollCallUI) if (Settings.RandSettings.UseNewRollCallUI)
{ {
// 使用新点名UI - 随机抽模式 // 使用新点名UI - 随机抽模式
new NewStyleRollCallWindow(Settings, false).ShowDialog(); var rollCallWindow = new NewStyleRollCallWindow(Settings, false);
rollCallWindow.Owner = this;
rollCallWindow.ShowDialog();
} }
else else
{ {
@@ -1378,11 +1380,15 @@ namespace Ink_Canvas
// 调用失败时回退到相应的点名窗口 // 调用失败时回退到相应的点名窗口
if (Settings.RandSettings.UseNewRollCallUI) if (Settings.RandSettings.UseNewRollCallUI)
{ {
new NewStyleRollCallWindow(Settings, true).ShowDialog(); // 单次抽模式 var rollCallWindow = new NewStyleRollCallWindow(Settings, true); // 单次抽模式
rollCallWindow.Owner = this;
rollCallWindow.ShowDialog();
} }
else else
{ {
new RandWindow(Settings, true).ShowDialog(); var randWindow = new RandWindow(Settings, true);
randWindow.Owner = this;
randWindow.ShowDialog();
} }
} }
} }
@@ -1392,12 +1398,16 @@ namespace Ink_Canvas
if (Settings.RandSettings.UseNewRollCallUI) if (Settings.RandSettings.UseNewRollCallUI)
{ {
// 使用新点名UI - 单次抽模式 // 使用新点名UI - 单次抽模式
new NewStyleRollCallWindow(Settings, true).ShowDialog(); var rollCallWindow = new NewStyleRollCallWindow(Settings, true);
rollCallWindow.Owner = this;
rollCallWindow.ShowDialog();
} }
else else
{ {
// 使用默认的随机点名窗口 // 使用默认的随机点名窗口
new RandWindow(Settings, true).ShowDialog(); var randWindow = new RandWindow(Settings, true);
randWindow.Owner = this;
randWindow.ShowDialog();
} }
} }
} }
+36 -6
View File
@@ -1652,7 +1652,7 @@ namespace Ink_Canvas
if (int.TryParse(File.ReadAllText(positionFile), out var page) && page > 0) if (int.TryParse(File.ReadAllText(positionFile), out var page) && page > 0)
{ {
_lastPlaybackPage = page; _lastPlaybackPage = page;
new YesOrNoNotificationWindow($"上次播放到了第 {page} 页, 是否立即跳转", () => var yesNoWindow = new YesOrNoNotificationWindow($"上次播放到了第 {page} 页, 是否立即跳转", () =>
{ {
try try
{ {
@@ -1674,7 +1674,17 @@ namespace Ink_Canvas
{ {
LogHelper.WriteLogToFile($"跳转到第{page}页失败: {ex}", LogHelper.LogType.Error); LogHelper.WriteLogToFile($"跳转到第{page}页失败: {ex}", LogHelper.LogType.Error);
} }
}).ShowDialog(); });
yesNoWindow.Owner = this;
PauseTopmostMaintenance();
try
{
yesNoWindow.ShowDialog();
}
finally
{
ResumeTopmostMaintenance();
}
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -1716,7 +1726,7 @@ namespace Ink_Canvas
if (hasHiddenSlides && !IsShowingRestoreHiddenSlidesWindow) if (hasHiddenSlides && !IsShowingRestoreHiddenSlidesWindow)
{ {
IsShowingRestoreHiddenSlidesWindow = true; IsShowingRestoreHiddenSlidesWindow = true;
new YesOrNoNotificationWindow("检测到此演示文档中包含隐藏的幻灯片,是否取消隐藏?", var yesNoWindow = new YesOrNoNotificationWindow("检测到此演示文档中包含隐藏的幻灯片,是否取消隐藏?",
() => () =>
{ {
try try
@@ -1740,7 +1750,17 @@ namespace Ink_Canvas
} }
}, },
() => { IsShowingRestoreHiddenSlidesWindow = false; }, () => { IsShowingRestoreHiddenSlidesWindow = false; },
() => { IsShowingRestoreHiddenSlidesWindow = false; }).ShowDialog(); () => { IsShowingRestoreHiddenSlidesWindow = false; });
yesNoWindow.Owner = this;
PauseTopmostMaintenance();
try
{
yesNoWindow.ShowDialog();
}
finally
{
ResumeTopmostMaintenance();
}
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -1786,7 +1806,7 @@ namespace Ink_Canvas
if (hasSlideTimings && !IsShowingAutoplaySlidesWindow) if (hasSlideTimings && !IsShowingAutoplaySlidesWindow)
{ {
IsShowingAutoplaySlidesWindow = true; IsShowingAutoplaySlidesWindow = true;
new YesOrNoNotificationWindow("检测到此演示文档中自动播放或排练计时已经启用,可能导致幻灯片自动翻页,是否取消?", var yesNoWindow = new YesOrNoNotificationWindow("检测到此演示文档中自动播放或排练计时已经启用,可能导致幻灯片自动翻页,是否取消?",
() => () =>
{ {
try try
@@ -1806,7 +1826,17 @@ namespace Ink_Canvas
} }
}, },
() => { IsShowingAutoplaySlidesWindow = false; }, () => { IsShowingAutoplaySlidesWindow = false; },
() => { IsShowingAutoplaySlidesWindow = false; }).ShowDialog(); () => { IsShowingAutoplaySlidesWindow = false; });
yesNoWindow.Owner = this;
PauseTopmostMaintenance();
try
{
yesNoWindow.ShowDialog();
}
finally
{
ResumeTopmostMaintenance();
}
} }
} }
catch (Exception ex) catch (Exception ex)
+3 -1
View File
@@ -401,7 +401,9 @@ namespace Ink_Canvas
if (!ok) return; if (!ok) return;
} }
new NamesInputWindow().ShowDialog(); var namesInputWindow = new NamesInputWindow();
namesInputWindow.Owner = this;
namesInputWindow.ShowDialog();
Window_Loaded(this, null); Window_Loaded(this, null);
} }
@@ -1,5 +1,8 @@
using System; using System;
using System.Runtime.InteropServices;
using System.Windows; using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;
namespace Ink_Canvas namespace Ink_Canvas
{ {
@@ -8,9 +11,26 @@ namespace Ink_Canvas
/// </summary> /// </summary>
public partial class YesOrNoNotificationWindow : Window public partial class YesOrNoNotificationWindow : Window
{ {
[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 IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(IntPtr hWnd);
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_SHOWWINDOW = 0x0040;
private const uint SWP_NOOWNERZORDER = 0x0200;
private readonly Action _yesAction; private readonly Action _yesAction;
private readonly Action _noAction; private readonly Action _noAction;
private readonly Action _windowClose; private readonly Action _windowClose;
private DispatcherTimer _topmostCheckTimer;
private IntPtr _hwnd;
public YesOrNoNotificationWindow(string text, Action yesAction = null, Action noAction = null, Action windowClose = null) public YesOrNoNotificationWindow(string text, Action yesAction = null, Action noAction = null, Action windowClose = null)
{ {
@@ -19,6 +39,62 @@ namespace Ink_Canvas
_windowClose = windowClose; _windowClose = windowClose;
InitializeComponent(); InitializeComponent();
Label.Text = text; Label.Text = text;
Loaded += YesOrNoNotificationWindow_Loaded;
Closed += YesOrNoNotificationWindow_Closed;
}
private void YesOrNoNotificationWindow_Loaded(object sender, RoutedEventArgs e)
{
_hwnd = new WindowInteropHelper(this).Handle;
Topmost = true;
Activate();
Focus();
if (_hwnd != IntPtr.Zero)
{
SetWindowPos(_hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOOWNERZORDER);
}
_topmostCheckTimer = new DispatcherTimer();
_topmostCheckTimer.Interval = TimeSpan.FromMilliseconds(100);
_topmostCheckTimer.Tick += TopmostCheckTimer_Tick;
_topmostCheckTimer.Start();
}
private void TopmostCheckTimer_Tick(object sender, EventArgs e)
{
try
{
if (_hwnd == IntPtr.Zero) return;
var foregroundWindow = GetForegroundWindow();
if (foregroundWindow != _hwnd && IsWindowVisible(_hwnd))
{
Topmost = true;
Activate();
Focus();
SetWindowPos(_hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOOWNERZORDER);
}
}
catch (Exception)
{
}
}
private void YesOrNoNotificationWindow_Closed(object sender, EventArgs e)
{
if (_topmostCheckTimer != null)
{
_topmostCheckTimer.Stop();
_topmostCheckTimer.Tick -= TopmostCheckTimer_Tick;
_topmostCheckTimer = null;
}
} }
private void ButtonYes_Click(object sender, RoutedEventArgs e) private void ButtonYes_Click(object sender, RoutedEventArgs e)