Search code examples
windowswinapidrawinggdiwin32gui

Failed to draw on DesktopWindow


I've tried to draw any thing on Desktop window using GetDC(GetDesktopWindow), like the following simple program:

#include <windows.h>

int main()
{
    TextOut(GetDC(GetDesktopWindow()), 10, 10, TEXT("Test TextOut Tester!!"), 21);
    return 0;
}

It seems that my current user privileges affect the drawing behavior, I am not admin on my PC, is this the reason for that? is there any documentation for this issue ? Thanks in advance


Solution

  • The simple reason you can't draw on the desktop window like this is you cannot actually see the desktop window. Since Windows 95 the desktop window has been completely obscured by a cluster of windows owned by explorer.

    The DC you get when you call GetDC(GetDesktopWindow()) will thus be completely clipped.

    If you want to draw directly on the display GetDC(NULL) will give you a DC that you can use to draw all over the desktop and visible windows. But that, as has been mentioned, will be operating entirely outside Windows' repainting logic and the results will be, well, ugly and unsuited to any real purpose (other than, say, getting some kind of debug feedback from a windowless app you are in the process of developing).

    Most applications that want do "display something on the desktop" do so by creating a window and drawing on that. Why is that not appropriate here?