0
votes

I am working on a WPF application with window size of 1200*932 including the title bar.

It displays well on my desktop monitor (2560*1440 with 125% scaling), the actual size is 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.

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. On my desktop, the UWP application default window size is 1500*1165 (maybe 1200*932 *125%); While on laptop, the window size is 2048*1600 (maybe 1024*800 *200%).

Is that feature provided by UWP by default? Can WPF do this? Maybe programmatically, I think.

Why not UWP: I find that UWP is working inside kind of a sandbox. There are some restrictions on Network, which is important for my application.

3
Now you question is even less clear.. at least for me i cant tell enaymore what you want do you want a frame always to be the exact same Actual pixel size or....?Denis Schaf

3 Answers

1
votes

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

    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.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.

0
votes

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

<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

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

tecniques-scaling-wpf-application

0
votes

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. 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. To work around that the standard unit you are setting your container to is 1/96th of an inch. 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

qualifiedDouble A double value as described above, followed by one of the following unit declaration strings: px, in, cm, pt.

px (default) is device-independent units (1/96th inch per unit)

in is inches; 1in==96px

cm is centimeters; 1cm==(96/2.54) px

pt is points; 1pt==(96/72) px

Auto Enables autosizing behavior. See Remarks.