#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// Draw a red pixel
SetPixel(hdc, 50, 50, RGB(255, 0, 0));
// Read the pixel back
COLORREF c = GetPixel(hdc, 50, 50);
if (c != CLR_INVALID) {
int r = GetRValue(c);
int g = GetGValue(c);
int b = GetBValue(c);
wchar_t title[128];
swprintf(title, 128, L"GetPixel Example - R=%d G=%d B=%d", r, g, b);
SetWindowText(hWnd, title);
}
EndPaint(hWnd, &ps);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR pCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"GetPixelDemo";
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hWnd = CreateWindowEx(
0,
CLASS_NAME,
L"GetPixel Example",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
640, 480,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hWnd, nCmdShow);
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
/*
run:
GetPixel Example - R=255 G=0 B=0
*/