In order to write some text at the right hand end of the title bar, my program catches WM_NCPAINT
, gets the device context, calculates the position to write the text and then calls DrawText
. This code used to work, but with Windows 8 and Windows 10, it no longer works. It would appear you simply cannot paint onto the title bar in these newer versions of Windows.
The device context is obtained as follows using Win API (not MFC):-
HDC hDC = GetWindowDC(hwnd);
which is described in Windows Dev Center thus:-
The GetWindowDC function retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars. A window device context permits painting anywhere in a window, because the origin of the device context is the upper-left corner of the window instead of the client area.
There is no mention of this function being Windows version specific, but what it describes simply no longer works. Replacing the call to DrawText
with a big black rectangle (-300,-300,1000,1000) leaves the title bar beautifully untouched showing that painting the entire window rectangle is not possible.
I have tried instead obtaining the device context as follows:-
HDC hDC = GetDCEx(hwnd, (HRGN)wParam, DCX_WINDOW|DCX_INTERSECTRGN);
as described in the documentation for WM_NCPAINT
. So long as the window class is registered with one of CS_CLASSDC
, CS_OWNDC
or CS_PARENTDC
, then an hDC
is returned (if not zero is returned). But this hDC
has exactly the same issue.
I tried a variation on the above, because the clipping seemed dubious. I tried:-
HDC hDC = GetDCEx(hwnd, 0, DCX_WINDOW);
after all the documentation says of DCX_WINDOW
:-
Value: DCX_WINDOW
Meaning: Returns a DC that corresponds to the window rectangle rather than the client rectangle.
This device context demonstrates the same behaviour too.
How do I obtain a device context that allows me to DrawText
or indeed draw anything, on the title bar?