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" xmlns:processbars="clr-namespace:Ink_Canvas.ProcessBars"
ui:ThemeManager.RequestedTheme="Light" Topmost="True" Background="Transparent" ui:ThemeManager.RequestedTheme="Light" Topmost="True" Background="Transparent"
mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True" mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True"
ResizeMode="CanMinimize"
Loaded="Window_Loaded" Closing="Window_Closing" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Closing="Window_Closing" WindowStartupLocation="CenterScreen"
Title="Ink Canvas 画板 - 计时器" Height="700" Width="1100"> Title="Ink Canvas 画板 - 计时器" Height="700" Width="1100">
<Border Background="{DynamicResource TimerWindowBackground}" CornerRadius="10" BorderThickness="1" BorderBrush="{DynamicResource TimerWindowBorderBrush}" Margin="60"> <Border Background="{DynamicResource TimerWindowBackground}" CornerRadius="10" BorderThickness="1" BorderBrush="{DynamicResource TimerWindowBorderBrush}" Margin="60">
@@ -1,7 +1,9 @@
using System; using System;
using System.Runtime.InteropServices;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Timers; using System.Timers;
@@ -33,8 +35,66 @@ namespace Ink_Canvas
updateTimer.Start(); updateTimer.Start();
parentWindow.TimerCompleted += ParentWindow_TimerCompleted; 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) private void UpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
{ {
if (parentWindow != null) if (parentWindow != null)
@@ -7,6 +7,7 @@
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
Topmost="True" Background="Transparent" Topmost="True" Background="Transparent"
mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True" mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True"
ResizeMode="NoResize"
WindowStartupLocation="Manual" Title="计时器" Height="200" Width="600" WindowStartupLocation="Manual" Title="计时器" Height="200" Width="600"
MouseLeftButtonDown="Window_MouseLeftButtonDown" MouseLeftButtonUp="Window_MouseLeftButtonUp" MouseLeftButtonDown="Window_MouseLeftButtonDown" MouseLeftButtonUp="Window_MouseLeftButtonUp"
MouseEnter="Window_MouseEnter" MouseLeave="Window_MouseLeave" MouseMove="Window_MouseMove"> MouseEnter="Window_MouseEnter" MouseLeave="Window_MouseLeave" MouseMove="Window_MouseMove">
@@ -1,7 +1,9 @@
using System; using System;
using System.Runtime.InteropServices;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Windows.Media.Animation; using System.Windows.Media.Animation;
@@ -40,8 +42,66 @@ namespace Ink_Canvas
// 应用主题 // 应用主题
ApplyTheme(); 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> /// <summary>
/// 根据屏幕分辨率和 DPI 缩放窗口大小(保持原始尺寸,使用Transform缩放) /// 根据屏幕分辨率和 DPI 缩放窗口大小(保持原始尺寸,使用Transform缩放)
/// </summary> /// </summary>
@@ -67,6 +67,25 @@ namespace Ink_Canvas
// 初始化点名相关变量 // 初始化点名相关变量
InitializeRollCallData(); 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) if (isSingleDrawMode)
{ {
@@ -102,6 +121,27 @@ namespace Ink_Canvas
// 初始化点名相关变量 // 初始化点名相关变量
InitializeRollCallData(); 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) if (isSingleDrawMode)
{ {
@@ -1671,6 +1711,21 @@ namespace Ink_Canvas
System.Threading.Thread.Sleep(autoCloseWaitTime); System.Threading.Thread.Sleep(autoCloseWaitTime);
Application.Current.Dispatcher.Invoke(() => 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(); Close();
}); });
}).Start(); }).Start();
@@ -1743,6 +1798,22 @@ namespace Ink_Canvas
System.Threading.Thread.Sleep(autoCloseWaitTime); System.Threading.Thread.Sleep(autoCloseWaitTime);
Application.Current.Dispatcher.Invoke(() => 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(); Close();
}); });
}).Start(); }).Start();
+53 -7
View File
@@ -52,6 +52,26 @@ namespace Ink_Canvas
// 添加窗口加载事件处理,确保置顶 // 添加窗口加载事件处理,确保置顶
Loaded += TimerWindow_Loaded; 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); StartPauseIcon.Data = Geometry.Parse(PlayIconData);
PlayTimerSound(); PlayTimerSound();
// 禁用全屏按钮
if (FullscreenBtn != null)
{
FullscreenBtn.IsEnabled = false;
}
TimerCompleted?.Invoke(this, EventArgs.Empty); TimerCompleted?.Invoke(this, EventArgs.Empty);
HandleTimerCompletion(); HandleTimerCompletion();
} }
@@ -337,7 +363,7 @@ namespace Ink_Canvas
minimizedWindow = new MinimizedTimerWindow(this); minimizedWindow = new MinimizedTimerWindow(this);
minimizedWindow.Show(); minimizedWindow.Show();
// 确保最小化窗口也置顶 // 确保最小化窗口也置顶(窗口加载时会自动应用)
minimizedWindow.Topmost = true; minimizedWindow.Topmost = true;
// 隐藏主窗口 // 隐藏主窗口
@@ -793,6 +819,12 @@ namespace Ink_Canvas
// 保存到最近计时记录 // 保存到最近计时记录
SaveRecentTimer(); SaveRecentTimer();
// 启用全屏按钮
if (FullscreenBtn != null)
{
FullscreenBtn.IsEnabled = true;
}
} }
} }
@@ -808,6 +840,12 @@ namespace Ink_Canvas
isOvertimeMode = false; isOvertimeMode = false;
UpdateDigitDisplays(); UpdateDigitDisplays();
StartPauseIcon.Data = Geometry.Parse(PlayIconData); StartPauseIcon.Data = Geometry.Parse(PlayIconData);
// 禁用全屏按钮
if (FullscreenBtn != null)
{
FullscreenBtn.IsEnabled = false;
}
} }
private void PlayTimerSound() private void PlayTimerSound()
@@ -884,6 +922,13 @@ namespace Ink_Canvas
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{ {
isTimerRunning = false; isTimerRunning = false;
// 恢复主窗口的置顶维护
var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow != null)
{
mainWindow.ResumeTopmostMaintenance();
}
} }
private void CloseButton_Click(object sender, RoutedEventArgs e) private void CloseButton_Click(object sender, RoutedEventArgs e)
@@ -1292,6 +1337,10 @@ namespace Ink_Canvas
private void Fullscreen_Click(object sender, RoutedEventArgs e) private void Fullscreen_Click(object sender, RoutedEventArgs e)
{ {
if (!isTimerRunning)
{
return;
}
ShowFullscreenTimer(); ShowFullscreenTimer();
} }
@@ -1304,7 +1353,7 @@ namespace Ink_Canvas
fullscreenWindow = new FullscreenTimerWindow(this); fullscreenWindow = new FullscreenTimerWindow(this);
fullscreenWindow.Show(); fullscreenWindow.Show();
// 确保全屏窗口也置顶 // 确保全屏窗口也置顶(窗口加载时会自动应用)
fullscreenWindow.Topmost = true; fullscreenWindow.Topmost = true;
// 隐藏主窗口 // 隐藏主窗口
@@ -1384,10 +1433,6 @@ namespace Ink_Canvas
var hwnd = new WindowInteropHelper(this).Handle; var hwnd = new WindowInteropHelper(this).Handle;
if (hwnd == IntPtr.Zero) return; if (hwnd == IntPtr.Zero) return;
// 强制激活窗口
Activate();
Focus();
// 设置WPF的Topmost属性 // 设置WPF的Topmost属性
Topmost = true; Topmost = true;
@@ -1397,8 +1442,9 @@ namespace Ink_Canvas
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOPMOST); SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOPMOST);
// 2. 使用SetWindowPos确保窗口在最顶层 // 2. 使用SetWindowPos确保窗口在最顶层
// 使用HWND_TOPMOST确保窗口始终在所有其他窗口之上,包括主窗口
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, 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); LogHelper.WriteLogToFile("计时器窗口已应用置顶", LogHelper.LogType.Trace);
} }
+8 -2
View File
@@ -8,6 +8,7 @@
xmlns:controls="clr-namespace:Ink_Canvas.Windows.Controls" xmlns:controls="clr-namespace:Ink_Canvas.Windows.Controls"
Topmost="True" Background="Transparent" Topmost="True" Background="Transparent"
mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True" mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True"
ResizeMode="CanMinimize"
Loaded="Window_Loaded" Closing="Window_Closing" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Closing="Window_Closing" WindowStartupLocation="CenterScreen"
MouseMove="Window_MouseMove" MouseEnter="Window_MouseEnter" MouseMove="Window_MouseMove" MouseEnter="Window_MouseEnter"
Title="Ink Canvas 画板 - 计时器" Height="450" Width="900"> Title="Ink Canvas 画板 - 计时器" Height="450" Width="900">
@@ -714,12 +715,17 @@
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<!-- 全屏按钮 --> <!-- 全屏按钮 -->
<Button x:Name="FullscreenBtn" Width="80" Height="40" Background="{DynamicResource NewTimerWindowButtonBackground}" <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> <Button.Template>
<ControlTemplate TargetType="Button"> <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"/> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border> </Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Opacity" Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate> </ControlTemplate>
</Button.Template> </Button.Template>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">