-1

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

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Mostafa Alayesh
  • 111
  • 1
  • 9
  • 1
    The issue is, that the desktop window does not belong to you. There is no contractual documentation that indicates, that this were a supported scenario. The GDI is not governed by privileges. What you are doing fails for another reason. Complete absence of error handling isn't going to be helpful in finding out. – IInspectable May 23 '16 at 09:52
  • What should I do? if I tried to `TextOut` to a created window, it success, but it fails when do it on the desktop window, so what is the problem ? even though the returned value from `GetDC` and `GetDesktopWindow` is not zero. – Mostafa Alayesh May 23 '16 at 10:49
  • 1
    Doing this is difficult to do, is non persistent between redraws and 99.9% of the time a terrible idea with better alternatives (You should state you ultimate goal). The GetDesktopWindow() window is not the window you see that contains icons, wallpaper etc, thats a syslistview you would need to locate starting from GetShellWindow(). – Alex K. May 23 '16 at 11:21
  • I have used to draw stuff on the desktop window on my PC, but in the office (where I am not the admin) the mentioned problem appeared, I am sure to use all GDI stuff on my PC, like `SetPixel` and `TextOut`, but in my current sotuation it is not working. – Mostafa Alayesh May 23 '16 at 12:09
  • 1
    You can't draw on the desktop. It isn't yours. Stop trying to draw on somebody else's window! Create a window. Draw on that. – David Heffernan May 23 '16 at 12:55
  • See the [answer here](http://stackoverflow.com/a/1399704/5240004) for two ways to add content to the desktop. – theB May 23 '16 at 13:11
  • Dears, as many people asked `what is the reason for doing such a thing`, the answer is "Educational purposes", actually I want to show how is "getting something drawn" is easy with winapi. thank you – Mostafa Alayesh May 24 '16 at 10:11

2 Answers2

1

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?

Chris Becke
  • 34,244
  • 12
  • 79
  • 148
-1

This is what you should do:

HDC hdc = ::GetDC(NULL);

//draw on the desktop using the hdc

::ReleaseDC(NULL, hdc);
AlphaP8
  • 17
  • 2
  • No don't do that. It will only get erased in the next paint cycle. And it's not your window to draw on anyway. – David Heffernan May 23 '16 at 20:13
  • I am amazed by the fact that you gave me a negative rank after all !!! Sometimes is it very desired to draw like this, specifically when you draw using ::SetROP2 with a R2_XORPEN raster operation on the desktop. The fact is that this is how it's done, nobody asked why it should be done! This is a shame. – AlphaP8 May 31 '16 at 11:40