The highlight of Windows PowerShell is the support of the. NET class library, which greatly expands the capabilities of the script. This allows us to create a window directly in the script by System.Windows.Forms the assembly.
For the first time today, I tried to create a WinForm program under PowerShell to share this simple example.
As in C # writing the WinForm program, create an instance of the form class first, and then initialize: Add the streaming layout panel, add a text box and a button to the panel, adding the necessary event handling to the control. Of course when using C #, it's usually vs. automatically generates a form class for us and adds some code for the member and initialization work. However, PS does not seem to support WinForm IDE.
After the form object is created, we can show it. First use the application class to enable visual effects, and then use the Application.Run (Form) method to display the window, starting the message loop.
Application.Run () can handle the message loop for the window correctly, and the show window outside the Run () method will let the window flash through.
Because the code is relatively simple, familiar with C # WinForm programming students should be able to glance, so no longer do detailed instructions.
Note that PS uses reflection to dynamically load the assembly's methods and how events in PS are handled.
[Reflection.assembly]::loadwithpartialname ("System.Windows.Forms")
$app =[system.windows.forms.application]
$myForm =new-object System.Windows.Forms.Form
$myForm. text= "My Window"
$button 1 = new-object System.Windows.Forms.Button
$button 1. Size = New-object System.drawing.size-argumentlist 75, 23
$button 1. Text = "point I try"
$textbox 1=new-object System.Windows.Forms.TextBox
$textBox 1.Multiline = $true;
$textBox 1.Text = "Hello World"
$textBox 1.Size = New-object system.drawing.size-argumentlist 281, 227
$flowLayoutPanel 1 = new-object System.Windows.Forms.FlowLayoutPanel
$myForm. Controls.Add ($flowLayoutPanel 1)
$flowLayoutPanel 1.controls.add ($textBox 1);
$flowLayoutPanel 1.controls.add ($button 1);
$flowLayoutPanel 1.Dock = "Fill"
$flowLayoutPanel 1.FlowDirection = "Topdown"
$button 1ClickEventHandler = [System.EventHandler] {
[System.windows.forms.messagebox]::show ("Hello world!")
}
$button 1. Add_click ($button 1ClickEventHandler)
$app:: EnableVisualStyles ()
$app:: Run ($myForm)
The effect of the implementation of the following figure: