MinGW >> C/C++ >> Painting

Brush


參考資訊:
1. win32
2. tutorial
3. examples_win32

繪畫的顏色設定可以分成Brush、Pen兩種,Brush代表填充的顏色,Pen則是線條的顏色

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;
    HBRUSH brush = 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);
        brush = CreateSolidBrush(RGB(0xff, 0x00, 0x00));
        FillRect(hdc, &ps.rcPaint, brush);
        EndPaint(hWnd, &ps);
        DeleteObject(brush);
        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:產生一個紅色的Brush
Line 22:將可視區域填入紅色
Line 24:釋放資源

編譯、執行

$ i686-w64-mingw32-gcc -mwindows main.c -o main.exe
$ wine ./main.exe


返回上一頁