程式語言 - Wine - C/C++ - Painting - Create Pen



參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gammasoft71/Examples_Win32
http://masm32.com/board/index.php?topic=3584.0
https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles

Pen可以想像成是一支畫筆,粗細、顏色都可以設定,DC(Device Context)可以透過SelectObject()選擇畫筆

main.c

#include <windows.h>

HWND hWin = NULL;
WNDPROC defWndProc = NULL;

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc = NULL;
    HPEN pen = NULL;
    PAINTSTRUCT ps = {0};

    switch (uMsg) {
    case WM_CLOSE:
        DestroyWindow(hWnd);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        pen = CreatePen(PS_SOLID, 10, RGB(0xff, 0x00, 0x00));
        SelectObject(hdc, pen);
        MoveToEx(hdc, 0, 0, NULL);
        LineTo(hdc, 100, 100);
        EndPaint(hWnd, &ps);
        DeleteObject(pen);
        break;
    }
    return CallWindowProc(defWndProc, hWnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    hWin = CreateWindow(WC_DIALOG, "main", 
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL);
    defWndProc = (WNDPROC)SetWindowLongPtr(hWin, GWLP_WNDPROC, (long int)WndProc);

    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0)) {
        DispatchMessage(&msg);
    }
    ExitProcess(0);
    return 0;
}

Line 21:創造一支Pen,粗細是10,顏色則是紅色
Line 22:選擇要使用的Pen
Line 23~24:畫一條線
Line 26:刪除資源

編譯、執行

$ winegcc main.c -o main -lgdi32
$ wine ./main.exe