improve:issue #332
This commit is contained in:
@@ -31,13 +31,213 @@ namespace Ink_Canvas.Windows
|
||||
// 监听主题变化事件
|
||||
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
|
||||
|
||||
// 监听分辨率变化事件
|
||||
SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
|
||||
|
||||
Unloaded += MinimizedTimerControl_Unloaded;
|
||||
|
||||
// 监听加载事件,设置UI大小
|
||||
Loaded += MinimizedTimerControl_Loaded;
|
||||
}
|
||||
|
||||
private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
|
||||
{
|
||||
// 分辨率变化时重新计算大小
|
||||
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
UpdateMinimizedTimerControlSize();
|
||||
}), System.Windows.Threading.DispatcherPriority.Loaded);
|
||||
}
|
||||
|
||||
private void MainWindow_DpiChanged(object sender, DpiChangedEventArgs e)
|
||||
{
|
||||
// DPI变化时重新计算大小
|
||||
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
UpdateMinimizedTimerControlSize();
|
||||
}), System.Windows.Threading.DispatcherPriority.Loaded);
|
||||
}
|
||||
|
||||
private void MinimizedTimerControl_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// 根据DPI和分辨率限制UI大小为屏幕的35%
|
||||
UpdateMinimizedTimerControlSize();
|
||||
|
||||
// 监听DPI变化事件(通过主窗口)
|
||||
var mainWindow = Application.Current.MainWindow as MainWindow;
|
||||
if (mainWindow != null)
|
||||
{
|
||||
mainWindow.DpiChanged += MainWindow_DpiChanged;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据DPI和分辨率更新最小化计时器控件大小,限制在屏幕大小的35%
|
||||
/// </summary>
|
||||
private void UpdateMinimizedTimerControlSize()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取主窗口
|
||||
var mainWindow = Application.Current.MainWindow as MainWindow;
|
||||
if (mainWindow == null) return;
|
||||
|
||||
// 获取DPI缩放因子
|
||||
var source = PresentationSource.FromVisual(mainWindow);
|
||||
double dpiScaleX = 1.0, dpiScaleY = 1.0;
|
||||
if (source != null && source.CompositionTarget != null)
|
||||
{
|
||||
var transform = source.CompositionTarget.TransformToDevice;
|
||||
dpiScaleX = transform.M11;
|
||||
dpiScaleY = transform.M22;
|
||||
}
|
||||
|
||||
// 获取屏幕工作区域大小(考虑DPI)
|
||||
double screenWidth = SystemParameters.WorkArea.Width / dpiScaleX;
|
||||
double screenHeight = SystemParameters.WorkArea.Height / dpiScaleY;
|
||||
|
||||
// 计算最大尺寸(屏幕的35%)
|
||||
double maxWidth = screenWidth * 0.35;
|
||||
double maxHeight = screenHeight * 0.35;
|
||||
|
||||
// 保持宽高比(基于原始设计尺寸 600x200,宽高比约为 3:1)
|
||||
double aspectRatio = 600.0 / 200.0;
|
||||
double calculatedWidth = maxWidth;
|
||||
double calculatedHeight = calculatedWidth / aspectRatio;
|
||||
|
||||
// 如果计算出的高度超过最大高度,则按高度计算
|
||||
if (calculatedHeight > maxHeight)
|
||||
{
|
||||
calculatedHeight = maxHeight;
|
||||
calculatedWidth = calculatedHeight * aspectRatio;
|
||||
}
|
||||
|
||||
// 计算缩放比例(基于原始尺寸600x200)
|
||||
double originalWidth = 600.0;
|
||||
double originalHeight = 200.0;
|
||||
double scaleX = calculatedWidth / originalWidth;
|
||||
double scaleY = calculatedHeight / originalHeight;
|
||||
double scale = Math.Min(scaleX, scaleY); // 使用较小的缩放比例以保持比例
|
||||
|
||||
// 查找父容器(MinimizedTimerContainer)
|
||||
var parent = this.Parent as FrameworkElement;
|
||||
if (parent == null)
|
||||
{
|
||||
// 如果直接父元素不是FrameworkElement,尝试查找Border
|
||||
var visualParent = System.Windows.Media.VisualTreeHelper.GetParent(this);
|
||||
while (visualParent != null)
|
||||
{
|
||||
if (visualParent is FrameworkElement fe && fe.Name == "MinimizedTimerContainer")
|
||||
{
|
||||
parent = fe;
|
||||
break;
|
||||
}
|
||||
visualParent = System.Windows.Media.VisualTreeHelper.GetParent(visualParent);
|
||||
}
|
||||
}
|
||||
|
||||
if (parent != null)
|
||||
{
|
||||
// 设置父容器大小
|
||||
parent.Width = calculatedWidth;
|
||||
parent.Height = calculatedHeight;
|
||||
|
||||
// 更新数字显示(Path元素)的大小 - 原始尺寸72x72
|
||||
double digitSize = 72.0 * scale;
|
||||
UpdateElementSize("MinHour1Display", digitSize, digitSize);
|
||||
UpdateElementSize("MinHour2Display", digitSize, digitSize);
|
||||
UpdateElementSize("MinMinute1Display", digitSize, digitSize);
|
||||
UpdateElementSize("MinMinute2Display", digitSize, digitSize);
|
||||
UpdateElementSize("MinSecond1Display", digitSize, digitSize);
|
||||
UpdateElementSize("MinSecond2Display", digitSize, digitSize);
|
||||
|
||||
// 更新冒号字体大小 - 原始尺寸48
|
||||
double colonFontSize = 48.0 * scale;
|
||||
UpdateTextBlockFontSize("MinColon1Display", colonFontSize);
|
||||
UpdateTextBlockFontSize("MinColon2Display", colonFontSize);
|
||||
|
||||
// 更新关闭按钮大小 - 原始尺寸24x24
|
||||
double closeButtonSize = 24.0 * scale;
|
||||
UpdateElementSize("CloseButton", closeButtonSize, closeButtonSize);
|
||||
|
||||
// 更新关闭按钮字体大小 - 原始尺寸12
|
||||
double closeButtonFontSize = 12.0 * scale;
|
||||
var closeButton = this.FindName("CloseButton") as System.Windows.Controls.Button;
|
||||
if (closeButton != null)
|
||||
{
|
||||
var textBlock = closeButton.Content as System.Windows.Controls.TextBlock;
|
||||
if (textBlock != null)
|
||||
{
|
||||
textBlock.FontSize = closeButtonFontSize;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新Margin - 原始Margin是12,需要按比例缩放
|
||||
double marginSize = 12.0 * scale;
|
||||
UpdateElementMargin("MinHour1Display", new Thickness(0, 0, marginSize, 0));
|
||||
UpdateElementMargin("MinHour2Display", new Thickness(0, 0, marginSize, 0));
|
||||
UpdateElementMargin("MinColon1Display", new Thickness(0, 0, marginSize, 0));
|
||||
UpdateElementMargin("MinMinute1Display", new Thickness(0, 0, marginSize, 0));
|
||||
UpdateElementMargin("MinMinute2Display", new Thickness(0, 0, marginSize, 0));
|
||||
UpdateElementMargin("MinColon2Display", new Thickness(0, 0, marginSize, 0));
|
||||
UpdateElementMargin("MinSecond1Display", new Thickness(0, 0, marginSize, 0));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"更新最小化计时器控件大小失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新元素的大小
|
||||
/// </summary>
|
||||
private void UpdateElementSize(string elementName, double width, double height)
|
||||
{
|
||||
var element = this.FindName(elementName) as FrameworkElement;
|
||||
if (element != null)
|
||||
{
|
||||
element.Width = width;
|
||||
element.Height = height;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新TextBlock的字体大小
|
||||
/// </summary>
|
||||
private void UpdateTextBlockFontSize(string elementName, double fontSize)
|
||||
{
|
||||
var textBlock = this.FindName(elementName) as System.Windows.Controls.TextBlock;
|
||||
if (textBlock != null)
|
||||
{
|
||||
textBlock.FontSize = fontSize;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新元素的Margin
|
||||
/// </summary>
|
||||
private void UpdateElementMargin(string elementName, Thickness margin)
|
||||
{
|
||||
var element = this.FindName(elementName) as FrameworkElement;
|
||||
if (element != null)
|
||||
{
|
||||
element.Margin = margin;
|
||||
}
|
||||
}
|
||||
|
||||
private void MinimizedTimerControl_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// 取消订阅主题变化事件
|
||||
SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged;
|
||||
SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;
|
||||
|
||||
// 取消订阅DPI变化事件
|
||||
var mainWindow = Application.Current.MainWindow as MainWindow;
|
||||
if (mainWindow != null)
|
||||
{
|
||||
mainWindow.DpiChanged -= MainWindow_DpiChanged;
|
||||
}
|
||||
|
||||
if (parentControl != null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user