Hello, Win32 GUI(D言語) World!

Posted on 7月 19th, 2012 by cx20

Win32 GUI(D言語)

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

ソースコード

import core.runtime;
import std.c.windows.windows;
import std.string;
 
extern(Windows)
LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint( hWnd, &ps );
        enum text = "Hello, Win32 GUI(D) World!";
        TextOutA( hdc, 0, 0, text, text.length );
        EndPaint( hWnd, &ps );
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcA(hWnd, message, wParam, lParam);
        break;
    }
 
    return 0;
}
 
extern(Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    string className = "helloWindow";
    string windowName = "Hello, World!";
    HWND hWnd;
    MSG  msg;
    WNDCLASSA wc;
 
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = &WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIconA(null, IDI_APPLICATION);
    wc.hCursor       = LoadCursorA(null, IDC_ARROW);
    wc.hbrBackground = cast(HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = null;
    wc.lpszClassName = className.toStringz;
 
    RegisterClassA(&wc);
    hWnd = CreateWindowA(className.toStringz, 
                         windowName.toStringz,
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
                         null, null, hInstance, null);
 
    ShowWindow(hWnd, SW_SHOWDEFAULT);
    UpdateWindow(hWnd);
 
    while (GetMessageA(&msg, null, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessageA(&msg);
    }
 
    return msg.wParam;
}

コンパイル方法

C:¥> dmd -L/subsystem:windows gdi32.lib hello.d

実行結果

+------------------------------------------+
|Hello, World!                    [_][~][X]|
+------------------------------------------+
|Hello, Win32 GUI(D) World!                |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
+------------------------------------------+

Tags:

Categories: D言語, Win32 API, Win32 GUI

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

WP-SpamFree by Pole Position Marketing