Question

I am modifying a piece of existing code for my company. We currently have our own custom form which stores its size in the configuration files so that, when the user returns, the window is the same size as it was before. The catch I'm noticing is that, after testing with higher DPI settings, the size being stored is the inflated one from the minimum size necessary at that DPI setting. To give an example, I have a login form which is 374 x 243 at 96x96 DPI. When I load the application at 150%, the form expands to 695x467, a little less than double the dimensions in either direction. If I change my computer back to 100% on the DPI, the login form stays at 695x467, a much larger display at that DPI.

The code we are using to store the size is as follows:

        AppConfig.GetAppConfig().SetFormAttrValue(Name, "LocationX", Location.X);
        AppConfig.GetAppConfig().SetFormAttrValue(Name, "LocationY", Location.Y);

        AppConfig.GetAppConfig().SetFormAttrValue(Name, "Width", Size.Width);
        AppConfig.GetAppConfig().SetFormAttrValue(Name, "Height", Size.Height);

Is there a good property to read to get the non-scaled size of the form? My suspicion, based on the non-linear increase in size from 100% to 150% turning into more of 185% increase, is that this is going to be a tougher nut to crack than I'd like (I'd initially hoped to be able to do transformations based on the Graphics.DpiX and Graphics.DpiY objects), but I figured I would ask in case someone knew of an easy answer. Thank you.

Was it helpful?

Solution

The only answer i have for you is a bit of a workaround... if it were me, i would simply store the form's size, with a DPI header in order to get the right one...

to get the DPI from the screen in C#:

using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
    float dpiX = graphics.DpiX;
    float dpiY = graphics.DpiY;
}

with this information, you could save in your config file the size of the form based on the DPI... then load the proper size based on the reading of the DPI above. The name of your config value could be something like "Size_At_96_96", and then the value would be the form's size...

Sorry if i don't have any more answers for you, but what i found is that unless you work with WPF, what you are experiencing happens to everybody.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top