简体   繁体   English

根据 DPI 调整 WPF 窗口大小

[英]Adjust WPF window size based on DPI

I am working on a WPF application with window size of 1200*932 including the title bar.我正在开发一个窗口大小为 1200*932 的 WPF 应用程序,包括标题栏。

It displays well on my desktop monitor (2560*1440 with 125% scaling), the actual size is 1500*1165.它在我的桌面显示器上显示良好(2560*1440,缩放比例为 125%),实际大小为 1500*1165。

However, when I run it on my Laptop (2736*1824 with 200% scaling), the actual size is 2400*1864, which is too large.但是,当我在笔记本电脑(2736*1824,缩放比例为 200%)上运行它时,实际大小为 2400*1864,这太大了。

How to handle this?如何处理? I mean dynamically?我的意思是动态?

Edit:编辑:

The original question may be not clear.最初的问题可能不清楚。 Sorry for that.对不起。

What I want to make is like how the UWP application works.我想做的就像 UWP 应用程序的工作方式。 On my desktop, the UWP application default window size is 1500*1165 (maybe 1200*932 *125%);在我的桌面上,UWP应用默认窗口大小是1500*1165(可能是1200*932*125%); While on laptop, the window size is 2048*1600 (maybe 1024*800 *200%).在笔记本电脑上,窗口大小为 2048*1600(可能为 1024*800 *200%)。

Is that feature provided by UWP by default? UWP 是否默认提供该功能? Can WPF do this? WPF可以这样做吗? Maybe programmatically, I think.也许以编程方式,我想。

Why not UWP: I find that UWP is working inside kind of a sandbox.为什么不使用 UWP:我发现 UWP 在某种沙箱中运行。 There are some restrictions on Network, which is important for my application. Network 有一些限制,这对我的应用程序很重要。

Finally I know what I want.我终于知道我想要什么了。 And the desciption in the question is not clear :(问题中的描述不清楚:(

I want the application size(window size as well as the content size) adjust according to the screen resolution and scaling so that my application will occupy (almost) the same partition of the screen the first time lunch.我希望应用程序大小(窗口大小以及内容大小)根据屏幕分辨率和缩放比例进行调整,以便我的应用程序在第一次午餐时占据(几乎)相同的屏幕分区。 Never become too large and get outside of screen nor too small to see clear.永远不要变得太大而超出屏幕,也不要太小而看不清。

So I make my window Height and Width Binding to所以我让我的窗口HeightWidth绑定到

    public double ReSizeWidth
    {
        get
        {
            return ((float)SystemParameters.VirtualScreenWidth) * (1500f / 2560f); 
            // 1500f / 2560f is the percentage
        }
    }

    public double ReSizeHeight
    {
        get
        {
            return ReSizeWidth * 0.75f + 32;
            // keep the window aspect ratio
        }
    }

And add this to the most outside Grid并将其添加到最外面的Grid

        <Grid.LayoutTransform>
            <ScaleTransform CenterX="0" 
                            CenterY="0" 
                            ScaleX="{Binding ElementName=myMainWindow, Path=ScaleValue}" 
                            ScaleY="{Binding ElementName=myXMainWindow, Path=ScaleValue}"/>
        </Grid.LayoutTransform>

Code behind背后的代码

    public double ScaleValue
    {
        get
        {
            return ReSizeWidth / 1200;
            // 1200 is the design height
        }
    }

Although it works, but I think it may cause some bad effects like blur or aliasing.虽然它有效,但我认为它可能会导致一些不好的效果,比如模糊或混叠。 I think there should be a built-in function or something to fulfill this.我认为应该有一个内置函数或其他东西来实现这一点。

If you want to change size of window of your app just use this link : WPF window size and this wpf-windows-overview#window-size如果您想更改应用程序窗口的大小,只需使用此链接: WPF 窗口大小和此wpf-windows-overview#window-size

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
SizeToContent="WidthAndHeight">

but i think you are looking for DPi integration in WPF app checkout his link for more info但我认为您正在 WPF 应用程序中寻找 DPi 集成,请查看他的链接以获取更多信息

https://docs.microsoft.com/en-us/windows/desktop/hidpi/declaring-managed-apps-dpi-aware https://docs.microsoft.com/en-us/windows/desktop/hidpi/declaring-managed-apps-dpi-aware

tecniques-scaling-wpf-application 技术-缩放-wpf-应用程序

What you are misunderstanding is the unit you are working with.你误解的是你正在工作的单位。 when setting the width of a container you wont tell him to be for example exactly 500 pixel in its width.在设置容器的宽度时,您不会告诉他例如宽度正好是 500 像素。 That makes total sense when you start thinking about fonts.当您开始考虑字体时,这完全有道理。 Fontsize 25 would be good on your display but an a 4 k display it might be minimalistic small and unreadable. Fontsize 25 在你的显示器上会很好,但 4 k 显示器可能是简约的小和不可读的。 To work around that the standard unit you are setting your container to is 1/96th of an inch.为了解决这个问题,您将容器设置为 1/96 英寸的标准单位。 You can modify it as seen below in the quote from:您可以修改它,如下所示:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.width?view=netframework-4.7.2 https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.width?view=netframework-4.7.2

qualifiedDouble A double value as described above, followed by one of the following unit declaration strings: px, in, cm, pt. qualifiedDouble 如上所述的双精度值,后跟以下单元声明字符串之一:px、in、cm、pt。

px (default) is device-independent units (1/96th inch per unit) px(默认)是与设备无关的单位(每单位 1/96 英寸)

in is inches; in 是英寸; 1in==96px 1in==96px

cm is centimeters; cm是厘米; 1cm==(96/2.54) px 1cm==(96/2.54) 像素

pt is points; pt 是点数; 1pt==(96/72) px 1pt==(96/72) 像素

Auto Enables autosizing behavior. Auto 启用自动调整大小行为。 See Remarks.见备注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM