Skip to content

Instantly share code, notes, and snippets.

@atoz-programming-tutorials
Last active January 8, 2025 10:57
Show Gist options
  • Save atoz-programming-tutorials/f0c00244acf913c086f3eb9840dec614 to your computer and use it in GitHub Desktop.
Save atoz-programming-tutorials/f0c00244acf913c086f3eb9840dec614 to your computer and use it in GitHub Desktop.

Revisions

  1. atoz-programming-tutorials revised this gist Nov 15, 2018. No changes.
  2. atoz-programming-tutorials created this gist Nov 14, 2018.
    72 changes: 72 additions & 0 deletions win32_gdiplus.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #include <windows.h>
    #include <gdiplus.h>

    LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam);
    void draw(HDC hdc);

    int WINAPI WinMain(HINSTANCE currentInstance, HINSTANCE previousInstance, PSTR cmdLine, INT cmdCount) {
    // Initialize GDI+
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);

    // Register the window class
    const char *CLASS_NAME = "myWin32WindowClass";
    WNDCLASS wc{};
    wc.hInstance = currentInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpfnWndProc = WindowProcessMessages;
    RegisterClass(&wc);

    // Create the window
    CreateWindow(CLASS_NAME, "Win32 Drawing with GDI+ Tutorial",
    WS_OVERLAPPEDWINDOW | WS_VISIBLE, // Window style
    CW_USEDEFAULT, CW_USEDEFAULT, // Window initial position
    800, 600, // Window size
    nullptr, nullptr, nullptr, nullptr);

    // Window loop
    MSG msg{};
    while (GetMessage(&msg, nullptr, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }

    Gdiplus::GdiplusShutdown(gdiplusToken);
    return 0;
    }

    LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam) {
    HDC hdc;
    PAINTSTRUCT ps;

    switch (msg) {
    case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);
    draw(hdc);
    EndPaint(hwnd, &ps);
    return 0;
    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    default:
    return DefWindowProc(hwnd, msg, param, lparam);
    }
    }

    void draw(HDC hdc) {
    Gdiplus::Graphics gf(hdc);
    Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 0)); // For lines, rectangles and curves
    Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 255, 0)); // For filled shapes

    gf.DrawLine(&pen, 0, 0, 500, 500);
    gf.FillRectangle(&brush, 320, 200, 100, 100);
    gf.DrawRectangle(&pen, 600, 400, 100, 150);

    Gdiplus::Bitmap bmp(L"water_small.png");
    gf.DrawImage(&bmp, 430, 10);

    gf.FillEllipse(&brush, 50, 400, 200, 100);
    }