我想在 PowerShell 对话框中显示一个简单的 HTML 页面。
这是使用 dialog.ps1
构建对话框的方法:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
[void] $objForm.ShowDialog()
在这个窗口中我想显示一个像index.html
这样的网页:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title</title>
</head>
<body>
Hello world!
</body>
</html>
当然,网页还有更多的元素,比如带有图片 map 的图片。
如果这也适用于 CMD,我会更喜欢这个选项。
最佳答案
以下代码片段 - 为了方便起见,使用 PSv5+[1] 语法 - 演示了 WebBrowser
control 的使用在 WinForms 对话框中显示 HTML 文本:
# PSv5+:
# Import namespaces so that types can be referred by
# their mere name (e.g., `Form` rather than `System.Windows.Forms.Form`)
#
using namespace System.Windows.Forms
using namespace System.Drawing
# Load the WinForms assembly.
Add-Type -AssemblyName System.Windows.Forms
# Create a form.
$form = [Form] @{
ClientSize = [Point]::new(400, 400)
Text = "WebBrowser-Control Demo"
}
# Create a web-browser control, make it as large as the inside of the form,
# and assign the HTML text.
$sb = [WebBrowser] @{
ClientSize = $form.ClientSize
DocumentText = @'
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title</title>
</head>
<body>
Hello world!
</body>
</html>
'@
}
# Add the web-browser control to the form...
$form.Controls.Add($sb)
# ... and display the form as a dialog (synchronously).
$form.ShowDialog()
# Clean up.
$form.Dispose()
[1] 该代码也适用于 PowerShell [Core] v7+,但不适用于 PowerShell Core v6.x,因为后者根本不支持WinForms(和 WPF)。
关于html - 如何在 PowerShell 对话框中显示 HTML 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58475901/