Archive for 6月 7th, 2012

  1. Hello, Windows Forms(PowerShell) World!

    Posted on 6月 7th, 2012 by cx20

    Windows Forms(PowerShell)

    Windows フォーム(Windows Forms)は .NET Framework におけるユーザーインターフェイス基盤である。Windows アプリケーションにおけるウィンドウやダイアログに対応する。
    以下は PowerShell における Windows フォーム の例となっている。

    ソースコード(PowerShell のみ)

    [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $form = New-Object Windows.Forms.Form
    $form.Size = New-Object Drawing.Size 640,480
    $form.Text = "Hello, World!"
    $label1 = New-Object Windows.Forms.Label
    $label1.Size = New-Object Drawing.Size 320, 20
    $label1.Text = "Hello, Windows Forms(PowerShell) World!"
    $form.Controls.Add( $label1 )
    $form.ShowDialog()

    なお、PowerShell 2.0 から Add-Type コマンドレットを用いることにより C# や VB.NET のコードも呼び出せるようになっている。

    ソースコード(PowerShell + C#)

    $source = @"
    using System;
    using System.Drawing;
    using System.Windows.Forms;
     
    public class HelloForm : Form
    {
        public HelloForm()
        {
            this.Size = new Size( 640, 480 );
            this.Text = "Hello, World!";
     
            Label label1 = new Label();
            label1.Size = new Size( 320, 20 );
            label1.Text = "Hello, Windows Forms(PowerShell) World!";
     
            this.Controls.Add( label1 );
        }
        [STAThread]
        public static void Main()
        {
            HelloForm form = new HelloForm();
            Application.Run(form);
        }
    }
    "@
    Add-Type -Language CSharp -TypeDefinition $source -ReferencedAssemblies ("System.Drawing", "System.Windows.Forms" )
    [HelloForm]::Main()

    ソースコード(PowerShell + VB.NET)

    $source = @"
    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
     
    Public Class HelloForm
        Inherits Form
     
        Public Sub New()
            Me.Size = New Size( 640, 480 )
            Me.Text = "Hello, World!"
     
            Dim label1 As New Label
            label1.Size = New Size( 320, 20 )
            label1.Text = "Hello, Windows Forms(PowerShell) World!"
     
            Me.Controls.Add( label1 )
        End Sub
     
        <STAThread> _
        Shared Sub Main()
            Dim form As New HelloForm()
            Application.Run(form)
        End Sub
    End Class
    "@
    Add-Type -Language VisualBasic -TypeDefinition $source -ReferencedAssemblies ("System.Drawing", "System.Windows.Forms" )
    [HelloForm]::Main()

    実行方法

    C:¥> PowerShell -File Hello.ps1

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Windows Forms(PowerShell) World!   |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+