WPF利用Canvas实现任意分辨率的全屏缩放

再界面绘制的时候,可以通过Canvas实现内容缩放,以此也可以用来实现分辨率的自适应。

假设我们的界面是这样的:
WPF利用Canvas实现任意分辨率的全屏缩放_第1张图片
其实也就是随便画的一个曲线,代码在下面:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="900" Width="1440"
        Loaded="Window_Loaded">
    <Grid>
        <Canvas Name="cav" RenderTransformOrigin="0,0">
            <Canvas.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="1" ScaleY="1"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                TransformGroup>
            Canvas.RenderTransform>
            <Path Data="M437.17647,410.64706 C575.89979,374.14092 642.63266,401.74701 726.58824,590.64706 757.73376,660.7245 820.68723,705.9505 691.29412,770.64706 615.58851,808.49986 529.14739,823.33078 447.76471,710.64706 381.62376,619.06729 458.52657,530.64931 730.11765,534.17647 879.98564,536.12281 996.19216,507.58998 1086.5882,668.29412" Fill="#FFF4F4F5" HorizontalAlignment="Left"  Height="250" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="422.112" Canvas.Left="999.888" Canvas.Top="609"/>
        Canvas>
    Grid>
Window>

显然我们的分辨率是1440*900的,刚好我电脑副屏的大小。

利用之前的全屏函数,可以实现全屏:

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Normal;//还原窗口(非最小化和最大化)
            this.WindowStyle = System.Windows.WindowStyle.None; //仅工作区可见,不显示标题栏和边框
            this.ResizeMode = System.Windows.ResizeMode.NoResize;//不显示最大化和最小化按钮
            this.Topmost = true;    //窗口在最前

            this.Left = 0.0;
            this.Top = 0.0;
            this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
        }

但是多余分辨率的地方是空的:
WPF利用Canvas实现任意分辨率的全屏缩放_第2张图片
而我们想要的是下面这种结果,比例放大:
WPF利用Canvas实现任意分辨率的全屏缩放_第3张图片
只需要再添加两句话:

            ScaleTransform st = new ScaleTransform(this.Width/1440,this.Height/900,0,0);
            cav.RenderTransform = st;

窗体加载时执行代码如下:

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Normal;//还原窗口(非最小化和最大化)
            this.WindowStyle = System.Windows.WindowStyle.None; //仅工作区可见,不显示标题栏和边框
            this.ResizeMode = System.Windows.ResizeMode.NoResize;//不显示最大化和最小化按钮
            this.Topmost = true;    //窗口在最前

            this.Left = 0.0;
            this.Top = 0.0;
            this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;

            ScaleTransform st = new ScaleTransform(this.Width/1440,this.Height/900,0,0);
            cav.RenderTransform = st;
        }

即创建一个缩放变换,起点为(0,0),宽度放大Width/1440倍,高度放大Height/900倍。

你可能感兴趣的:(C#,分辨率自适应,任意缩放)