improve:计时器UI与点名UI

改进视觉反馈和按钮逻辑及窗口置顶
This commit is contained in:
2025-11-15 21:14:08 +08:00
parent 082c9a03ec
commit bf2b8fec35
7 changed files with 254 additions and 9 deletions
@@ -8,6 +8,7 @@
xmlns:processbars="clr-namespace:Ink_Canvas.ProcessBars"
ui:ThemeManager.RequestedTheme="Light" Topmost="True" Background="Transparent"
mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True"
ResizeMode="CanMinimize"
Loaded="Window_Loaded" Closing="Window_Closing" WindowStartupLocation="CenterScreen"
Title="Ink Canvas 画板 - 计时器" Height="700" Width="1100">
<Border Background="{DynamicResource TimerWindowBackground}" CornerRadius="10" BorderThickness="1" BorderBrush="{DynamicResource TimerWindowBorderBrush}" Margin="60">
@@ -1,7 +1,9 @@
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Timers;
@@ -33,7 +35,65 @@ namespace Ink_Canvas
updateTimer.Start();
parentWindow.TimerCompleted += ParentWindow_TimerCompleted;
// 确保窗口置顶
Loaded += FullscreenTimerWindow_Loaded;
}
private void FullscreenTimerWindow_Loaded(object sender, RoutedEventArgs e)
{
// 使用延迟确保窗口完全加载后再应用置顶
Dispatcher.BeginInvoke(new Action(() =>
{
ApplyTopmost();
}), System.Windows.Threading.DispatcherPriority.Loaded);
}
#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);
private const int GWL_EXSTYLE = -20;
private const int WS_EX_TOPMOST = 0x00000008;
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_NOACTIVATE = 0x0010;
private const uint SWP_SHOWWINDOW = 0x0040;
/// <summary>
/// 应用全屏窗口置顶
/// </summary>
private void ApplyTopmost()
{
try
{
var hwnd = new WindowInteropHelper(this).Handle;
if (hwnd == IntPtr.Zero) return;
// 设置WPF的Topmost属性
Topmost = true;
// 使用Win32 API强制置顶
int exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOPMOST);
// 使用SetWindowPos确保窗口在最顶层
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"应用全屏窗口置顶失败: {ex.Message}");
}
}
#endregion
private void UpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
{
@@ -7,6 +7,7 @@
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
Topmost="True" Background="Transparent"
mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True"
ResizeMode="NoResize"
WindowStartupLocation="Manual" Title="计时器" Height="200" Width="600"
MouseLeftButtonDown="Window_MouseLeftButtonDown" MouseLeftButtonUp="Window_MouseLeftButtonUp"
MouseEnter="Window_MouseEnter" MouseLeave="Window_MouseLeave" MouseMove="Window_MouseMove">
@@ -1,7 +1,9 @@
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
@@ -40,7 +42,65 @@ namespace Ink_Canvas
// 应用主题
ApplyTheme();
// 确保窗口置顶
Loaded += MinimizedTimerWindow_Loaded;
}
private void MinimizedTimerWindow_Loaded(object sender, RoutedEventArgs e)
{
// 使用延迟确保窗口完全加载后再应用置顶
Dispatcher.BeginInvoke(new Action(() =>
{
ApplyTopmost();
}), System.Windows.Threading.DispatcherPriority.Loaded);
}
#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);
private const int GWL_EXSTYLE = -20;
private const int WS_EX_TOPMOST = 0x00000008;
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_NOACTIVATE = 0x0010;
private const uint SWP_SHOWWINDOW = 0x0040;
/// <summary>
/// 应用最小化窗口置顶
/// </summary>
private void ApplyTopmost()
{
try
{
var hwnd = new WindowInteropHelper(this).Handle;
if (hwnd == IntPtr.Zero) return;
// 设置WPF的Topmost属性
Topmost = true;
// 使用Win32 API强制置顶
int exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOPMOST);
// 使用SetWindowPos确保窗口在最顶层
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"应用最小化窗口置顶失败: {ex.Message}");
}
}
#endregion
/// <summary>
/// 根据屏幕分辨率和 DPI 缩放窗口大小(保持原始尺寸,使用Transform缩放)
@@ -67,6 +67,25 @@ namespace Ink_Canvas
// 初始化点名相关变量
InitializeRollCallData();
if (isSingleDrawMode)
{
if (ControlOptionsGrid != null)
{
ControlOptionsGrid.Opacity = 0.4;
ControlOptionsGrid.IsHitTestVisible = false;
}
if (StartRollCallBtn != null)
{
StartRollCallBtn.Opacity = 0.4;
StartRollCallBtn.IsEnabled = false;
}
if (ResetBtn != null)
{
ResetBtn.Opacity = 0.4;
ResetBtn.IsEnabled = false;
}
}
// 单次抽模式:自动开始抽选
if (isSingleDrawMode)
{
@@ -102,6 +121,27 @@ namespace Ink_Canvas
// 初始化点名相关变量
InitializeRollCallData();
// 单次抽模式:禁用控制面板,阻止用户点击按钮
if (isSingleDrawMode)
{
if (ControlOptionsGrid != null)
{
ControlOptionsGrid.Opacity = 0.4;
ControlOptionsGrid.IsHitTestVisible = false;
}
// 禁用开始点名和重置按钮
if (StartRollCallBtn != null)
{
StartRollCallBtn.Opacity = 0.4;
StartRollCallBtn.IsEnabled = false;
}
if (ResetBtn != null)
{
ResetBtn.Opacity = 0.4;
ResetBtn.IsEnabled = false;
}
}
// 单次抽模式:自动开始抽选
if (isSingleDrawMode)
{
@@ -1671,6 +1711,21 @@ namespace Ink_Canvas
System.Threading.Thread.Sleep(autoCloseWaitTime);
Application.Current.Dispatcher.Invoke(() =>
{
if (ControlOptionsGrid != null)
{
ControlOptionsGrid.Opacity = 1;
ControlOptionsGrid.IsHitTestVisible = true;
}
if (StartRollCallBtn != null)
{
StartRollCallBtn.Opacity = 1;
StartRollCallBtn.IsEnabled = true;
}
if (ResetBtn != null)
{
ResetBtn.Opacity = 1;
ResetBtn.IsEnabled = true;
}
Close();
});
}).Start();
@@ -1743,6 +1798,22 @@ namespace Ink_Canvas
System.Threading.Thread.Sleep(autoCloseWaitTime);
Application.Current.Dispatcher.Invoke(() =>
{
if (ControlOptionsGrid != null)
{
ControlOptionsGrid.Opacity = 1;
ControlOptionsGrid.IsHitTestVisible = true;
}
// 恢复开始点名和重置按钮
if (StartRollCallBtn != null)
{
StartRollCallBtn.Opacity = 1;
StartRollCallBtn.IsEnabled = true;
}
if (ResetBtn != null)
{
ResetBtn.Opacity = 1;
ResetBtn.IsEnabled = true;
}
Close();
});
}).Start();
+53 -7
View File
@@ -52,6 +52,26 @@ namespace Ink_Canvas
// 添加窗口加载事件处理,确保置顶
Loaded += TimerWindow_Loaded;
// 暂停主窗口的置顶维护
var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow != null)
{
mainWindow.PauseTopmostMaintenance();
}
// 窗口关闭时恢复主窗口的置顶维护
Closing += TimerWindow_Closing;
}
private void TimerWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// 恢复主窗口的置顶维护
var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow != null)
{
mainWindow.ResumeTopmostMaintenance();
}
}
@@ -116,6 +136,12 @@ namespace Ink_Canvas
StartPauseIcon.Data = Geometry.Parse(PlayIconData);
PlayTimerSound();
// 禁用全屏按钮
if (FullscreenBtn != null)
{
FullscreenBtn.IsEnabled = false;
}
TimerCompleted?.Invoke(this, EventArgs.Empty);
HandleTimerCompletion();
}
@@ -337,7 +363,7 @@ namespace Ink_Canvas
minimizedWindow = new MinimizedTimerWindow(this);
minimizedWindow.Show();
// 确保最小化窗口也置顶
// 确保最小化窗口也置顶(窗口加载时会自动应用)
minimizedWindow.Topmost = true;
// 隐藏主窗口
@@ -793,6 +819,12 @@ namespace Ink_Canvas
// 保存到最近计时记录
SaveRecentTimer();
// 启用全屏按钮
if (FullscreenBtn != null)
{
FullscreenBtn.IsEnabled = true;
}
}
}
@@ -808,6 +840,12 @@ namespace Ink_Canvas
isOvertimeMode = false;
UpdateDigitDisplays();
StartPauseIcon.Data = Geometry.Parse(PlayIconData);
// 禁用全屏按钮
if (FullscreenBtn != null)
{
FullscreenBtn.IsEnabled = false;
}
}
private void PlayTimerSound()
@@ -884,6 +922,13 @@ namespace Ink_Canvas
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
isTimerRunning = false;
// 恢复主窗口的置顶维护
var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow != null)
{
mainWindow.ResumeTopmostMaintenance();
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
@@ -1292,6 +1337,10 @@ namespace Ink_Canvas
private void Fullscreen_Click(object sender, RoutedEventArgs e)
{
if (!isTimerRunning)
{
return;
}
ShowFullscreenTimer();
}
@@ -1304,7 +1353,7 @@ namespace Ink_Canvas
fullscreenWindow = new FullscreenTimerWindow(this);
fullscreenWindow.Show();
// 确保全屏窗口也置顶
// 确保全屏窗口也置顶(窗口加载时会自动应用)
fullscreenWindow.Topmost = true;
// 隐藏主窗口
@@ -1384,10 +1433,6 @@ namespace Ink_Canvas
var hwnd = new WindowInteropHelper(this).Handle;
if (hwnd == IntPtr.Zero) return;
// 强制激活窗口
Activate();
Focus();
// 设置WPF的Topmost属性
Topmost = true;
@@ -1397,8 +1442,9 @@ namespace Ink_Canvas
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOPMOST);
// 2. 使用SetWindowPos确保窗口在最顶层
// 使用HWND_TOPMOST确保窗口始终在所有其他窗口之上,包括主窗口
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOOWNERZORDER);
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
LogHelper.WriteLogToFile("计时器窗口已应用置顶", LogHelper.LogType.Trace);
}
+8 -2
View File
@@ -8,6 +8,7 @@
xmlns:controls="clr-namespace:Ink_Canvas.Windows.Controls"
Topmost="True" Background="Transparent"
mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True"
ResizeMode="CanMinimize"
Loaded="Window_Loaded" Closing="Window_Closing" WindowStartupLocation="CenterScreen"
MouseMove="Window_MouseMove" MouseEnter="Window_MouseEnter"
Title="Ink Canvas 画板 - 计时器" Height="450" Width="900">
@@ -714,12 +715,17 @@
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<!-- 全屏按钮 -->
<Button x:Name="FullscreenBtn" Width="80" Height="40" Background="{DynamicResource NewTimerWindowButtonBackground}"
BorderThickness="0" Click="Fullscreen_Click" Cursor="Hand" Margin="0,0,30,0">
BorderThickness="0" Click="Fullscreen_Click" Cursor="Hand" Margin="0,0,30,0" IsEnabled="False">
<Button.Template>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" CornerRadius="8">
<Border x:Name="border" Background="{TemplateBinding Background}" CornerRadius="8" Opacity="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Opacity" Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">