Morag Hughson
Morag Hughson

Reputation: 7527

DrawText on Windows Title Bar no longer works

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?

Upvotes: 1

Views: 1134

Answers (1)

Olaf Hess
Olaf Hess

Reputation: 1483

Windows Vista introduced the Aero theme and with it the Desktop Windows Manager: The desktop composition feature, introduced in Windows Vista, fundamentally changed the way applications display pixels on the screen. When desktop composition is enabled, individual windows no longer draw directly to the screen or primary display device as they did in previous versions of Windows. Instead, their drawing is redirected to off-screen surfaces in video memory, which are then rendered into a desktop image and presented on the display.

The article Custom Window Frame Using DWM demonstrates how to use the Desktop Window Manager (DWM) APIs to create custom window frames for your application. This includes drawing text on a window's title bar. Windows Vista and Windows 7 allowed the user to turn the Aero theme off to allow a program to manipulate a program's title bar the same way as in XP and earlier versions. Starting with Windows 8, desktop composition is always enabled.

Upvotes: 2

Related Questions