读书人

Powershell创办WinForm应用程序

发布时间: 2012-09-15 19:09:29 作者: rapoo

Powershell创建WinForm应用程序

Function Show-WinForm([Array]$objectArray){
#$objectArray = @($input)
#Ensure that they've piped information into the script
if($objectArray.Count -eq 0 )
{
?Write-Error "This script requires pipeline input." ;
?return
}
#load the windows Forms assembly
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms");
##Create the main form
$form = New-Object Windows.Forms.Form
$form.Size? = New-Object System.Drawing.Size @(600,600);

#Create the listBox to hold the items form the pipeline
$listbox = New-Object System.Windows.Forms.CheckedListBox;
$listbox.CheckOnClick = $true
$listbox.Dock = 'Fill' ;
$form.Text = "Select the list of objects you wish to pass down the pipeline"
$listbox.Items.AddRange($objectArray);

#Create the button panel to hold the ok and cancel buttons
$buttonPanel = New-Object Windows.Forms.Panel ;
$buttonPanel.Size = New-Object System.Drawing.Size @(600,300);
$buttonPanel.Dock = "Bottom";

#Crate the cancel button ,which will anchor to the bottom right
$cancleButton = New-Object Windows.Forms.Button
$cancleButton.Text = "Cancel" ;
$cancleButton.DialogResult = "Cancel"
$cancleButton.Top = $buttonPanel.Height - $cancleButton.Height - 5;
$cancleButton.Left = $buttonPanel.Width - $cancleButton.Width - 10;
$cancleButton.Anchor = 'Right' ;

#Create the ok button, which will anchor to the left of cancel
$okButton = New-Object windows.Forms.Button ;
$okButton.Text = "OK"
$okButton.DialogResult = "OK" ;
$okButton.Top = $cancleButton.Top ;
$okButton.Left = $cancleButton.Left - $okButton.Width - 5;
$okButton.Anchor = 'Right' ;

#Add the buttons to the button panel
$buttonPanel.Controls.Add($okButton);
$buttonPanel.Controls.Add($cancleButton);
#add the button panel and list box to the form and also set the actions for the buttons
$form.Controls.Add($listbox);
$form.Controls.Add($buttonPanel);
$form.AcceptButton? = $okButton ;
$form.CancelButton = $cancleButton ;
$form.add_Shown({ $form.Activate() });

#show the form and wait for the response
$result = $form.ShowDialog();

#if the pressed oj or enter go through all the checked items and send the corresponding object down the pipeline
if($result -eq "OK")
{
?foreach($index in $listbox.CheckedIndices)
?{
?? $objectArray[$index] ;
?}
}
}

读书人网 >编程

热点推荐