2 Pluspunkte 0 Minuspunkte

Wie kann ich auf einem Fenster mit OpenGL einfache 2D Elemente wie Rechteck, Kreis, etc. rendern?

#include <windows.h>
#include <tchar.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = _T("SimpleWindowClass");

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,
        _T("SimpleWindowClass"),
        _T("Zeichnen in C"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        0,
        0,
        hInstance,
        0);

    ShowWindow(hwnd, nCmdShow);

    MSG msg = {};
    while(GetMessage(&msg, 0, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

Hier ist ein einfaches Beispiel, das mit GDI (Graphics device interface) ein blaues Rechteck zeichnet. Beim kompilieren musst du den Parameter "-lgdi32" anhängen.

#include <windows.h>
#include <tchar.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            
            // Zeichnen Sie hier
            RECT rect = {50, 50, 150, 150}; // Koordinaten für das Rechteck
            HBRUSH blueBrush = CreateSolidBrush(RGB(0, 0, 255)); // Blaue Farbe
            FillRect(hdc, &rect, blueBrush); // Zeichnen des Rechtecks
            
            EndPaint(hwnd, &ps);
        }
        return 0;
        
        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;
            
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = _T("SimpleWindowClass");

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,
        _T("SimpleWindowClass"),
        _T("Zeichnen in C"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        0,
        0,
        hInstance,
        0);

    ShowWindow(hwnd, nCmdShow);

    MSG msg = {};
    while(GetMessage(&msg, 0, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

von (532 Punkte)