繁体   English   中英

Windows中是否有一种方法可以为具有DPI感知功能的前台应用程序获取正确的矩形?

[英]Is there a way in Windows that can get the correct rectangle for the foreground application with DPI awareness?

大家好我正在研究一个项目来跟踪用户正在处理的当前活动窗口的位置。 它适用于单显示器或具有统一DPI设置的显示器。 但它不适用于具有不同DPI /比例因子的监视器。

现象是,用WinForms编写的应用程序能够在与应用程序所在的屏幕相同的屏幕上获得正确的窗口矩形。 它无法为其他应用程序获取正确的。

我使用的方法是https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowrect以通过句柄获取矩形。

我以前构建的.NET Framework版本是4.7

我已经在这样的app.manifest中设置了DPI意识,并使用了

[DllImport("user32.dll"]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll"]
public static extern bool SetProcessDPIAware();
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Reactive.Core" publicKeyToken="94bc3704cddfc263" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.3000.0" newVersion="3.0.3000.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <appSettings>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
  </System.Windows.Forms.ApplicationConfigurationSection>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

这里我举一个例子。 假设只有两个显示器,第一个显示DPI 100%,第二个显示DPI 150%。 应用程序在显示器上具有100%DPI。 对于100%DPI监视器上的窗口,前景应用程序矩形上的坐标是正确的。

但是对于150%DPI上的窗口,它不会给我正确的,我看到的坐标看起来像应用程序计算100%的坐标,就像屏幕应用程序所使用的一样。

Windows 8.1中引入了“每个监视器DPI感知”,如果您的Windows版本低于8.1,则与此无关。

如果windows version> = 8,则根据文档<dpiAware>true</dpiAware>仅在应用程序中设置PROCESS_SYSTEM_DPI_AWARE的值。 要启用PROCESS_PER_MONITOR_DPI_AWARE<dpiAware>true/PM</dpiAware>

和其他参考: Windows窗体中的高DPI支持

暂无
暂无

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

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