【发布时间】:2021-12-15 13:16:41
【问题描述】:
启用屏幕缩放后,我想从我的代码中获取正确的数据。所以我使用SetProcessDPIAware()函数:
using namespace System.Windows.Forms
Clear-Host
Add-Type -AssemblyName System.Windows.Forms
Add-Type -Name User32 -Namespace W32 '
[DllImport("user32.dll")]
public static extern bool SetProcessDPIAware();'
[Cursor]::Position | Write-Host
[Screen]::PrimaryScreen.Bounds.Size | Write-Host
[SystemInformation]::PrimaryMonitorSize | Write-Host
[W32.User32]::SetProcessDPIAware()
[Cursor]::Position | Write-Host
[Screen]::PrimaryScreen.Bounds.Size | Write-Host
[SystemInformation]::PrimaryMonitorSize | Write-Host
输出:
{X=630,Y=313} # ❌ incorrect data
{Width=2048, Height=864} # ❌ incorrect data
{Width=2048, Height=864} # ❌ incorrect data
True
{X=788,Y=391} # ✔️ data became correct
{Width=2048, Height=864} # ❌ still incorrect data
{Width=2560, Height=1080} # ✔️ data became correct
与c#代码相同的情况:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class Class1
{
[DllImport("user32.dll")]
static extern bool SetProcessDPIAware();
public static void Main()
{
Console.WriteLine("\n");
Console.WriteLine(Cursor.Position);
Console.WriteLine(Screen.PrimaryScreen.Bounds.Size);
Console.WriteLine(SystemInformation.PrimaryMonitorSize);
Console.WriteLine(SetProcessDPIAware());
Console.WriteLine(Cursor.Position);
Console.WriteLine(Screen.PrimaryScreen.Bounds.Size);
Console.WriteLine(SystemInformation.PrimaryMonitorSize);
}
}
那么这是Screen.PrimaryScreen.Bounds 的错误还是我必须做其他事情才能从该属性中获取正确的数据?
如果唯一的方法是使用SystemInformation 类,那么我如何获取关于第二台监视器的数据,而不仅仅是关于主监视器的数据?
或 有没有办法为 powershell.exe / powershell_ise.exe 设置this 设置?
【问题讨论】:
-
如果您需要每台显示器的 DPI 感知,将 DPI 感知设置为
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE不会解决问题。
标签: c# .net powershell winapi