Winforms PowerShell-支持DPI的表单

Winforms PowerShell-支持DPI的表单,winforms,powershell,Winforms,Powershell,大家好,我很好奇在PowerShell中创建winform DPI时,如何使其具有感知能力?或者至少,防止它自动缩放。我在谷歌上搜索了又搜索,但似乎找不到答案,也找不到如何做的例子。最常见的答案是将其包含在清单中,但对于PowerShell来说,这不是一个可行的选项 如果有什么问题的话,我只是想阻止Windows以高于96(100%)的DPI自动重新缩放我的表单。我尝试了AutoScaleMode=“DPI”,但不幸的是,这不起作用,而且似乎没有任何作用,因为将其设置为“无”或根本不包含它都是相

大家好,我很好奇在PowerShell中创建winform DPI时,如何使其具有感知能力?或者至少,防止它自动缩放。我在谷歌上搜索了又搜索,但似乎找不到答案,也找不到如何做的例子。最常见的答案是将其包含在清单中,但对于PowerShell来说,这不是一个可行的选项

如果有什么问题的话,我只是想阻止Windows以高于96(100%)的DPI自动重新缩放我的表单。我尝试了AutoScaleMode=“DPI”,但不幸的是,这不起作用,而且似乎没有任何作用,因为将其设置为“无”或根本不包含它都是相同的结果

一个简单的例子

Add-Type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.Drawing'

$Dialog = New-Object Windows.Forms.Form
$Dialog.Text = 'Main'
$Dialog.Size = New-Object Drawing.Size(200, 100)
$Dialog.AutoScaleMode = "DPI"

$Label = New-Object Windows.Forms.Label
$Label.Size = New-Object Drawing.Size(200, 16)
$Label.Location = New-Object Drawing.Size(10, 20)
$Label.Text = 'This text is to test autoscaling.'
$Dialog.Controls.Add($Label)

$Dialog.ShowDialog()
左侧的图像自动缩放且模糊。我不想要这个,我想要它看起来像正确的。如果我能做到这一点,我将尝试找出如何处理缩放


所以我偶然找到了一个解决办法。我在研究WPF表单(我对它知之甚少)时遇到了一个示例。在WPF中添加一个从未实际显示的虚拟窗口的最小值,以某种方式防止winforms自动扩展

# Load assemblies.
Add-Type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.Drawing'
Add-Type -AssemblyName 'PresentationFramework'

# Dummy WPF window (prevents auto scaling).
[xml]$Xaml = @"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window">
</Window>
"@
$Reader = (New-Object System.Xml.XmlNodeReader $Xaml)
$Window = [Windows.Markup.XamlReader]::Load($Reader)

# Business as usual.
$Dialog = New-Object Windows.Forms.Form
$Dialog.Text = 'Main Window'
$Dialog.Size = New-Object Drawing.Size(200, 100)

$Label = New-Object Windows.Forms.Label
$Label.Size = New-Object Drawing.Size(200, 16)
$Label.Location = New-Object Drawing.Size(10, 20)
$Label.Text = 'This text is to test autoscaling.'
$Dialog.Controls.Add($Label)

$Dialog.ShowDialog()
#加载程序集。
添加类型-AssemblyName“System.Windows.Forms”
添加类型-AssemblyName“System.Drawing”
添加类型-AssemblyName“PresentationFramework”
#虚拟WPF窗口(防止自动缩放)。
[xml]$Xaml=@”
"@
$Reader=(新对象System.Xml.XmlNodeReader$Xaml)
$Window=[Windows.Markup.XamlReader]::加载($Reader)
#一切照旧。
$Dialog=新建对象Windows.Forms.Form
$Dialog.Text='主窗口'
$Dialog.Size=新对象图形。大小(200100)
$Label=新对象Windows.Forms.Label
$Label.Size=新对象图形。大小(200,16)
$Label.Location=新对象图形。尺寸(10,20)
$Label.Text='此文本用于测试自动缩放。'
$Dialog.Controls.Add($Label)
$Dialog.ShowDialog()

您希望它完全不根据操作系统DPI缩放比例进行缩放,还是希望它在自动缩放时不模糊?老实说,我想知道如何做到这两个方面,以便我可以尝试一下。如果我能让DPI缩放正常工作,那么我会尝试一下。如果没有,那么我可以手动尝试根据DPI调整所有缩放。请检查此GitHub(测试)项目:。它处理
PreferExternalManifest
注册表项以加载激活/停用
DpiAware
选项的清单。