3

我试过在互联网上寻找,但找不到任何东西。

我已经构建了一个 powershell GUI 应用程序,但想添加一个带有不同菜单项的菜单栏。

这可能吗?有没有人做过(有例子)?

谢谢

4

1 回答 1

8

这是使用 Windows 窗体的示例。我使用 Visual Studio 对其进行设计,然后使用Convert-Form 将其转换为 PowerSell,这是一个由 Laurent Dardenne 从漂亮的法国 PowerShell 站点PowerShelll-Scripting.com编写的免费工具

# Load external assemblies
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$MS_Main = new-object System.Windows.Forms.MenuStrip
$fileToolStripMenuItem = new-object System.Windows.Forms.ToolStripMenuItem
$openToolStripMenuItem = new-object System.Windows.Forms.ToolStripMenuItem
$editionToolStripMenuItem = new-object System.Windows.Forms.ToolStripMenuItem
#
# MS_Main
#
$MS_Main.Items.AddRange(@(
$fileToolStripMenuItem,
$editionToolStripMenuItem))
$MS_Main.Location = new-object System.Drawing.Point(0, 0)
$MS_Main.Name = "MS_Main"
$MS_Main.Size = new-object System.Drawing.Size(354, 24)
$MS_Main.TabIndex = 0
$MS_Main.Text = "menuStrip1"
#
# fileToolStripMenuItem
#
$fileToolStripMenuItem.DropDownItems.AddRange(@(
$openToolStripMenuItem))
$fileToolStripMenuItem.Name = "fileToolStripMenuItem"
$fileToolStripMenuItem.Size = new-object System.Drawing.Size(35, 20)
$fileToolStripMenuItem.Text = "&File"
#
# openToolStripMenuItem
#
$openToolStripMenuItem.Name = "openToolStripMenuItem"
$openToolStripMenuItem.Size = new-object System.Drawing.Size(152, 22)
$openToolStripMenuItem.Text = "&Open"
function OnClick_openToolStripMenuItem($Sender,$e){
    [void][System.Windows.Forms.MessageBox]::Show("Event openToolStripMenuItem.Add_Click is not implemented.")
}

$openToolStripMenuItem.Add_Click( { OnClick_openToolStripMenuItem $openToolStripMenuItem $EventArgs} )
#
# editionToolStripMenuItem
#
$editionToolStripMenuItem.Name = "editionToolStripMenuItem"
$editionToolStripMenuItem.Size = new-object System.Drawing.Size(51, 20)
$editionToolStripMenuItem.Text = "&Edition"
#
$MenuForm = new-object System.Windows.Forms.form
#
$MenuForm.ClientSize = new-object System.Drawing.Size(354, 141)
$MenuForm.Controls.Add($MS_Main)
$MenuForm.MainMenuStrip = $MS_Main
$MenuForm.Name = "MenuForm"
$MenuForm.Text = "I\'ve got a menu"
function OnFormClosing_MenuForm($Sender,$e){ 
    # $this represent sender (object)
    # $_ represent  e (eventarg)

    # Allow closing
    ($_).Cancel= $False
}
$MenuForm.Add_FormClosing( { OnFormClosing_MenuForm $MenuForm $EventArgs} )
$MenuForm.Add_Shown({$MenuForm.Activate()})
$MenuForm.ShowDialog()
#Free ressources
$MenuForm.Dispose()
于 2012-05-03T03:39:51.327 回答