zoukankan      html  css  js  c++  java
  • 窗体进行自动适应窗口

    调用窗体自定义适应的代码
    xmlns:wi="定义类位置"
    wi:WindowAdaptation.HeightByScreenRatio="0.75" MaxHeight="900" MinHeight="520"
    wi:WindowAdaptation.WidthByScreenRatio="0.8" MaxWidth="1440" MinWidth="800">

    自定义类:

    /// <summary>
    /// 为窗口<see cref="Window"/>添加附加属性的辅助类
    /// </summary>
    public class WindowAdaptation
    {
    #region 窗口宽度比例
    /// <summary>
    /// 窗口宽度比例 单位:小数(0 - 1.0]
    /// <para>窗口实际宽度=使用屏幕可显示区域(屏幕高度-任务栏高度)* 窗口宽度比例</para>
    /// </summary>
    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.MaxWidth) && screenRatioWidth > window.MaxWidth)
    {
    window.Width = window.MaxWidth;
    }
    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 窗口高度比例
    /// <summary>
    /// 窗口宽度比例 单位:小数(0 - 1.0]
    /// <para>窗口实际宽度=使用屏幕可显示区域(屏幕高度-任务栏高度)* 窗口宽度比例</para>
    /// </summary>
    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.MaxHeight) && screenRatioHeight > window.MaxHeight)
    {
    window.Height = window.MaxHeight;
    }
    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 };
    }
    }
    }

  • 相关阅读:
    perl 解json数组
    华为云3大体系化防护实践,保障金融业云上数据安全
    弹性文件服务解密 -- 块存储、文件存储、对象存储的区别
    【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程
    云+AI+5G时代,华为云已准备好多元化云服务架构
    高能街访 | 为什么他们都纷纷为深圳打Call?
    Angularjs进阶笔记(2)—自定义指令中的数据绑定
    Angularjs进阶笔记(1)—不同类型的双向数据绑定
    ServiceComb java-chassis和CSE java-chassis的区别
    使用inspector功能查看和管理契约
  • 原文地址:https://www.cnblogs.com/wlming/p/12058213.html
Copyright © 2011-2022 走看看