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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Drawing on the Screen

Drawing on the Screen

Scheduled Pinned Locked Moved C#
graphicscsswinformsquestionlearning
12 Posts 5 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.
  • A albean

    I'm trying to draw directly on the screen. Whatever is drawn needs to be "above" the controls on the form. And, of course, I need to erase whatever is drawn to restore the way things looked before I did my drawing. I looked at the "Reversible" drawing functions on the ControlPaint object but these don't give me the control over the color that I need. (I really only need Black, White, and a dotted grid of Gray.) What should I be looking at now to get GDI+ to work with me on this? ----- I'm essentially trying allow users to select controls. If one control overlaps another I still want the selection cues to appear atop the covering control.

    A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #2

    With DirectDraw you are able to draw directly on the screen!

    A 1 Reply Last reply
    0
    • A Anonymous

      With DirectDraw you are able to draw directly on the screen!

      A Offline
      A Offline
      albean
      wrote on last edited by
      #3

      Really all I'm trying to do is recreate Visual Studio's form desinger. I think adding DirectDraw would be overkill.

      A 2 Replies Last reply
      0
      • A albean

        Really all I'm trying to do is recreate Visual Studio's form desinger. I think adding DirectDraw would be overkill.

        A Offline
        A Offline
        Alex Korchemniy
        wrote on last edited by
        #4

        You got me interested! What do you mean recreate VisualStudio's form designer? I think there is a way you can just import the functionality from a the original libary. I think its located in C:\Program Files\Microsoft Visual Studio .NET\Common7\IDE\Microsoft.VSDesigner.dll. If you want to do it another way, it can get very messy very fast. Ie. if you draw directly on the screen you then have to do hooks to handle the mouse events... Good luck to you. By the way, ASP WebMatrix did an ASP think like this. They have a VisualStudio like UI, with with the ASP webforms designer surface.

        1 Reply Last reply
        0
        • A albean

          Really all I'm trying to do is recreate Visual Studio's form desinger. I think adding DirectDraw would be overkill.

          A Offline
          A Offline
          Alex Korchemniy
          wrote on last edited by
          #5

          To answer your question directly, this is how you can draw on the screen: System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(System.IntPtr.Zero); -or, something like this- IntPtr handle = System.IntPtr.Zero; System.Drawing.Graphics g = System.Drawing.Graphics.FromHdc(handle); -- Basicly you need to tell it you use null as the handle. This creates a graphics object that draw on top of everything. But, it can be very messy.

          A 1 Reply Last reply
          0
          • A Alex Korchemniy

            To answer your question directly, this is how you can draw on the screen: System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(System.IntPtr.Zero); -or, something like this- IntPtr handle = System.IntPtr.Zero; System.Drawing.Graphics g = System.Drawing.Graphics.FromHdc(handle); -- Basicly you need to tell it you use null as the handle. This creates a graphics object that draw on top of everything. But, it can be very messy.

            A Offline
            A Offline
            albean
            wrote on last edited by
            #6

            AK, I tried both suggestions. The second throws an exception and the first doesnt crash but it doesn't do anything. Can you give me a link to a web page about using the null pointer to gain access to the screen? Thanks.

            L 1 Reply Last reply
            0
            • A albean

              I'm trying to draw directly on the screen. Whatever is drawn needs to be "above" the controls on the form. And, of course, I need to erase whatever is drawn to restore the way things looked before I did my drawing. I looked at the "Reversible" drawing functions on the ControlPaint object but these don't give me the control over the color that I need. (I really only need Black, White, and a dotted grid of Gray.) What should I be looking at now to get GDI+ to work with me on this? ----- I'm essentially trying allow users to select controls. If one control overlaps another I still want the selection cues to appear atop the covering control.

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #7

              I've never tried this, so it may or may not work but here goes :-) Create a control the same size as the Form (a panel would do), now when you want to draw your grid or whatever above the Form you do several things.... form will be the Form you are trying to draw on top of parent will be the panel or whatever that the Form is sitting on top of Graphics g = parent.CreateGraphics(); PaintEventArgs e = new PaintEventArgs(e, form.Bounds); InvokePaint(form, e); // At this point the form has painted itself // to the graphics object // Do your drawing here using the Graphics object, g James "And we are all men; apart from the females." - Colin Davies

              A 1 Reply Last reply
              0
              • J James T Johnson

                I've never tried this, so it may or may not work but here goes :-) Create a control the same size as the Form (a panel would do), now when you want to draw your grid or whatever above the Form you do several things.... form will be the Form you are trying to draw on top of parent will be the panel or whatever that the Form is sitting on top of Graphics g = parent.CreateGraphics(); PaintEventArgs e = new PaintEventArgs(e, form.Bounds); InvokePaint(form, e); // At this point the form has painted itself // to the graphics object // Do your drawing here using the Graphics object, g James "And we are all men; apart from the females." - Colin Davies

                A Offline
                A Offline
                albean
                wrote on last edited by
                #8

                James, Hmm, I've been trying a similar approach but I like yours better because there is only a single surface to draw on. My only question is will I need to make your panel Transparent? I ask because I’ve been trying to create a user control with the graphics drawn on it around the edges. Then I was going to make the background color transparent and place this usercontrol over the other control (lets say it is a button) already on the form. In this way the button would show threw and my usercontrol's graphics would indicate that the button has been selected; even though the user control completely covers the button. The only problem is that I can't make the usercontrol transparent!!! Will I have to make your panel transparent too. Because this just isn't working for me.

                J 1 Reply Last reply
                0
                • A albean

                  James, Hmm, I've been trying a similar approach but I like yours better because there is only a single surface to draw on. My only question is will I need to make your panel Transparent? I ask because I’ve been trying to create a user control with the graphics drawn on it around the edges. Then I was going to make the background color transparent and place this usercontrol over the other control (lets say it is a button) already on the form. In this way the button would show threw and my usercontrol's graphics would indicate that the button has been selected; even though the user control completely covers the button. The only problem is that I can't make the usercontrol transparent!!! Will I have to make your panel transparent too. Because this just isn't working for me.

                  J Offline
                  J Offline
                  James T Johnson
                  wrote on last edited by
                  #9

                  Here you go, a quick demo showing how its done :) It doesn't paint the non-client area but I think you can send some messages to windows to get it to do that (just need to call GetHdc/ReleaseHdc when passing it to windows). InvokePaintTest.zip (8.1 Kb) James "And we are all men; apart from the females." - Colin Davies

                  A 1 Reply Last reply
                  0
                  • A albean

                    AK, I tried both suggestions. The second throws an exception and the first doesnt crash but it doesn't do anything. Can you give me a link to a web page about using the null pointer to gain access to the screen? Thanks.

                    L Offline
                    L Offline
                    Li kai Liu Angus
                    wrote on last edited by
                    #10

                    Hi, I read AK's article and experimented with his second solution. It did throw an exception. And I modify it as follow and now it works: IntPtr hdc = GetDC(IntPtr.Zero); // get hdc for the screen Graphics g = Graphics.FromHdc(hdc); g.DrawLine(new Pen(Color.Blue), 0,0,500,500); // draw a line for testing. g.Dispose(); // <- I don't know if I should use g.Dispose or g.ReleaseHdc here... ReleaseDC(IntPtr.Zero,hdc); GetDC & ReleaseDC are Win32 API in user32.dll Hope this could help you:)

                    A 1 Reply Last reply
                    0
                    • L Li kai Liu Angus

                      Hi, I read AK's article and experimented with his second solution. It did throw an exception. And I modify it as follow and now it works: IntPtr hdc = GetDC(IntPtr.Zero); // get hdc for the screen Graphics g = Graphics.FromHdc(hdc); g.DrawLine(new Pen(Color.Blue), 0,0,500,500); // draw a line for testing. g.Dispose(); // <- I don't know if I should use g.Dispose or g.ReleaseHdc here... ReleaseDC(IntPtr.Zero,hdc); GetDC & ReleaseDC are Win32 API in user32.dll Hope this could help you:)

                      A Offline
                      A Offline
                      Alex Korchemniy
                      wrote on last edited by
                      #11

                      Sorry, I forgot to tell you to use GetDC. I knew I missed something.

                      1 Reply Last reply
                      0
                      • J James T Johnson

                        Here you go, a quick demo showing how its done :) It doesn't paint the non-client area but I think you can send some messages to windows to get it to do that (just need to call GetHdc/ReleaseHdc when passing it to windows). InvokePaintTest.zip (8.1 Kb) James "And we are all men; apart from the females." - Colin Davies

                        A Offline
                        A Offline
                        albean
                        wrote on last edited by
                        #12

                        Wow, thanks James :-D I tried out your sample just now and its pretty cool. I'm probably not going to use it though because I want to keep the controls on the same form BUT if the screen painting turns out to be a hassle I’ll definitely come back to your solution.

                        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