为什么弹出窗口上的图标和按钮有时看起来不一样?

问题描述 投票:0回答:2

我正在使用 PowerShell 测试弹出窗口,这时我注意到发生了一些奇怪的事情。

弹出窗口左侧的图标与其他显示方法相比看起来有所不同。另外,按钮样式也不同。

它们都在同一系统(Windows 10 Pro)上运行,并且代码完全相同(如下所示)。两者都在 PowerShell 5.1 上运行。

Add-Type -AssemblyName PresentationFramework

$result = [System.Windows.MessageBox]::Show("Would you like to continue?", "Continue?", "YesNo", "Information", "No")
Write-Host $result

1。旧图标和按钮

旧图标和按钮是使用典型的 PowerShell 应用程序创建的。我将文件保存为

.ps1
文件,然后像往常一样运行它。

$ ./test.ps1

但是,它给了我这个弹出窗口。这很正常,但它看起来更像是 Windows 7 弹出窗口,而不是 Windows 10。


2。新图标和按钮

新图标和按钮是使用 Windows PowerShell ISE 创建的。它在不保存文件的情况下运行(只需单击 Run 按钮即可执行)。

这给了我一个我希望在 Windows 10 上看到的弹出窗口。


那么,这种行为是预期的吗?还有,为什么会这样?

windows powershell popup
2个回答
3
投票

解决方案

您需要在弹出窗口之前将以下行添加到代码中。

[System.Windows.Forms.Application]::EnableVisualStyles()

这将为弹出窗口带来新的、现代的感觉。


为什么?

发生这种情况的原因是因为在 PowerShell ISE 中运行它时,它会自动使用

EnableVisualStyles
选项运行它。

但是,当代码在 PowerShell 中运行时,除非您指定,否则它不会添加

EnableVisualStyles
部分。

这就是为什么弹出窗口在 PowerShell 中看起来“较旧”,但在 PowerShell ISE 中看起来更现代。


0
投票

EnableVisualStyles是不够的。您需要在 EnableVisualStyles 之前调用 SetProcessDPIAware

Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
public class ProcessDpi
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool SetProcessDPIAware();
}
'@
$null = [ProcessDpi]::SetProcessDPIAware()

[System.Windows.Forms.Application]::EnableVisualStyles()

从这里复制

© www.soinside.com 2019 - 2024. All rights reserved.