html - 如何在 PowerShell 对话框中显示 HTML 页面?

标签 html winforms powershell

我想在 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/

相关文章:

javascript - 带有幻灯片阵列的 Photoswipe 画廊

html - 使用CSS使div与普通文本相同

c# - 在表单之间切换(关闭一个表单,然后打开另一个表单)

powershell - 使用 CSV 作为输入清理 AD

javascript - 灯箱使用CSS问题【附照片】

javascript - 如何让用户通过单击文本来增加字体大小?

c# - 如何在表格布局中合并两个单元格

c# - ADO.NET 从 datagrid1 中选择一行以显示 datagrid2 中的行

powershell - 删除 HTML 中的区域

arrays - 如何使这个 PowerShell 函数返回纯字符串,而不返回字符串数组?