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

Dpi awareness support #5

Merged
merged 8 commits into from
Sep 23, 2021
Merged

Dpi awareness support #5

merged 8 commits into from
Sep 23, 2021

Conversation

FoxPride
Copy link
Contributor

No description provided.

andbayd added 4 commits May 23, 2021 06:39
- All values returns in wpf units
- Project update for StyleCop rules
- Bugfix for SystemInformation.VirtualScreen returning only primary screen
…hould set WindowStyle="None" ResizeMode="NoResize" properties for window or you will get position problem. See dotnet/wpf#4127)
WindowHelper added; Some fixes for SystemInformation;
@micdenny
Copy link
Owner

Did you checked if the framework as the dpi awareness in the new sources? From winforms or wpf new sources? Because last time I did this library if I remember well I simply took the sources from winforms source code that were release publicly, now that is also open sourced and they did new netcore version maybe they did something about that we can copy as well?

@FoxPride
Copy link
Contributor Author

Can't say about winforms but in WPF dpi awareness is working from NET Framework 4.6.0. I suppose winforms works too.
But the reason why I decided to use your library is that in Windows Forms Screen there is no way to get monitor dpi (you can only calculate dpi of primary). So in case I want to move window from one monitor to another (with different dpi scale) I will end up having wrong coordinates.

@micdenny
Copy link
Owner

@FoxPride what about create the DpiTest as net5 app?

Copy link
Owner

@micdenny micdenny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FoxPride what about create the DpiTest as net5 app?

I'm doing it apart, moving under test folder with a name like WpfScreenHelper.DpiTestWpfApp

src/WpfScreenHelper/Screen.cs Outdated Show resolved Hide resolved
@FoxPride
Copy link
Contributor Author

@FoxPride what about create the DpiTest as net5 app?

I'm doing it apart, moving under test folder with a name like WpfScreenHelper.DpiTestWpfApp

.NET 5 should work too. I can bump version and rename project if you want to.

@micdenny
Copy link
Owner

I'm doing a cleanup and will push it

@micdenny
Copy link
Owner

I'm making some test and I can't reproduce the problem.

first of all why you have to set this in the manifest:

  <asmv3:application>
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
      <!-- Per Monitor V1 [OS >= Windows 8.1] 
         Values: False, True, Per-monitor, True/PM -->
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
      <!-- Per Monitor V1 [OS >= Windows 10 Anniversary Update (1607, 10.0.14393, Redstone 1)]
         Values: Unaware, System, PerMonitor -->
      <!-- Per Monitor V2 [OS >= Windows 10 Creators Update (1703, 10.0.15063, Redstone 2)]
         Value: PerMonitorV2 -->
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
    </asmv3:windowsSettings>
  </asmv3:application>

when the default manifest template for a net5 wpf app says this:

Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need to opt in.

  <!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
       DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need 
       to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should 
       also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. 
       
       Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
  <!--
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    </windowsSettings>
  </application>
  -->

BTW I run your sample app and when I move the window to a scaled monitor, it doesn't move correctly. I have 3 monitor, on one I've set a 150% scale, I'm doing it right?

image

It seems to work only when I align to top-right, right and bottom-right, if you have the window on unscaled monitor and you click to center on the scaled monitor, it will end up in the middle of the two monitors. If I drag the window on the scaled monitor I saw that it changes the size when half the window is on the scaled window, then the buttons you add works as expected.

If I add the actual published v1.0.0 package in fact is getting worst, because when the window is dragged on the scaled monitor, the buttons works badly, so the changes are of course doing something good 😉

But it works fine with v1.0.0 if I leave the manifest unchanged as dpi-aware should be already in every WPF app, and it works better (also works the center, left-right and right button from another monitor to a scaled one), why?

I'm for sure ignorant on this topic, so I'm trying to understand 😄

@FoxPride
Copy link
Contributor Author

I'm making some test and I can't reproduce the problem.

first of all why you have to set this in the manifest:

  <asmv3:application>
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
      <!-- Per Monitor V1 [OS >= Windows 8.1] 
         Values: False, True, Per-monitor, True/PM -->
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
      <!-- Per Monitor V1 [OS >= Windows 10 Anniversary Update (1607, 10.0.14393, Redstone 1)]
         Values: Unaware, System, PerMonitor -->
      <!-- Per Monitor V2 [OS >= Windows 10 Creators Update (1703, 10.0.15063, Redstone 2)]
         Value: PerMonitorV2 -->
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
    </asmv3:windowsSettings>
  </asmv3:application>

when the default manifest template for a net5 wpf app says this:

Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need to opt in.

  <!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
       DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need 
       to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should 
       also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. 
       
       Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
  <!--
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    </windowsSettings>
  </application>
  -->

Looks like they changed smth for dpi scale in .NET 5 🤔. So it's better to stick to new manifest then.

BTW I run your sample app and when I move the window to a scaled monitor, it doesn't move correctly. I have 3 monitor, on one I've set a 150% scale, I'm doing it right?

image

I have the same configuration (150% and 100% monitors), works with 4.7.1 version. I'll test it for 5.0.0.

It seems to work only when I align to top-right, right and bottom-right, if you have the window on unscaled monitor and you click to center on the scaled monitor, it will end up in the middle of the two monitors. If I drag the window on the scaled monitor I saw that it changes the size when half the window is on the scaled window, then the buttons you add works as expected.

Oh I see. Looks like size of window ends up being bigger for second monitor and on first move can't detect dpi changed event. That's why it moves window to the center of two monitors.

If I add the actual published v1.0.0 package in fact is getting worst, because when the window is dragged on the scaled monitor, the buttons works badly, so the changes are of course doing something good 😉

But it works fine with v1.0.0 if I leave the manifest unchanged as dpi-aware should be already in every WPF app, and it works better (also works the center, left-right and right button from another monitor to a scaled one), why?

I'm for sure ignorant on this topic, so I'm trying to understand 😄

May be in .NET 5.0.0 we finally have right coordinates with scale calculation? If so that's a great news for future development (unfortunately not for older .NET Framework). I'll check it out!

@micdenny
Copy link
Owner

in the meanwhile I did some code review and pushed on your branch, take a look at it

@micdenny
Copy link
Owner

@FoxPride should we bump the version to a non-breaking change or to a breaking change one? 1.1.0 or 2.0.0?

@FoxPride
Copy link
Contributor Author

🤔I designed changes to be compatible with previous version. So it can be just 1.1.0 update as for me.

@FoxPride
Copy link
Contributor Author

in the meanwhile I did some code review and pushed on your branch, take a look at it

Thank you! I will have a look.

@FoxPride
Copy link
Contributor Author

@micdenny Finally tested .NET 5.0 project on several setups and it works just fine! Small note: on windows 10 commenting manifest section about dpi awareness changes nothing so it can be used for windows 7\8 setup.

@micdenny micdenny merged commit 6714ca8 into micdenny:master Sep 23, 2021
@FoxPride FoxPride deleted the dpi_awareness_support branch September 23, 2021 16:50
@micdenny
Copy link
Owner

published on nuget https://www.nuget.org/packages/WpfScreenHelper/1.1.0

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

Successfully merging this pull request may close these issues.

2 participants