• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1083
  • Last Modified:

Drawing over the desktop in C#

We are trying to draw over the desktop to implement an "annotation tool" (we record the screen, and we want to add some arrows over the desktop).  How this can be done in C#?

Ok, here's the things we have tried, and "pseudo-solutions" that we have found, but none of these is acceptable in our case, therefore your solution shouldn't include them:
Tried to set a global hook to WH_CALLWNDPROC. (Result: Failure... SetWindowsHookEx return 0
We have found a solution where it's mentionned that we could have a transparent form, covering all the desktop, and beeing set as "Top Most".  So we could draw on it.  But the problem with this solution is that we can't select our "Arrow Tool" to "return to normal mode", to be able to select a window to drag it, since the form would be top most and transparent)
We have tried to draw using the GetDC like the example shown bellow, but as soon as there's a window is moved over what we have drawn, what we have drawn dissappear
            hDC = GetDC(IntPtr.Zero);

            Graphics gfx = Graphics.FromHdc(hDC);
            gfx.DrawRectangle(Pens.Black, new Rectangle(10, 10, 200, 200));
            ReleaseDC(IntPtr.Zero, hDC);

Open in new window

Thanks for your help
Christian de Bellefeuille
Christian de Bellefeuille
  • 2
1 Solution
Am not really big on graphics, but how about taking a screenshot of the desktop from c# (http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html), drawing that image onto a borderless maximised form, say in a picture box even. Set the form to minimise once it loses focus - to restore normal desktop behaviour. That will ensure that you remain with the mousepointer etc, since it's all just a normal form. you user "wont know" they are now looking at a screenshot of the desktop.

other than that, you will have to perhaps custom draw the mouse pointer on your invisible form etc etc.
Christian de BellefeuilleProgrammerAuthor Commented:
There's one flaw, if you use an Eraser tool.

I've found the solution to my problem this morning, i didn't had the time to come here to update the thread so i'ill give you the points for the help you provided.

Here's the solution i've found for those who are interrested:
- I've created 2 forms
- One of them has OPACITY set to 100%, but it's TransparencyKey is set to the form background.  This is my "Drawing Layer", and it contain the controls of my form that i've placed inside a docked toolbar.
- One minor problem when you set the TransparencyKey is that your form doesn't trigger any mouse event (other than on the toolbar), since nothing is displayed.  To solve this problem, i've used a MouseHook that i've created a week ago.
- Another problem with this solution is that with the MouseHook, my app is advised of any mouse activity, but I've found no way to prevent the other applications (such as Explorer.exe) to be notified.
- Here come the 2nd Form.  It's opacity is set to 0.5% (almost invisible, but still there, enough to "trap" all the mouse events to prevent that explorer.exe or any other app catch them).  I've called this form my "BlockingLayer".
- As soon as i click my "Mouse Pointer" in my toolbar, i hide the blocking layer and things under the Drawing become accessible.

It's probably not the best way, but so far in C#, it's the best i've found.

And with my method, i can set the brush color to the form TransparencyKey color.  When i use it, it draw the "transparent color" over in the DrawingLayer, and Windows do the rest and update what's bellow...

I already had the full class for Mouse & Keyboard (global & local) hooks written, so it was not so thought, but for someone who got to do that from scratch, it would be a pain in the ... :)
Oh cool! Glad you got it to work. Thanks for the points :)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now