To create a WinForm program using Windows PowerShell

Source: Internet
Author: User
Tags argumentlist reflection

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.