Drawing A Yellow Rectangle

VC++, WinAPI, GDI+ on Windows

This program was written with Microsoft Visual Studio Community 2017 and compiled with Visual C++ 2017; at the time this article was written, both could be acquired from Microsoft, Inc. by having a „Microsoft Live Account“, which is free of charge.

Execution temporarily changes the display resolution of the primary screen to 640 pixels width, 480 pixels height.

The program has been tested on Windows 10.

/* DesktopRectangle.cpp - Draw a yellow rectangle using GDI+. */
/* Released into the Public Domain on Mar 11 2018 by Tilman Kranz . */

#include "stdafx.h"

#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>

using namespace std;
using namespace Gdiplus;

#pragma comment (lib, "Gdiplus.lib")

#define WIDTH 640
#define HEIGHT 480

VOID OnPaint(HDC hdc) {
    Graphics graphics(hdc);
    SolidBrush yellowBrush(Color(255, 255, 255, 0));

    graphics.FillRectangle(&yellowBrush, 10, 20, 100, 75);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    HDC hdc;
    PAINTSTRUCT  ps;

    switch (message) {
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            OnPaint(hdc);
            EndPaint(hWnd, &ps);

            return 0;
        case WM_KEYDOWN | WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
}

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow) {
    HWND hWnd;
    MSG msg;
    WNDCLASS wndClass;
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    DEVMODE DevMode;
    DWORD dwCurrentSettings = -1;
    BOOL bResult; 

    ZeroMemory(&wndClass, sizeof(WNDCLASS));

    wndClass.lpszClassName = TEXT("DesktopRectangle");
    wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);

    RegisterClass(&wndClass);

    ZeroMemory(&DevMode, sizeof(DEVMODE));

    DevMode.dmSize = sizeof(DEVMODE);

    do {
        dwCurrentSettings++;

        bResult = EnumDisplaySettings(NULL, dwCurrentSettings, &DevMode);

        if (!bResult) {
             dwCurrentSettings = -1;

             break;
        }
        else if (DevMode.dmPelsWidth == WIDTH && DevMode.dmPelsHeight == HEIGHT) {
            break;
        }
    } while (bResult);

    if (dwCurrentSettings == -1) {
        MessageBox(NULL, L"Display mode unavailable.", L"Error", MB_ICONERROR);
    }

    int rv = ChangeDisplaySettings(&DevMode, CDS_FULLSCREEN);

    if (rv != DISP_CHANGE_SUCCESSFUL) {
        MessageBox(NULL, L"Display mode could not be set", L"Error", MB_ICONERROR);

        exit(1);
    }

    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    hWnd =
        CreateWindow(
            TEXT("DesktopRectangle"), NULL,
            WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
            640, 480,
            NULL, NULL, hInstance, NULL
        );

    ShowWindow(hWnd, iCmdShow);
    UpdateWindow(hWnd);

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

    GdiplusShutdown(gdiplusToken);

    return msg.wParam;
}