Drawing over the desktop in C#

Posted on 2011-04-20
Last Modified: 2012-05-11
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
Question by:cdebel
    LVL 30

    Accepted Solution

    Am not really big on graphics, but how about taking a screenshot of the desktop from c# (, 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.
    LVL 10

    Author Comment

    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 ... :)
    LVL 30

    Expert Comment

    Oh cool! Glad you got it to work. Thanks for the points :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now