簡體   English   中英

PowerShell .NET在表單中導航

[英]PowerShell .NET Navigating within forms

當創建一個表單並想通過單擊該表單上的按鈕進行導航時,是否需要創建一個全新的表單來顯示下一頁? 還是可以使用其他按鈕以某種方式重新初始化當前表單?

單擊“ 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()

我自己做了一些東西,希望對您有用:

Function Open-Form {

[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles() | Out-Null

$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + '\powershell.exe')
$Image = [system.drawing.image]::FromFile('S:\Prod\Script center\Background.jpg')

$PanelLocation = New-Object System.Drawing.Point(122,60)
$PanelSize = New-Object System.Drawing.Size(535,390)
$ButtonSize = New-Object System.Drawing.Size(100,35)
$PanelBackColor = [System.Drawing.Color]::CornflowerBlue

# Navigation
$ButtonHome_OnClick  = {$PanelHome.Visible=$true;$PanelAD.Visible = $false;$PanelFF.Visible = $false;$PanelAbout=$false}
$ButtonAD_OnClick    = {$PanelHome.Visible=$false;$PanelAD.Visible = $true;$PanelFF.Visible = $false;$PanelAbout=$false}
$ButtonFF_OnClick    = {$PanelHome.Visible=$false;$PanelAD.Visible = $false;$PanelFF.Visible = $True;$PanelAbout=$false}
$ButtonAbout_OnClick = {$PanelHome.Visible=$false;$PanelAD.Visible = $false;$PanelFF.Visible = $false;$PanelAbout=$true}

$Form = New-Object System.Windows.Forms.Form
$Form.StartPosition = 'CenterScreen'
$Form.ClientSize = New-Object System.Drawing.Size(665,474)
$Form.MaximizeBox = $False
$Form.AutoSize = $False
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Form.Icon = $Icon
$Form.BackgroundImage = $Image
$Form.Text = 'Script Center'
$Form.TopMost = $True
$Form.add_Load($handler_form1_Load)

$TextTop = New-Object System.Windows.Forms.Label
$TextTop.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",14,1,3,1)
$TextTop.Location = New-Object System.Drawing.Point(1,20)
$TextTop.Size = New-Object System.Drawing.Size(380,40) 
$TextTop.Text = 'Welcome to Script Center'
$TextTop.BackColor = 'Transparent'
$TextTop.TextAlign = [System.Drawing.ContentAlignment]::TopCenter
$Form.Controls.Add($TextTop)

$ButtonHome = New-Object System.Windows.Forms.Button
$ButtonHome.Text = 'Home'
$ButtonHome.TabIndex = 2
$ButtonHome.UseVisualStyleBackColor = $True
$ButtonHome.add_Click($ButtonHome_OnClick)
$ButtonHome.Location = New-Object System.Drawing.Point(8,60)
$ButtonHome.Size = $ButtonSize
$Form.Controls.Add($ButtonHome)

$ButtonAD = New-Object System.Windows.Forms.Button
$ButtonAD.Text = 'Active Directory'
$ButtonAD.TabIndex = 3
$ButtonAD.UseVisualStyleBackColor = $True
$ButtonAD.add_Click($ButtonAD_OnClick)
$ButtonAD.Location = New-Object System.Drawing.Point(8,100)
$ButtonAD.Size = $ButtonSize
$Form.Controls.Add($ButtonAD)

$ButtonFF = New-Object System.Windows.Forms.Button
$ButtonFF.Text = 'Files and Folders'
$ButtonFF.TabIndex = 4
$ButtonFF.UseVisualStyleBackColor = $True
$ButtonFF.add_Click($ButtonFF_OnClick)
$ButtonFF.Location = New-Object System.Drawing.Point(8,140)
$ButtonFF.Size = $ButtonSize
$Form.Controls.Add($ButtonFF)

$ButtonAbout = New-Object System.Windows.Forms.Button
$ButtonAbout.Text = 'About'
$ButtonAbout.TabIndex = 7
$ButtonAbout.UseVisualStyleBackColor = $True
$ButtonAbout.add_Click($ButtonAbout_OnClick)
$ButtonAbout.Location = New-Object System.Drawing.Point(8,414)
$ButtonAbout.Size = $ButtonSize
$Form.Controls.Add($ButtonAbout)

# Panel 1: Home
$PanelHome = New-Object System.Windows.Forms.Panel
$PanelHome.Location = $PanelLocation
$PanelHome.Size = $PanelSize
$PanelHome.TabIndex = 8
#$PanelHome.BackgroundImage = $Image
#$PanelHome.BackgroundImageLayout = 'Zoom' # None, Tile, Center, Stretch, Zoom
$PanelHome.BackColor = $PanelBackColor
$PanelHome.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
$Form.Controls.Add($PanelHome)

# Panel 2: Active Directory
$PanelAD = New-Object System.Windows.Forms.Panel
$PanelAD.Location = $PanelLocation
$PanelAD.Size = $PanelSize
$PanelAD.TabIndex = 8
$PanelAD.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
$Form.Controls.Add($PanelAD)

$PanelADTabControl = New-object System.Windows.Forms.TabControl
$PanelADTabControl.TabIndex = 4
$PanelADTabControl.SelectedIndex = 0
$PanelADTabControl.Location = New-Object System.Drawing.Point(0,0)
$PanelADTabControl.Size = New-Object System.Drawing.Size(535,390)
$PanelAD.Controls.Add($PanelADTabControl)

$PanelADTab1 = New-Object System.Windows.Forms.TabPage
$PanelADTab1.Location = New-Object System.Drawing.Point(0,0)
$PanelADTab1.Size = New-Object System.Drawing.Size(205,445) 
$PanelADTab1.TabIndex = 2
$PanelADTab1.Text = 'Users without manager'
$PanelADTab1.UseVisualStyleBackColor = $True
$PanelADTab1.BackColor = $PanelBackColor
$PanelADTabControl.Controls.Add($PanelADTab1)

$PanelADTab2 = New-Object System.Windows.Forms.TabPage
$PanelADTab2.Location = New-Object System.Drawing.Point(4,22)
$PanelADTab2.Size = New-Object System.Drawing.Size(205,445) 
$PanelADTab2.TabIndex = 2
$PanelADTab2.Text = 'Inconsistencies'
$PanelADTab2.UseVisualStyleBackColor = $True
$PanelADTab2.BackColor = $PanelBackColor
$PanelADTabControl.Controls.Add($PanelADTab2)

# Panel 3: Files and Folders
$PanelFF = New-Object System.Windows.Forms.Panel
$PanelFF.Location = $PanelLocation
$PanelFF.Size = $PanelSize
$PanelFF.TabIndex = 8
$PanelFF.BackColor = $PanelBackColor
$PanelFF.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
$Form.Controls.Add($PanelFF)

$PanelFFTxt = New-Object System.Windows.Forms.Label
#$PanelFFTxt.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",14,1,3,1)
$PanelFFTxt.Location = New-Object System.Drawing.Point(1,20)
$PanelFFTxt.Size = New-Object System.Drawing.Size(380,40) 
$PanelFFTxt.Text = 'This is the panel for Files and Folders'
$PanelFFTxt.BackColor = 'Transparent'
$PanelFFTxt.TextAlign = [System.Drawing.ContentAlignment]::TopCenter
$PanelFF.Controls.Add($PanelFFTxt)

# Panel X: About
$PanelAbout = New-Object System.Windows.Forms.Panel
$PanelAbout.Location = $PanelLocation
$PanelAbout.Size = $PanelSize
$PanelAbout.TabIndex = 8
$PanelAbout.BackColor = $PanelBackColor
$PanelAbout.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
$Form.Controls.Add($PanelAbout)

$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Location = New-Object System.Drawing.Point(589,458)
$ProgressBar.Size = New-Object System.Drawing.Size(75,15)
$ProgressBar.TabIndex = 0
$Form.Controls.Add($ProgressBar)

$StatusBar = New-Object System.Windows.Forms.StatusBar
$StatusBar.Text = 'StatusBar'
$StatusBar.Location = New-Object System.Drawing.Point(0,456)
$StatusBar.Size = New-Object System.Drawing.Size(665,18)
$StatusBar.TabIndex = 1
$Form.Controls.Add($StatusBar)

$Form.ShowDialog()| Out-Null
}

Open-Form

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM