[英]Why does the icon and buttons on the popup look different sometimes?
I was testing popups with PowerShell when I noticed something weird occurring.当我注意到发生了一些奇怪的事情时,我正在使用 PowerShell 测试弹出窗口。
The icon on the left of the popup looked different compared to another method of showing it.与另一种显示方法相比,弹出窗口左侧的图标看起来不同。 Also, the button styles were different.
此外,按钮样式也不同。
These were both run on the same system (Windows 10 Pro), and the code is exactly the same (shown below).它们都在同一个系统(Windows 10 Pro)上运行,并且代码完全相同(如下所示)。 Both were run on PowerShell 5.1.
两者都在 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. Old Icon and Buttons 1. 旧图标和按钮
The old icon and buttons were created using the typical PowerShell application.旧图标和按钮是使用典型的 PowerShell 应用程序创建的。 I saved the file as a
.ps1
file and then ran it like usual.我将文件保存为
.ps1
文件,然后像往常一样运行它。
$ ./test.ps1
However, it gave me this popup.但是,它给了我这个弹出窗口。 It's normal, but it looks more like a Windows 7 popup than Windows 10.
这很正常,但它看起来更像是 Windows 7 的弹出窗口,而不是 Windows 10。
2. New Icon and Buttons 2.新图标和按钮
The new icon and buttons were created using the Windows PowerShell ISE.新图标和按钮是使用 Windows PowerShell ISE 创建的。 It was run without saving the file (just clicked the Run button to execute it).
它在没有保存文件的情况下运行(只需单击“运行”按钮即可执行它)。
This gave me a popup I would expect to see on Windows 10.这给了我一个我希望在 Windows 10 上看到的弹出窗口。
So, is this behaviour expected?那么,这种行为是预期的吗? Also, why is it happening?
另外,为什么会这样?
Solution解决方案
You need to add the following line to your code before the popup.您需要在弹出窗口之前将以下行添加到您的代码中。
[System.Windows.Forms.Application]::EnableVisualStyles()
This will give the popup the new, modern feel to it.这将使弹出窗口具有新的现代感。
Why?为什么?
The reason that this is occurring is because when running this in the PowerShell ISE, it automatically runs it with the EnableVisualStyles
option.发生这种情况的原因是,在 PowerShell ISE 中运行它时,它会使用
EnableVisualStyles
选项自动运行它。
However, when the code is run in PowerShell, it doesn't add the EnableVisualStyles
part unless you specify so.但是,当代码在 PowerShell 中运行时,除非您指定,否则它不会添加
EnableVisualStyles
部分。
This is why the popup looks "older" in PowerShell, but more modern in the PowerShell ISE.这就是为什么弹出窗口在 PowerShell 中看起来“旧”,但在 PowerShell ISE 中更现代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.