Drawing on Desktop
-
I have an interesting behavior: [DllImport("user32.dll")] static public extern IntPtr GetDC(IntPtr hWnd); [DllImport("user32.dll")] static public extern IntPtr GetDesktopWindow(); When I get the Graphics object through Graphics g = Graphics.FromHwnd(GetDesktopWindow()); any drawing on the desktop do not result in anything. When I get the Graphics object through Graphics g = Graphics.FromHdc(GetDC(IntPtr.Zero)); I can draw on the desktop. Any explanation? Juergen
-
I have an interesting behavior: [DllImport("user32.dll")] static public extern IntPtr GetDC(IntPtr hWnd); [DllImport("user32.dll")] static public extern IntPtr GetDesktopWindow(); When I get the Graphics object through Graphics g = Graphics.FromHwnd(GetDesktopWindow()); any drawing on the desktop do not result in anything. When I get the Graphics object through Graphics g = Graphics.FromHdc(GetDC(IntPtr.Zero)); I can draw on the desktop. Any explanation? Juergen
i did it by accident a few weeks ago and im not sure if this is the code i used but i think you could give it a shot // okay i remeber how i did it..i originally posted a message here that was long..but i found the solution. you can do it using a class that was already wrote..and ill show you how... first off download the clas here : Link[^] second....adapt this code segment i just wrote into your needs...and it should work....remeber to add the namespaces that the class you downloaded uses... also the class you downloaded uses unsafe code so you will have to turn on the unsafe option in vs.net you can do it by selecting the properties of your project
//api calls [DllImport("user32.dll")] private static extern IntPtr GetWindowDC(int Pointer); //method Win32Window ww = Win32Util.Win32Window.DesktopWindow; IntPtr DC = GetWindowDC(ww.Window.ToInt32()); Graphics jm = Graphics.FromHdc(DC); jm.DrawLine(Pens.Yellow,130,30,30,130); jm.DrawRectangle(Pens.Red,100,100,300,300);
-
i did it by accident a few weeks ago and im not sure if this is the code i used but i think you could give it a shot // okay i remeber how i did it..i originally posted a message here that was long..but i found the solution. you can do it using a class that was already wrote..and ill show you how... first off download the clas here : Link[^] second....adapt this code segment i just wrote into your needs...and it should work....remeber to add the namespaces that the class you downloaded uses... also the class you downloaded uses unsafe code so you will have to turn on the unsafe option in vs.net you can do it by selecting the properties of your project
//api calls [DllImport("user32.dll")] private static extern IntPtr GetWindowDC(int Pointer); //method Win32Window ww = Win32Util.Win32Window.DesktopWindow; IntPtr DC = GetWindowDC(ww.Window.ToInt32()); Graphics jm = Graphics.FromHdc(DC); jm.DrawLine(Pens.Yellow,130,30,30,130); jm.DrawRectangle(Pens.Red,100,100,300,300);
Thanks for your reply, in my post there already was a working solution. But the question was why the first solution don't work
Graphics g = Graphics.FromHwnd(GetDesktopWindow());
while the second solution works fine.Graphics g = Graphics.FromHdc(GetDC(IntPtr.Zero));
In my understanding both should work. I like to know the reason for this behaviour. Juergen -
Thanks for your reply, in my post there already was a working solution. But the question was why the first solution don't work
Graphics g = Graphics.FromHwnd(GetDesktopWindow());
while the second solution works fine.Graphics g = Graphics.FromHdc(GetDC(IntPtr.Zero));
In my understanding both should work. I like to know the reason for this behaviour. JuergenGetDesktopWindow() only returns the handle to the Device Context (DC) so passing that handle into the graphics object is incorrect. when you call GetDC you are grapping the device context its self from the handle. The device is what you work with to do your drawing function in this case. hope this helps. Jesse The Code Project Is Your Friend...
-
GetDesktopWindow() only returns the handle to the Device Context (DC) so passing that handle into the graphics object is incorrect. when you call GetDC you are grapping the device context its self from the handle. The device is what you work with to do your drawing function in this case. hope this helps. Jesse The Code Project Is Your Friend...
The GetDesktopWindow function returns a handle to the desktop window (MSDN).
Graphics g = Graphics.FromHwnd(GetDesktopWindow());
creates the Graphics object. In the debugger it looks like g is created properly, (exactly the same values as when created with Graphics.FromHdc(GetDC(IntPtr.Zero));) but any Drawing on it, is at least invisible. Juergen -
The GetDesktopWindow function returns a handle to the desktop window (MSDN).
Graphics g = Graphics.FromHwnd(GetDesktopWindow());
creates the Graphics object. In the debugger it looks like g is created properly, (exactly the same values as when created with Graphics.FromHdc(GetDC(IntPtr.Zero));) but any Drawing on it, is at least invisible. Juergenit returns the handle to the window its self...but that doesnt necessarily mean the graphics object. the window propertie in the api is kinda like a form in .net it has many methods/properties to it but you need the Graphics object to draw to the form. Simular to that you need the device context of the 'Window' to draw to it. g would be created properly because it is a handle and it can be drawn to..its just not creating the handle on the correct drawing object. there are 2 handles....a Form.Handle and Graphics().GetHdc() (i think thats it) GetDesktopWindow() returns the Form.Handle essentially. from that handle u get the GraphicsHandle from GetDC jesse The Code Project Is Your Friend...