How to draw by Desktop window DC?

  • Thread starter Thread starter °Ë´óɽÈË
  • Start date Start date
°

°Ë´óɽÈË

Hi,

I create a graphics object for desktop window by following step:
IntPtr hWnd = GetDesktopWindow(); // API
Graphics g = Graphics.FromHWnd(hWnd);

then draw the screen by g, but I can't draw anything.

WHY?? And I try to draw by DC (get by GetDC), it's fail either.
 
Hi,

GetDesktopWindow returns handle to a window which is under the desktop you
see (the one with the icons).This is a window which is parent of all other
top level widows, and normaly you don't see it. So, the drawing might work
but you can't see the result. If you wnat to draw on the screen use GetDC
method passing NULL for HWND. Then use Graphics.FromHdc to create a graphics
object.
 
Yes, I think you are right. I write a program like you see, It works. But I
find ControlPaint.DrawReversibleLine realized by GetDesktopWindow. Why does
it
work?


Stoitcho Goutsev (100) said:
Hi,

GetDesktopWindow returns handle to a window which is under the desktop you
see (the one with the icons).This is a window which is parent of all other
top level widows, and normaly you don't see it. So, the drawing might work
but you can't see the result. If you wnat to draw on the screen use GetDC
method passing NULL for HWND. Then use Graphics.FromHdc to create a graphics
object.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


°Ë´óɽÈË said:
Hi,

I create a graphics object for desktop window by following step:
IntPtr hWnd = GetDesktopWindow(); // API
Graphics g = Graphics.FromHWnd(hWnd);

then draw the screen by g, but I can't draw anything.

WHY?? And I try to draw by DC (get by GetDC), it's fail either.
 
Well, PaintControl indeed uses GetDesktopWindow, but it uses GetDCEx as
well.

When you use Graphics.FormHwnd the Graphics object that this method returns
clips all the child and sibling controls so you can paint over them. Because
the desktop window is fully covered, by its children all the drawing is
clipped. If you want to use GetDesktopWindow you have to use GetDCEx in
order to tell the GDI not to clip the children and siblings.

So, the code bellow works

[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();

[DllImport("user32.dll")]
static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgn, uint flags);

.......
IntPtr hdc = GetDCEx(GetDesktopWindow(), IntPtr.Zero, 1027);
using(Graphics g = Graphics.FromHdc(hdc))
{
g.FillEllipse(Brushes.Red, 0, 0, 400, 400);
}

......


--

HTH
Stoitcho Goutsev (100) [C# MVP]


°Ë´óɽÈË said:
Yes, I think you are right. I write a program like you see, It works. But I
find ControlPaint.DrawReversibleLine realized by GetDesktopWindow. Why does
it
work?


Stoitcho Goutsev (100) said:
Hi,

GetDesktopWindow returns handle to a window which is under the desktop you
see (the one with the icons).This is a window which is parent of all other
top level widows, and normaly you don't see it. So, the drawing might work
but you can't see the result. If you wnat to draw on the screen use GetDC
method passing NULL for HWND. Then use Graphics.FromHdc to create a graphics
object.

--
HTH
Stoitcho Goutsev (100) [C# MVP]


°Ë´óɽÈË said:
Hi,

I create a graphics object for desktop window by following step:
IntPtr hWnd = GetDesktopWindow(); // API
Graphics g = Graphics.FromHWnd(hWnd);

then draw the screen by g, but I can't draw anything.

WHY?? And I try to draw by DC (get by GetDC), it's fail either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top