Archive for the ‘Pascal’ Category

  1. Hello, COM(Pascal) World!

    Posted on 1月 29th, 2013 by cx20

    COM(Pascal)

    COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
    COM を用いて開発された部品であれば言語を問わず利用することができる。
    以下は Free Pascal による COM クライアントの例となっている。

    ソースコード

    program hello;
     
    uses
        ComObj;
     
    var
        shell: Variant;
        folder: Variant;
     
    begin
        shell := CreateOleObject('Shell.Application');
        folder := shell.BrowseForFolder( 0, 'Hello, COM(Pascal) World!', 0, 36 );
    end.

    実行方法

    C:¥> fpc hello.pas

    実行結果

    +----------------------------------------+
    |Browse For Folder                    [X]|
    +----------------------------------------+
    | Hello, COM(Pascal) Wolrd!              |
    |                                        |
    | +------------------------------------+ |
    | |[Windows]                           | |
    | | +[addins]                          | |
    | | +[AppCompat]                       | |
    | | +[AppPatch]                        | |
    | | +[assembly]                        | |
    | |     :                              | |
    | |     :                              | |
    | |     :                              | |
    | +------------------------------------+ |
    | [Make New Folder]    [  OK  ] [Cancel] |
    +----------------------------------------+
  2. Hello, Win32 GUI(Pascal) World!

    Posted on 7月 20th, 2012 by cx20

    Win32 GUI(Pascal)

    Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
    以下は Free Pascal における Win32 GUI アプリケーション の例となっている。

    ソースコード

    {$APPTYPE GUI}
    {$MODE DELPHI}
    program hello;
     
    uses
        Windows, Messages;
     
    function WindowProc(hWindow:HWnd; message:Cardinal; wParam:Word; lParam:Longint):LongWord; stdcall;
    var
        hdc:        THandle;
        ps:         TPaintStruct;
    const
        strMessage = 'Hello, Win32 GUI(Pascal) World!';
    begin
        case message of
            WM_PAINT:
                begin
                    hdc := BeginPaint(hWindow, ps );
                    TextOut( hdc, 0, 0, strMessage, Length(strMessage) );
                    EndPaint( hWindow, ps );
                end;
     
            WM_DESTROY:
                PostQuitMessage(0);
        else
            WindowProc := DefWindowProc(hWindow, message, wParam, lParam);
            exit;
        end;
        WindowProc := 0;
    end;
     
    function WinMain(hInstance, hPrevInstance:THandle; lpCmdLine:PAnsiChar; nCmdShow:Integer):Integer; stdcall;
    var
        wcex:       TWndClassEx;
        hWindow:    HWnd;
        msg:        TMsg;
    const
        ClassName = 'helloWindow';
        WindowName = 'Hello, World!';
     
    begin
        wcex.cbSize         := SizeOf(TWndclassEx);
        wcex.style          := CS_HREDRAW or CS_VREDRAW;
        wcex.lpfnWndProc    := WndProc(@WindowProc);
        wcex.cbClsExtra     := 0;
        wcex.cbWndExtra     := 0;
        wcex.hInstance      := hInstance;
        wcex.hIcon          := LoadIcon(0, IDI_APPLICATION);
        wcex.hCursor        := LoadCursor(0, IDC_ARROW);
        wcex.hbrBackground  := COLOR_WINDOW +1;
        wcex.lpszMenuName   := nil;
        wcex.lpszClassName  := ClassName;
     
        RegisterClassEx(wcex);
        hWindow := CreateWindowEX(
            0,
            ClassName,
            WindowName,
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
            0, 0, hInstance, nil
        );
     
        ShowWindow(hWindow, SW_SHOWDEFAULT);
        UpdateWindow(hWindow);
     
        while GetMessage(msg, 0, 0, 0) do begin
            TranslateMessage(msg);
            DispatchMessage(msg);
        end;
     
        WinMain := msg.wParam;
    end;
     
    begin
        WinMain( hInstance, 0, nil, cmdShow );
    end.

    コンパイル方法

    C:¥> fpc hello.pas

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI(Pascal) World!           |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  3. Hello, Win32 API(Pascal) World!

    Posted on 6月 28th, 2012 by cx20

    Win32 API(Pascal)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は Free Pascal からの呼出し例である。

    ソースコード

    {$APPTYPE GUI}
    {$MODE DELPHI}
    program hello;
     
    uses
        Windows;
     
    begin
        MessageBox( 0, 'Hello, Win32 API(Pascal) World!', 'Hello, World!', MB_OK );
    end.

    コンパイル方法(Free Pascal)

    C:¥> fpc hello.pas

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Pascal) World!
    ---------------------------
    OK   
    ---------------------------
  4. Hello, Oxygene World!

    Posted on 1月 24th, 2012 by cx20

    Oxygene

    Oxygene は RemObjects Software 社による .NET または Java 環境向けのプログラミング言語である。
    Embarcadero 社の Delphi Prism のプログラミング言語として採用されている。
    コンパイラ単体は「Command Line Compiler」として無償提供されている。
    C# 同様に、UNIX 環境向けの .NET Framework 互換プロジェクト「Mono」により他の OS でも動作させることができる。

    ソースコード(.NET ライブラリを使用した場合)

    namespace hello;
     
    interface
     
    type
        Hello = class
    public
        class method Main(args: array of String): Integer;
    end;
     
    implementation
     
    class method Hello.Main(args: array of String): Integer;
    begin
        System.Console.WriteLine('Hello, Oxygene World!');
    end;
     
    end.

    ソースコード(Java ライブラリを使用した場合)

    namespace hello;
     
    interface
     
    type
        Hello = class
    public
        class method Main(args: array of String): Integer;
    end;
     
    implementation
     
    class method Hello.Main(args: array of String): Integer;
    begin
        System.out.println('Hello, Oxygene World!');
    end;
     
    end.

    コンパイル&実行方法(.NET プログラムとしてコンパイル&実行)

    C:¥> oxygene HelloDotNet.pas -type:exe -mode:Net
    C:¥> HelloDotNet

    コンパイル&実行方法(Java プログラムとしてコンパイル&実行)

    C:¥> oxygene HelloJava.pas -mode:Java
    C:¥> java -jar hellojava.jar

    実行結果

    Hello, Oxygene World!
  5. Hello, Pascal World!

    Posted on 12月 9th, 2011 by cx20

    Pascal

    Pascal は教育用言語として開発された構造化プログラミングに適した言語である。名前は世界最初の機械式計算機を開発した「ブレーズ・パスカル」に由来する。
    ちなみに Borland 社の Turbo Pascal や、その後継の Delphi ではコンパイルの早さに定評があった。
    なお、Borland Delphi は、いくつかの会社(Borland → Inprise → CodeGear → Embarcadero)を経て、現在は、Embarcadero Delphi となっている。
    また、Delphi は当初、Oracle Database のフロントエンドの採用を目論んで名付けられたとされている。

    ソースコード

    program hello;
    begin
        writeln( 'Hello, Pascal World!' );
    end.

    コンパイル方法(Free Pascal)

    $ fpc hello.pas

    コンパイル方法(GNU Pascal)

    $ gpc -o hello hello.pas

    コンパイル方法(Delphi XE2)

    C:¥> dcc32 hello.pas

    実行結果

    Hello, Pascal World!