What is the shortest code for writing text / image in HWND

I do not need controls or anything else, I just need to write something on HWND, either in the center of the text or in the image that shows that it was shown. Actual use case - I get HWND at a point where I’m not ready to deal with it, so I want to display text on it, for example, “this window cannot be used” or graphics with a sad face, etc.

Nothing unusual, just raw hacker code, but all you have is HWND to work with pure Win32.

+3
source share
2 answers
HDC hdc = GetDC(hwnd);
RECT rect;
GetClientRect(hwnd, &rect);
char * text = "this Window cannot be used";
DrawTextA(hdc, text, strlen(text), &rect, DT_CENTER | DT_VCENTER);
ReleaseDC(hdc);

You can choose a different font before drawing text, but this will get you started.

+7
source

It is actually quite simple.

// Grab the window dimensions.
RECT bounds;
GetClientRect(hwnd, &bounds);

// Grab a DC to draw with.
HDC hdc = GetDC(hwnd);

// The money shot!
DrawText(hdc, messageText, -1, &bounds, DT_CENTER | DT_VCENTER);

// Now give back the borrowed DC.
ReleaseDC(hdc);
+6
source

Source: https://habr.com/ru/post/1793150/


All Articles