PowerShell .NET预加载表单

时间:2015-01-14 10:15:56

标签: .net winforms forms powershell

我有两种形式,其中一种是主要形式(Show-MainForm),其中有许多按钮和其中的东西加载速度很快。然后有另一种形式(Show-SelectionBox)需要25秒或更长时间来加载,因为它需要查询一些东西。

按主窗体中的按钮时会调用选择框表单。所以我一直在寻找一种方法来开始加载选择框表单,但还没有显示它,所以当人们点击调用它的按钮时它会更快。

有没有办法预加载此选择框表单?谢谢你的帮助。

示例代码:

Function Show-SelectionBox {
    Function Add-Node($Nodes, $Path) {  
        $Path.Split("/") | % {
            Write-Verbose "Searching For: $_"
            $SearchResult = $Nodes.Find($_, $False)

        If ($SearchResult.Count -eq 1) {
          Write-Verbose "Selecting: $($SearchResult.Name)"
          $Nodes = $SearchResult[0].Nodes
        }
        Else {
          Write-Verbose "Adding: $_"
          $Node = New-Object Windows.Forms.TreeNode($_)
          $Node.Name = $_
          $Nodes.Add($Node)  | Out-Null
        }
      }
    }

    Function Get-CheckedNodes {
        Param (
            $nodes
        )
        foreach ($n in $Nodes) {
            if ($n.nodes.count -gt 0) {
                Get-CheckedNodes $n.nodes
            }
            if ($n.checked) {
                Write-Output $n.Name
            }           
        }   
    }
    Start-Sleep -Seconds 5 #slow loading

    $script:SelectedOU = $null

    Add-Type -AssemblyName System.Windows.Forms
    $Form = New-Object System.Windows.Forms.Form
    $Form.ClientSize = '342, 502'
    $Form.FormBorderStyle = 'FixedDialog'

    $ButtonOK_Click = {
        $script:SelectedOU = Get-CheckedNodes $treeView.Nodes
        $Form.close()
    }

    $TreeView = New-Object System.Windows.Forms.TreeView
    $TreeView.Dock = 'Fill'
    $TreeView.CheckBoxes = $true

    Add-Node $TreeView.Nodes "Fruits"  
    Add-Node $TreeView.Nodes "Vegetables"  
    Add-Node $TreeView.Nodes "Cars" 

    $ButtonCancel = New-Object System.Windows.Forms.Button
    $ButtonCancel.text = “&Cancel”
    $ButtonCancel.Location = '120,467'
    $ButtonCancel.size = '75,23'
    $ButtonCancel.Anchor = 'Bottom, Left'
    $ButtonCancel.add_Click({$Form.close()})
    $Form.Controls.Add($ButtonCancel)

    $ButtonOK = New-Object System.Windows.Forms.Button
    $ButtonOK.Location = '245,467'
    $ButtonOK.Size = '75,23'
    $ButtonOK.Name = 'ButtonOK'
    $ButtonOK.Text = 'OK'
    $ButtonOK.add_Click($ButtonOK_Click)
    $Form.Controls.Add($ButtonOK)
    $Form.Controls.Add($TreeView)

    $Form.ShowDialog() > $null
}

Function Show-MainForm {
    Add-Type -AssemblyName System.Windows.Forms

    $Form = New-Object System.Windows.Forms.Form
    $Form.StartPosition = 'CenterScreen'
    $Form.ClientSize = '665,474'
    $Form.MaximizeBox = $False
    $Form.AutoSize = $False
    $Form.FormBorderStyle = 'Fixed3D'
    $Form.Text = 'Script Center'

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

    $Button = New-Object System.Windows.Forms.Button
    $Button.Text = 'Launch Selectionbox'
    $Button.TabIndex = 2
    $Button.add_Click({Show-SelectionBox})
    $Button.Location = '8,60'
    $Button.Size = '100,35'
    $Form.Controls.Add($Button)

    $Panel = New-Object System.Windows.Forms.Panel
    $Panel.Location = '122,60'
    $Panel.Size = '535,390'
    $Panel.TabIndex = 8
    $Panel.BackColor = 'CornflowerBlue'
    $Panel.BorderStyle = 'Fixed3D'
    $Form.Controls.Add($Panel)

    $Form.ShowDialog()| Out-Null
}

Show-MainForm

0 个答案:

没有答案