Drawing on the desktop

  • Thread starter Thread starter Jon Shemitz
  • Start date Start date
J

Jon Shemitz

I'm writing a docking window control, and need to write on the
desktop, so I can show a candidate dock rectangle on top of the dock
manager's child controls.

I found

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

and

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

but when I do

IntPtr Handle = DesktopWindow();
Graphics Desktop = Graphics.FromHdc(Handle);
Desktop.ReleaseHdc(Handle);

private IntPtr DesktopWindow()
{
IntPtr Result = GetShellWindow();
if (Result != IntPtr.Zero)
return Result;
return GetDesktopWindow();
}

in an OnPaint or OnMouseMove handler, I get an "Out of memory"
exception - even when I don't draw anything!

Google shows me that others have this problem - but doesn't show me a
solution ....
 
GetDesktopWindow returns a window handle, not an hDC so that's why the
Graphics.FromHdc fails.

Import GetDC instead and use...

IntPtr deskDC=GetDC(IntPtr.Zero);
Graphics g=Graphics.FromHdc(deskDC);
//draw on the desktop here
g.Dispose();

Alternatively you can use the ControlPaint.DrawReversibleFrame method which
draws on the desktop automatically.

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com
 
Bob Powell said:
GetDesktopWindow returns a window handle, not an hDC so that's why the
Graphics.FromHdc fails.

Import GetDC instead and use...

IntPtr deskDC=GetDC(IntPtr.Zero);
Graphics g=Graphics.FromHdc(deskDC);
//draw on the desktop here
g.Dispose();

Thanks - that works just fine.

// As does
IntPtr deskDC=GetDC(IntPtr.Zero);
using (Graphics g=Graphics.FromHdc(deskDC))
{
}

Now I have to get the bugs out of my rectangle code, but that's much
more straightforward.
 
hi sir,

i need to know how to capture the desktop as the video or i need to view as live desktop in application which should be created by me

and i am not able to find any codings.
i dont require any encodings things and i am not able to trace the encodings as i am new to c #. help me :(
 
Back
Top