Archive for 6月 17th, 2012

  1. Hello, Windows Forms(UWSC) World!

    Posted on 6月 17th, 2012 by cx20

    Windows Forms(UWSC)

    Windows フォーム(Windows Forms)は .NET Framework におけるユーザーインターフェイス基盤である。Windows アプリケーションにおけるウィンドウやダイアログに対応する。
    以下は UWSC における Windows フォーム の例となっている。
    UWSC 自体は .NET 対応言語ではないが、v4.6b より追加された PowerShell() 関数により、PowerShell 経由で Windows フォームが利用できるようになっている。

    ソースコード(UWSC + PowerShell)

    Option Explicit
     
    Main()
     
    Procedure Main()
        PowerShell( helloBlock, True, True )
    FEnd
     
    TextBlock helloBlock
        [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(UWSC) World!"
        $form.Controls.Add( $label1 )
        $form.ShowDialog()
    EndTextBlock

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

    Option Explicit
     
    Main()
     
    Procedure Main()
        PowerShell( helloBlock, True, True )
    FEnd
     
    TextBlock helloBlock
        $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(UWSC) 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()
    EndTextBlock

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

    Option Explicit
     
    Main()
     
    Procedure Main()
        PowerShell( helloBlock, True, True )
    FEnd
     
    TextBlock helloBlock
        $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(UWSC) 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()
    EndTextBlock

    実行方法

    C:¥> uwsc /K hello.uws

    実行結果

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