【发布时间】:2015-01-07 15:44:02
【问题描述】:
当创建一个表单并希望通过单击该表单上的按钮进行导航时,是否需要创建一个全新的表单才能打开下一页?或者当前表单是否可以通过其他按钮以某种方式重新初始化?
单击“Active Directory”按钮时,您可以看到表单正在重新加载或重新绘制自身。由于我对此并没有真正的经验,并且很难找到有关表单内导航的信息,如果有人能告诉我这是否是正确执行此操作的方法,那就太好了?
因为每次进入菜单时,您都需要创建一个全新的表单,以便为错误留出空间,因为您想要的东西(例如“无法最大化”、图标、标题栏、退出按钮、后退按钮、. ..') 在不同的窗口中都相同。
对于第二个窗口,“Active Directory”,我尝试了StartPosition = 'CenterParent' 之类的方法,但这不起作用,因为它出现在一个完全不同的位置,然后它的父级已关闭。
感谢您的帮助。
代码:
[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'
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$Image = [system.drawing.image]::FromFile("S:\Prod\Script center\Background.jpg")
# 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.MaximizeBox = $False
$MenuBox.AutoSize = $False
$MenuBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$MenuBox.Icon = $Icon
$MenuBox.BackgroundImage = $Image
$MenuBox.BackgroundImageLayout = 'None' # None, Tile, Center, Stretch, Zoom
# 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()
})
# 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.BackColor = 'Transparent'
$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:'
$BoxLabel.BackColor = 'Transparent'
$MenuBox.Controls.Add($BoxLabel)
# AD menu
$ADBox = New-Object System.Windows.Forms.Form
$ADBox.Size = New-Object System.Drawing.Size @(650,450)
$ADBox.Text = $WindowTitle
$ADBox.StartPosition = 'CenterParent'
$ADBox.AutoSize = $False
$ADBox.MaximizeBox = $False
$ADBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$ADBox.Icon = $Icon
$ADBox.BackgroundImage = $Image
# AD button 1
$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)
$ADBox.Controls.Add($ExitButton)
# 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()
$MenuBox.Dispose()
$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 1'
$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)
$MenuBox.Controls.Add($ExitButton)
# Show Menu
$MenuBox.Topmost = $True
$MenuBox.Add_Shown({$MenuBox.Activate()})
[void] $MenuBox.ShowDialog()
【问题讨论】:
-
虽然这一切都可以使用 PowerShell,但我想知道您是否应该使用 Visual Studio 和适当的表单设计器来执行此操作,因为这听起来相当复杂。在这种情况下,我认为最好在一个表单中拥有多个面板,您可以在导航期间显示/隐藏这些面板
-
谢谢arco444,我刚刚找到this 作为如何使用面板的示例。我仍在试图弄清楚。与此同时,我也在查看 Visual Studio,看看使用此工具是否更容易创建正确的表单。
标签: .net forms powershell user-interface