Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Drawing on Desktop

Drawing on Desktop

Scheduled Pinned Locked Moved C#
graphicsquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Juergen
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • J 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

      J Offline
      J Offline
      jtmtv18
      wrote on last edited by
      #2

      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);

      J 1 Reply Last reply
      0
      • J jtmtv18

        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);

        J Offline
        J Offline
        Juergen
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • J 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. Juergen

          J Offline
          J Offline
          jtmtv18
          wrote on last edited by
          #4

          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...

          J 1 Reply Last reply
          0
          • J jtmtv18

            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...

            J Offline
            J Offline
            Juergen
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • J 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. Juergen

              J Offline
              J Offline
              jtmtv18
              wrote on last edited by
              #6

              it 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...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups