What is the shortest code to write text/image to a HWND
I don't need to have controls or anything, I just need to write something onto a HWND, either centred text or an image, that shows it has been rendered to. The actual use-case is I get passed a HWND at a point I'm not ready to deal with it, so I want to to display text on it like "this Window cannot be used", or a sad-face graphics, et开发者_开发百科c.
Nothing fancy, just raw hacky code but all you have is the HWND to work with, in pure Win32.
That's actually fairly straightforward.
// 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);
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 might want to select a different font before you draw the text, but this will get you started.
精彩评论