This commit is contained in:
2025-12-27 22:57:22 +08:00
parent ab6425e0d9
commit e972adfbcb
4 changed files with 127 additions and 1 deletions
+70
View File
@@ -340,6 +340,66 @@ namespace Ink_Canvas
}
}
/// <summary>
/// 根据DPI缩放因子调整TimerContainer的尺寸
/// </summary>
private void AdjustTimerContainerSize()
{
try
{
var timerContainer = FindName("TimerContainer") as FrameworkElement;
if (timerContainer == null) return;
var source = System.Windows.PresentationSource.FromVisual(this);
if (source != null)
{
var dpiScaleX = source.CompositionTarget.TransformToDevice.M11;
var dpiScaleY = source.CompositionTarget.TransformToDevice.M22;
// 如果DPI缩放因子大于1.25,则适当缩小容器尺寸
// 这样可以确保在高DPI屏幕上,计时器窗口的物理像素大小不会过大
if (dpiScaleX > 1.25 || dpiScaleY > 1.25)
{
// 使用较小的缩放因子来限制最大尺寸
double scaleFactor = Math.Min(dpiScaleX, dpiScaleY);
// 计算目标物理像素大小(约1350x750物理像素)
// 然后转换为逻辑像素
double targetPhysicalWidth = 1350;
double targetPhysicalHeight = 750;
// 转换为逻辑像素
double maxWidth = targetPhysicalWidth / scaleFactor;
double maxHeight = targetPhysicalHeight / scaleFactor;
// 确保不会小于原始尺寸的70%
maxWidth = Math.Max(maxWidth, 900 * 0.7);
maxHeight = Math.Max(maxHeight, 500 * 0.7);
// 应用调整后的尺寸
timerContainer.Width = maxWidth;
timerContainer.Height = maxHeight;
}
else
{
// 标准DPI,使用原始尺寸
timerContainer.Width = 900;
timerContainer.Height = 500;
}
}
else
{
// 无法获取DPI信息,使用原始尺寸
timerContainer.Width = 900;
timerContainer.Height = 500;
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"调整TimerContainer尺寸失败: {ex.Message}", LogHelper.LogType.Error);
}
}
#endregion
@@ -787,6 +847,16 @@ namespace Ink_Canvas
{
ShowNotification($"系统DPI发生变化,从 {e.OldDpi.DpiScaleX}x{e.OldDpi.DpiScaleY} 变化为 {e.NewDpi.DpiScaleX}x{e.NewDpi.DpiScaleY}");
// 如果TimerContainer可见,调整其尺寸
Dispatcher.Invoke(() =>
{
var timerContainer = FindName("TimerContainer") as FrameworkElement;
if (timerContainer != null && timerContainer.Visibility == Visibility.Visible)
{
AdjustTimerContainerSize();
}
});
new Thread(() =>
{
var isFloatingBarOutsideScreen = false;
@@ -1033,6 +1033,10 @@ namespace Ink_Canvas
{
// 每次打开计时器窗口时重置计时器
TimerControl.ResetTimerState();
// 根据DPI缩放因子调整TimerContainer的尺寸
AdjustTimerContainerSize();
TimerContainer.Visibility = Visibility.Visible;
if (MinimizedTimerContainer != null)
{
+5 -1
View File
@@ -34,7 +34,11 @@
<!-- 主要内容区域 -->
<Grid>
<!-- 使用Viewbox自动缩放内容 -->
<Viewbox x:Name="MainViewController" Margin="20,20,20,20">
<Viewbox x:Name="MainViewController"
Margin="20,20,20,20"
StretchDirection="DownOnly"
MaxWidth="900"
MaxHeight="500">
<Grid Height="400" Width="900">
<!-- 顶部标题栏 -->
<Grid Height="50"
+48
View File
@@ -50,6 +50,54 @@ namespace Ink_Canvas.Windows
// 监听卸载事件,清理资源
Unloaded += TimerControl_Unloaded;
// 监听加载事件,调整DPI相关的尺寸
Loaded += TimerControl_Loaded;
}
private void TimerControl_Loaded(object sender, RoutedEventArgs e)
{
// 根据DPI缩放因子调整Viewbox的最大尺寸,确保在高DPI屏幕上不会过大
try
{
var source = System.Windows.PresentationSource.FromVisual(this);
if (source != null)
{
var dpiScaleX = source.CompositionTarget.TransformToDevice.M11;
var dpiScaleY = source.CompositionTarget.TransformToDevice.M22;
// 如果DPI缩放因子大于1.25,则适当缩小最大尺寸
// 这样可以确保在高DPI屏幕上,计时器窗口的物理像素大小不会过大
if (dpiScaleX > 1.25 || dpiScaleY > 1.25)
{
// 使用较小的缩放因子来限制最大尺寸
double scaleFactor = Math.Min(dpiScaleX, dpiScaleY);
if (MainViewController != null)
{
// 计算目标物理像素大小(约1350x750物理像素)
// 然后转换为逻辑像素
double targetPhysicalWidth = 1350;
double targetPhysicalHeight = 750;
// 转换为逻辑像素
double maxWidth = targetPhysicalWidth / scaleFactor;
double maxHeight = targetPhysicalHeight / scaleFactor;
// 确保不会小于原始尺寸的70%
maxWidth = Math.Max(maxWidth, 900 * 0.7);
maxHeight = Math.Max(maxHeight, 500 * 0.7);
MainViewController.MaxWidth = maxWidth;
MainViewController.MaxHeight = maxHeight;
}
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"调整计时器窗口DPI尺寸失败: {ex.Message}", LogHelper.LogType.Error);
}
}
private void TimerControl_Unloaded(object sender, RoutedEventArgs e)