From 8317865b415587f1550aeca656f33f2c7f136584 Mon Sep 17 00:00:00 2001
From: croire <1432593898@qq.com>
Date: Sat, 19 Mar 2022 16:32:43 +0800
Subject: [PATCH] =?UTF-8?q?150%=E7=BC=A9=E6=94=BE=E4=B8=8B=E7=9A=84?=
=?UTF-8?q?=E7=AA=97=E5=8F=A3=E4=B8=8D=E8=B6=85=E5=87=BA=E5=B1=8F=E5=B9=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
DownKyi/DownKyi.csproj | 1 +
DownKyi/Utils/WindowAdaptation.cs | 146 ++++++++++++++++++++++++++++++
DownKyi/Views/MainWindow.xaml | 8 +-
3 files changed, 153 insertions(+), 2 deletions(-)
create mode 100644 DownKyi/Utils/WindowAdaptation.cs
diff --git a/DownKyi/DownKyi.csproj b/DownKyi/DownKyi.csproj
index 1ee1080..64479af 100644
--- a/DownKyi/DownKyi.csproj
+++ b/DownKyi/DownKyi.csproj
@@ -100,6 +100,7 @@
+
diff --git a/DownKyi/Utils/WindowAdaptation.cs b/DownKyi/Utils/WindowAdaptation.cs
new file mode 100644
index 0000000..355be0d
--- /dev/null
+++ b/DownKyi/Utils/WindowAdaptation.cs
@@ -0,0 +1,146 @@
+using System;
+using System.Drawing;
+using System.Windows;
+using System.Windows.Forms;
+using System.Windows.Interop;
+
+namespace DownKyi.Utils
+{
+ ///
+ /// 为窗口添加附加属性的辅助类
+ ///
+ /// https://www.cnblogs.com/kybs0/p/9834023.html
+ ///
+ public class WindowAdaptation
+ {
+ #region 窗口宽度比例
+ ///
+ /// 窗口宽度比例 单位:小数(0 - 1.0]
+ /// 窗口实际宽度=使用屏幕可显示区域(屏幕高度-任务栏高度)* 窗口宽度比例
+ ///
+ public static readonly DependencyProperty WidthByScreenRatioProperty = DependencyProperty.RegisterAttached(
+ "WidthByScreenRatio", typeof(double), typeof(WindowAdaptation), new PropertyMetadata(1.0, OnWidthByScreenRatioPropertyChanged));
+
+ private static void OnWidthByScreenRatioPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ if (d is Window window && e.NewValue is double widthByScreenRatio)
+ {
+ if (widthByScreenRatio <= 0 || widthByScreenRatio > 1)
+ {
+ throw new ArgumentException($"屏幕比例不支持{widthByScreenRatio}");
+ }
+
+ var screenDisplayArea = GetScreenSize(window);
+ var screenRatioWidth = screenDisplayArea.Width * widthByScreenRatio;
+
+ //if (!double.IsNaN(window.Width) && screenDisplayArea.Width > window.Width)
+ //{
+ // window.Width = window.Width;
+ //}
+ //else
+ //{
+ // window.Width = screenRatioWidth;
+ //}
+
+ if (!double.IsNaN(window.Width) && screenRatioWidth > window.Width)
+ {
+ window.Width = window.Width;
+ }
+ else if (!double.IsNaN(window.MinWidth) && screenRatioWidth < window.MinWidth)
+ {
+ window.Width = screenDisplayArea.Width;
+ }
+ else
+ {
+ window.Width = screenRatioWidth;
+ }
+ }
+ }
+
+ public static void SetWidthByScreenRatio(DependencyObject element, double value)
+ {
+ element.SetValue(WidthByScreenRatioProperty, value);
+ }
+
+ public static double GetWidthByScreenRatio(DependencyObject element)
+ {
+ return (double)element.GetValue(WidthByScreenRatioProperty);
+ }
+
+ #endregion
+
+ #region 窗口高度比例
+ ///
+ /// 窗口宽度比例 单位:小数(0 - 1.0]
+ /// 窗口实际宽度=使用屏幕可显示区域(屏幕高度-任务栏高度)* 窗口宽度比例
+ ///
+ public static readonly DependencyProperty HeightByScreenRatioProperty = DependencyProperty.RegisterAttached(
+ "HeightByScreenRatio", typeof(double), typeof(WindowAdaptation), new PropertyMetadata(1.0, OnHeightByScreenRatioPropertyChanged));
+
+ private static void OnHeightByScreenRatioPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ if (d is Window window && e.NewValue is double heightByScreenRatio)
+ {
+ if (heightByScreenRatio <= 0 || heightByScreenRatio > 1)
+ {
+ throw new ArgumentException($"屏幕比例不支持{heightByScreenRatio}");
+ }
+
+ var screenDisplayArea = GetScreenSize(window);
+ var screenRatioHeight = screenDisplayArea.Height * heightByScreenRatio;
+
+ if (!double.IsNaN(window.Height) && screenDisplayArea.Height > window.Height)
+ {
+ window.Height = window.Height;
+ }
+ else
+ {
+ window.Height = screenRatioHeight;
+ }
+
+ //if (!double.IsNaN(window.Height) && screenRatioHeight > window.Height)
+ //{
+ // window.Height = window.Height;
+ //}
+ //else if (!double.IsNaN(window.MinHeight) && screenRatioHeight < window.MinHeight)
+ //{
+ // window.Height = screenDisplayArea.Height;
+ //}
+ //else
+ //{
+ // window.Height = screenRatioHeight;
+ //}
+ }
+ }
+
+ public static void SetHeightByScreenRatio(DependencyObject element, double value)
+ {
+ element.SetValue(HeightByScreenRatioProperty, value);
+ }
+
+ public static double GetHeightByScreenRatio(DependencyObject element)
+ {
+ return (double)element.GetValue(HeightByScreenRatioProperty);
+ }
+
+ #endregion
+
+ const int DpiPercent = 96;
+ private static dynamic GetScreenSize(Window window)
+ {
+ var intPtr = new WindowInteropHelper(window).Handle;//获取当前窗口的句柄
+ var screen = Screen.FromHandle(intPtr);//获取当前屏幕
+ using (Graphics currentGraphics = Graphics.FromHwnd(intPtr))
+ {
+ //分别获取当前屏幕X/Y方向的DPI
+ double dpiXRatio = currentGraphics.DpiX / DpiPercent;
+ double dpiYRatio = currentGraphics.DpiY / DpiPercent;
+
+ var width = screen.WorkingArea.Width / dpiXRatio;
+ var height = screen.WorkingArea.Height / dpiYRatio;
+
+ return new { Width = width, Height = height };
+ }
+ }
+ }
+}
diff --git a/DownKyi/Views/MainWindow.xaml b/DownKyi/Views/MainWindow.xaml
index 8abb849..4316099 100644
--- a/DownKyi/Views/MainWindow.xaml
+++ b/DownKyi/Views/MainWindow.xaml
@@ -3,12 +3,15 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
+ xmlns:local="clr-namespace:DownKyi.Utils"
xmlns:prism="http://prismlibrary.com/"
Title="{DynamicResource AppName}"
Width="1100"
Height="750"
- MinWidth="900"
- MinHeight="600"
+ MinWidth="800"
+ MinHeight="550"
+ local:WindowAdaptation.HeightByScreenRatio="0.75"
+ local:WindowAdaptation.WidthByScreenRatio="0.8"
prism:ViewModelLocator.AutoWireViewModel="True"
ResizeMode="CanResize"
WindowStartupLocation="CenterScreen"
@@ -200,4 +203,5 @@
+