[英]Why does the same program look different when run in Windows and Linux?
[英]Why does the icon and buttons on the popup look different sometimes?
当我注意到发生了一些奇怪的事情时,我正在使用 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 创建的。 它在没有保存文件的情况下运行(只需单击“运行”按钮即可执行它)。
这给了我一个我希望在 Windows 10 上看到的弹出窗口。
那么,这种行为是预期的吗? 另外,为什么会这样?
解决方案
您需要在弹出窗口之前将以下行添加到您的代码中。
[System.Windows.Forms.Application]::EnableVisualStyles()
这将使弹出窗口具有新的现代感。
为什么?
发生这种情况的原因是,在 PowerShell ISE 中运行它时,它会使用EnableVisualStyles
选项自动运行它。
但是,当代码在 PowerShell 中运行时,除非您指定,否则它不会添加EnableVisualStyles
部分。
这就是为什么弹出窗口在 PowerShell 中看起来“旧”,但在 PowerShell ISE 中更现代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.