Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect scaling behaviour if Windows settings changed #899

Open
kmcnaught opened this issue May 25, 2023 · 0 comments
Open

Incorrect scaling behaviour if Windows settings changed #899

kmcnaught opened this issue May 25, 2023 · 0 comments

Comments

@kmcnaught
Copy link
Collaborator

If you change:

  • what multi-monitor setup you have
  • which monitor is primary (in a 2 monitor setup), or
  • what UI scaling you have in the Windows settings

then Optikey has various incorrect behaviours until you have restarted your computer.

This is documented here: https://github.com/OptiKey/OptiKey/wiki/Multiple-monitors

but I raise it as an issue in case anyone has any suggestions on how to fix, and also as a future reminder to myself not to test anything without a full restart, and to document some of my findings.

The issue is that Graphics.DpiX and Graphics.DpiY are fundamentally incorrect until a restart (i.e. they keep the previous value). These are currently computed with:


    var desktop = g.GetHdc();
    DpiX = PInvoke.GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSX);
    DpiY = PInvoke.GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSY);

I have also tried


    var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
    var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);

    var dpiX = (int)dpiXProperty.GetValue(null, null);
    var dpiY = (int)dpiYProperty.GetValue(null, null);
    

and, when I have access to the mainWindow:

    PresentationSource source = PresentationSource.FromVisual(mainWindow);
    double dpiX, dpiY;
    if (source != null)
    {
        dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11;
        dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22;

        Log.Info($"MainWindow FromVisual: dpiX={dpiX}, dpiY={dpiY}");
    }

All three methods exhibit the same incorrect behaviour. I have not been able to find any fix with my googlings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant