PowerShell 调用新的 .NET 表单
PowerShell Calling a new .NET form
我正在尝试让最终用户可以使用我的脚本。为此,他们需要能够单击一些按钮来启动我们拥有的一些脚本。理想的情况是显示不同类别的 .NET 表单,并在选择类别后向您显示正确的操作按钮。
我遇到的问题在#Call Sub menu 1部分,之前的窗体没有关闭并且保持可见(当你把窗体移动到顶部时你可以看到这个离开)。为什么 $MenuBox.Close()
无法摆脱初始窗口?
有没有更好的制作菜单的方法呢?还是每次需要查看新窗口时都必须创建一个新的 Form
?在稍后阶段,我需要添加一个 后退 按钮以用于导航目的。
代码:
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$WindowTitle = 'Script center'
$Title = 'Welcome to Script Center'
# Main menu
$MenuBox = New-Object System.Windows.Forms.Form
$MenuBox.Size = New-Object System.Drawing.Size @(650,450)
$MenuBox.Text = $WindowTitle
$MenuBox.StartPosition = "CenterScreen"
$MenuBox.AutoSize = $False
$MenuBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$MenuBox.Icon = $Icon
# AD menu
$ADBox = New-Object System.Windows.Forms.Form
$ADBox.Size = New-Object System.Drawing.Size @(650,450)
$ADBox.Text = $WindowTitle
$ADBox.StartPosition = "CenterScreen"
$ADBox.AutoSize = $False
$ADBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$ADBox.Icon = $Icon
# Main menu button 2
$ADButton1 = New-Object System.Windows.Forms.Button
$ADButton1.Location = New-Object System.Drawing.Size(62,160)
$ADButton1.Size = New-Object System.Drawing.Size(500,30)
$ADButton1.Add_Click({
$global:ButtonResult = 'Result: AD Button 1'
$MenuBox.Close()
})
$ADButton1.Text = 'AD Button 1'
$ADButton1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$ADBox.Controls.Add($ADButton1)
# Main menu Header Text
$MenuHeader = New-Object System.Windows.Forms.Label
$MenuHeader.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",14,1,3,1)
$MenuHeader.Location = New-Object System.Drawing.Size(118,20)
$MenuHeader.Size = New-Object System.Drawing.Size(380,40)
$MenuHeader.Text = $Title
$MenuHeader.TextAlign = [System.Drawing.ContentAlignment]::TopCenter
$MenuBox.Controls.Add($MenuHeader)
# Main menu
$BoxLabel = New-Object System.Windows.Forms.Label
$BoxLabel.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",10,0,3,1)
$BoxLabel.Location = New-Object System.Drawing.Size(10,60)
$BoxLabel.Size = New-Object System.Drawing.Size(680,20)
$BoxLabel.Text = 'Select the category:'
$MenuBox.Controls.Add($BoxLabel)
# Main menu button 1
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(62,100)
$Button2.Size = New-Object System.Drawing.Size(500,30)
$Button2.Add_Click({
# Call Sub menu 1
$MenuBox.Close()
$ADBox.Topmost = $True
$ADBox.Add_Shown({$ADBox.Activate()})
[void] $ADBox.ShowDialog()
})
$Button2.Text = 'Active directory'
$Button2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$MenuBox.Controls.Add($Button2)
# Main menu button 2
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(62,160)
$Button1.Size = New-Object System.Drawing.Size(500,30)
$Button1.Add_Click({
$global:ButtonResult = 'Result: Button 2'
$MenuBox.Close()
})
$Button1.Text = 'Files and Folders'
$Button1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$MenuBox.Controls.Add($Button1)
# Exit Button
$ExitButton = New-Object System.Windows.Forms.Button
$ExitButton.Location = New-Object System.Drawing.Size(540,370)
$ExitButton.Size = New-Object System.Drawing.Size(75,23)
$ExitButton.Text = 'Exit'
$ExitButton.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1)
$ExitButton.Add_Click({
Remove-Item "$WorkingDirectory\Temp\*.*" -Force
$MenuBox.Close()
})
$MenuBox.Controls.Add($ExitButton)
# Show Menu
$MenuBox.Topmost = $True
$MenuBox.Add_Shown({$MenuBox.Activate()})
[void] $MenuBox.ShowDialog()
感谢您的帮助。
您可以在调用 Close() 后使用 Dispose() 方法强制关闭 $MenuBox
表单:
$Button2.Add_Click({
# Call Sub menu 1
$MenuBox.Close()
$MenuBox.Dispose()
$ADBox.Topmost = $True
$ADBox.Add_Shown({$ADBox.Activate()})
[void] $ADBox.ShowDialog()
})
我正在尝试让最终用户可以使用我的脚本。为此,他们需要能够单击一些按钮来启动我们拥有的一些脚本。理想的情况是显示不同类别的 .NET 表单,并在选择类别后向您显示正确的操作按钮。
我遇到的问题在#Call Sub menu 1部分,之前的窗体没有关闭并且保持可见(当你把窗体移动到顶部时你可以看到这个离开)。为什么 $MenuBox.Close()
无法摆脱初始窗口?
有没有更好的制作菜单的方法呢?还是每次需要查看新窗口时都必须创建一个新的 Form
?在稍后阶段,我需要添加一个 后退 按钮以用于导航目的。
代码:
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$WindowTitle = 'Script center'
$Title = 'Welcome to Script Center'
# Main menu
$MenuBox = New-Object System.Windows.Forms.Form
$MenuBox.Size = New-Object System.Drawing.Size @(650,450)
$MenuBox.Text = $WindowTitle
$MenuBox.StartPosition = "CenterScreen"
$MenuBox.AutoSize = $False
$MenuBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$MenuBox.Icon = $Icon
# AD menu
$ADBox = New-Object System.Windows.Forms.Form
$ADBox.Size = New-Object System.Drawing.Size @(650,450)
$ADBox.Text = $WindowTitle
$ADBox.StartPosition = "CenterScreen"
$ADBox.AutoSize = $False
$ADBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$ADBox.Icon = $Icon
# Main menu button 2
$ADButton1 = New-Object System.Windows.Forms.Button
$ADButton1.Location = New-Object System.Drawing.Size(62,160)
$ADButton1.Size = New-Object System.Drawing.Size(500,30)
$ADButton1.Add_Click({
$global:ButtonResult = 'Result: AD Button 1'
$MenuBox.Close()
})
$ADButton1.Text = 'AD Button 1'
$ADButton1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$ADBox.Controls.Add($ADButton1)
# Main menu Header Text
$MenuHeader = New-Object System.Windows.Forms.Label
$MenuHeader.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",14,1,3,1)
$MenuHeader.Location = New-Object System.Drawing.Size(118,20)
$MenuHeader.Size = New-Object System.Drawing.Size(380,40)
$MenuHeader.Text = $Title
$MenuHeader.TextAlign = [System.Drawing.ContentAlignment]::TopCenter
$MenuBox.Controls.Add($MenuHeader)
# Main menu
$BoxLabel = New-Object System.Windows.Forms.Label
$BoxLabel.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",10,0,3,1)
$BoxLabel.Location = New-Object System.Drawing.Size(10,60)
$BoxLabel.Size = New-Object System.Drawing.Size(680,20)
$BoxLabel.Text = 'Select the category:'
$MenuBox.Controls.Add($BoxLabel)
# Main menu button 1
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(62,100)
$Button2.Size = New-Object System.Drawing.Size(500,30)
$Button2.Add_Click({
# Call Sub menu 1
$MenuBox.Close()
$ADBox.Topmost = $True
$ADBox.Add_Shown({$ADBox.Activate()})
[void] $ADBox.ShowDialog()
})
$Button2.Text = 'Active directory'
$Button2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$MenuBox.Controls.Add($Button2)
# Main menu button 2
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(62,160)
$Button1.Size = New-Object System.Drawing.Size(500,30)
$Button1.Add_Click({
$global:ButtonResult = 'Result: Button 2'
$MenuBox.Close()
})
$Button1.Text = 'Files and Folders'
$Button1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$MenuBox.Controls.Add($Button1)
# Exit Button
$ExitButton = New-Object System.Windows.Forms.Button
$ExitButton.Location = New-Object System.Drawing.Size(540,370)
$ExitButton.Size = New-Object System.Drawing.Size(75,23)
$ExitButton.Text = 'Exit'
$ExitButton.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1)
$ExitButton.Add_Click({
Remove-Item "$WorkingDirectory\Temp\*.*" -Force
$MenuBox.Close()
})
$MenuBox.Controls.Add($ExitButton)
# Show Menu
$MenuBox.Topmost = $True
$MenuBox.Add_Shown({$MenuBox.Activate()})
[void] $MenuBox.ShowDialog()
感谢您的帮助。
您可以在调用 Close() 后使用 Dispose() 方法强制关闭 $MenuBox
表单:
$Button2.Add_Click({
# Call Sub menu 1
$MenuBox.Close()
$MenuBox.Dispose()
$ADBox.Topmost = $True
$ADBox.Add_Shown({$ADBox.Activate()})
[void] $ADBox.ShowDialog()
})