2 Pluspunkte 0 Minuspunkte
Wie kann ich einfache 2D Elemente und Text auf einer Canvas zeichnen in C um eine Art grafische Oberfläche selbst zu erstellen?
von  

3 Antworten

1 Pluspunkt 0 Minuspunkte

GDI ist eine Windows API mit Funktionen zum Zeichnen von Linien, Kreisen, Rechtecken, Text und anderen grafischen Elementen.

#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);
            
            // Erstellen eines Brush (Pinsel)
            HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 255));
            HGDIOBJ hOldBrush = SelectObject(hdc, hBrush);
            
            // Zeichne einen blauen Kreis
            Ellipse(hdc, 50, 50, 150, 150);
            
            SelectObject(hdc, hOldBrush);
            DeleteObject(hBrush);
            
            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 = L"SimpleWindowClass";

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0,
        L"SimpleWindowClass",
        _T("GDI 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 (396 Punkte)  
1 Pluspunkt 0 Minuspunkte

Du kannst SDL auf Linux, Windows und Mac verwenden.

#include <SDL2/SDL.h>

int main(int argc, char* argv[]) {

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("Blaues Viereck", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); // Blaue Farbe (R=0, G=0, B=255, A=255)
    SDL_Rect rect = {100, 100, 200, 200}; // Koordinaten (x, y) und Dimensionen (Breite, Höhe) des Vierecks

    SDL_RenderFillRect(renderer, &rect);
    SDL_RenderPresent(renderer);

    SDL_Delay(2000);

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
    
}
von (532 Punkte)  
0 Pluspunkte 0 Minuspunkte

GLFW ist eine beliebte Bibliothek um grafische Inhalte plattformunabhängig in einem Fenster darzustellen.

#include <GLFW/glfw3.h>

int main(void){

    GLFWwindow* window;

    if(!glfwInit())
        return -1;

    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);

    if(!window){
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    while(!glfwWindowShouldClose(window)){

        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);

        glfwPollEvents();

    }

    glfwTerminate();
    return 0;

}

von (716 Punkte)