Archive for the ‘Delphi’ Category

  1. Hello, Win32 GUI(Delphi) World!

    Posted on 7月 20th, 2012 by cx20

    Win32 GUI(Delphi)

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

    ソースコード

    program hello;
     
    uses
        Windows, Messages;
     
    function WndProc(hWindow:HWnd; message:Cardinal; wParam:Word; lParam:Longint):LongWord; stdcall;
    var
        hdc:        THandle;
        ps:         TPaintStruct;
    const
        strMessage = 'Hello, Win32 GUI(Delphi) 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
            Result := DefWindowProc(hWindow, message, wParam, lParam);
            exit;
        end;
        Result := 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;
        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;
     
        Result := msg.wParam;
    end;
     
    begin
        WinMain( hInstance, 0, nil, cmdShow );
    end.

    コンパイル方法

    C:¥> dcc32 hello.pas

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI(Delphi) World!           |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  2. Hello, Win32 API(Oxygene) World!

    Posted on 6月 26th, 2012 by cx20

    Win32 API(Oxygene)

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

    ソースコード

    namespace hello;
     
    interface
    uses
        System,
        System.Runtime.InteropServices;
     
    type
        Hello =  static class
    public
        [DllImport('user32.dll')]
        method MessageBox(hwnd: Integer; text: String; caption: String; utype: Integer): Integer; external;
        class method Main(args: array of String): Integer;
    end;
     
    implementation
     
    [STAThread] 
    class method Hello.Main(args: array of String): Integer;
    begin
        MessageBox( 0, 'Hello, Win32 API(Oxygene) World!', 'Hello, World!', 0 );
    end;
     
    end.

    コンパイル方法

    C:¥> oxygene hello.pas

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Oxygene) World!
    ---------------------------
    OK   
    ---------------------------
  3. Hello, Windows Forms(Oxygene) World!

    Posted on 6月 16th, 2012 by cx20

    Windows Forms(Oxygene)

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

    ソースコード

    namespace hello;
     
    interface
    uses
        System,
        System.Drawing,
        System.Windows.Forms;
     
    type
        HelloForm = class(System.Windows.Forms.Form)
        public
            constructor;
            class method Main(args: array of String): Integer;
        end;
     
    implementation
     
    constructor HelloForm;
    var
        label1: Label;
    begin
        self.Size := new Size( 640, 480 );
        self.Text := "Hello, World!";
        label1 := new Label();
        label1.Size := new Size( 320, 20 );
        label1.Text := "Hello, Windows Forms(Oxygene) World!";
        self.Controls.Add( label1 );
    end;
     
    [STAThread] 
    class method HelloForm.Main(args: array of String): Integer;
    begin
        using form := new HelloForm do
        Application.Run(form);
    end;
     
    end.

    実行方法

    C:¥> oxygene Hello.pas ^
        -type:winexe ^
        -ref:System.dll;System.Drawing.dll;System.Windows.Forms.dll

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Windows Forms(Oxygene) World!      |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  4. Hello, COM(Delphi) World!

    Posted on 5月 30th, 2012 by cx20

    COM(Delphi)

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

    ソースコード

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

    実行方法

    C:¥> dcc32 hello.pas

    実行結果

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

    Posted on 5月 5th, 2012 by cx20

    Win32 API(Delphi)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は Delphi からの呼出し例である。
    Win32 API の参照方法としては、external 宣言を用いる方法の他、Winapi.Windows ライブラリを用いる方法などがある。

    ソースコード(external 宣言)

    program hello;
     
    function MessageBox(hWnd: THandle; lpText: PAnsiChar; lpCaption: PAnsiChar; uType: Cardinal): Integer;
        stdcall; external 'user32.dll' name 'MessageBoxA';
     
    begin
        MessageBox( 0, 'Hello, Win32 API(Delphi) World!', 'Hello, World!', 0 );
    end.

    ソースコード(Winapi.Windows ライブラリ)

    program hello;
     
    uses
        Windows;
     
    begin
        MessageBox( 0, 'Hello, Win32 API(Delphi) World!', 'Hello, World!', MB_OK );
    end.

    Win32 データ型と Delphi データ型の対応は主に以下のようになっている。

    Win32 データ型 C/C++ データ型 Delphi データ型
    HANDLE void * THandle
    BYTE unsigned char Byte
    SHORT short Smallint
    WORD unsigned short Word
    INT int Integer
    UINT unsigned int Cardinal
    LONG long Integer
    BOOL int LongBool
    DWORD unsigned long LongWord
    ULONG unsigned long LongWord
    CHAR char AnsiChar
    WCHAR wchar_t WideChar, Char
    LPSTR char * PAnsiChar
    LPCSTR const char * PAnsiChar
    LPWSTR wchar_t * PWideChar, PChar
    LPCWSTR const wchar_t * PWideChar, PChar
    FLOAT float Single
    DOUBLE double Double
    LONGLONG __int64 Int64
    DWORD64 unsigned __int64 UInt64

    コンパイル方法(Delphi XE2)

    C:¥> dcc32 hello.pas

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Delphi) World!
    ---------------------------
    OK   
    ---------------------------
  6. 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!
  7. 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!