C++API

12個のボタンを描画して、1個目のボタンを押すと「Hallo!」のメッセージが表示される

#include <Windows.h>
#include <vector>

// グローバル変数
std::vector<HWND> g_buttons; // ボタンウィンドウのハンドルを格納するベクター

// ボタンクリック時の処理
void OnButtonClick(int index) {
    MessageBox(NULL, TEXT("Hallo!"), TEXT("Message"), MB_OK | MB_ICONINFORMATION);
}

// ウィンドウプロシージャ
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_CLOSE:
            PostQuitMessage(0);
            break;

        case WM_COMMAND:
            for (size_t i = 0; i < g_buttons.size(); ++i) {
                if *1 {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

****************************

直線を描画(\斜め線)

#include <Windows.h>

// ウィンドウプロシージャ
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_CLOSE:
            PostQuitMessage(0);
            break;

        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);

                // 直線を描画
                MoveToEx(hdc, 50, 50, NULL); // 始点の設定
                LineTo(hdc, 200, 200); // 終点の設定

                EndPaint(hwnd, &ps);
            }
            break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

// エントリーポイント
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    // ウィンドウクラスの定義
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = TEXT("WindowClass");

    // ウィンドウクラスの登録
    RegisterClass(&wc);

    // ウィンドウの作成
    HWND hwnd = CreateWindowEx(
        0,
        TEXT("WindowClass"),
        TEXT("Line Window"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    // ウィンドウ表示
    ShowWindow(hwnd, nCmdShow);

    // メッセージループ
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

****************************

以下は、Visual C++のWindowsAPIを使用して、黒い背景のウィンドウを作成し、青色のテキストで「Hallo!」を表示するプログラムの例です。

#include <Windows.h>

// ウィンドウプロシージャ
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_CLOSE:
            PostQuitMessage(0);
            break;

        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);

                // 文字列を描画
                SetTextColor(hdc, RGB(0, 0, 255)); // 色を青色に設定
                SetBkColor(hdc, RGB(0, 0, 0)); // 背景色を黒に設定
                TextOut(hdc, 20, 20, TEXT("Hallo!"), 6);

                EndPaint(hwnd, &ps);
            }
            break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

// エントリーポイント
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    // ウィンドウクラスの定義
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = TEXT("WindowClass");

    // ウィンドウクラスの登録
    RegisterClass(&wc);

    // ウィンドウの作成
    HWND hwnd = CreateWindowEx(
        0,
        TEXT("WindowClass"),
        TEXT("Text Window"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    // ウィンドウ表示
    ShowWindow(hwnd, nCmdShow);

    // メッセージループ
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

****************************

黒い背景のウィンドウを作成するプログラム

#include <Windows.h>

// ウィンドウプロシージャ
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_CLOSE:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

// エントリーポイント
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    // ウィンドウクラスの定義
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = TEXT("WindowClass");
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // 背景色を黒に設定

    // ウィンドウクラスの登録
    RegisterClass(&wc);

    // ウィンドウの作成
    HWND hwnd = CreateWindowEx(
        0,
        TEXT("WindowClass"),
        TEXT("Black Window"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    // ウィンドウ表示
    ShowWindow(hwnd, nCmdShow);

    // メッセージループ
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

*1:HWND)lParam == g_buttons[i]) {
                    OnButtonClick(i);
                    break;
                }
            }
            break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

// エントリーポイント
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    // ウィンドウクラスの定義
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = TEXT("WindowClass");

    // ウィンドウクラスの登録
    RegisterClass(&wc);

    // ウィンドウの作成
    HWND hwnd = CreateWindowEx(
        0,
        TEXT("WindowClass"),
        TEXT("Button Window"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    // ボタンの作成
    const int buttonWidth = 100;
    const int buttonHeight = 30;
    const int buttonSpacing = 10;
    const int buttonCount = 12;
    const int buttonStartX = (400 - (buttonWidth * buttonCount + buttonSpacing * (buttonCount - 1))) / 2;
    const int buttonY = 230;

    for (int i = 0; i < buttonCount; ++i) {
        HWND button = CreateWindow(
            TEXT("BUTTON"),
            TEXT("Button"),
            WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
            buttonStartX + (buttonWidth + buttonSpacing) * i, buttonY, buttonWidth, buttonHeight,
            hwnd,
            NULL,
            hInstance,
            NULL
        );
        g_buttons.push_back(button);
    }

    // ウィンドウ表示
    ShowWindow(hwnd, nCmdShow);

    // メッセージループ
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0