1

Possible Duplicate:
How to effectively draw on desktop in C#?

I am trying to draw a rectangle on the desktop using the following code, but then the rectangle being drawn is getting erased on refreshing the screen or by opening any other window. am following the below code:

class Program {

    [DllImport("User32.dll")]
    static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("User32.dll")]
    static extern void ReleaseDC(IntPtr dc);

    static void Main(string[] args) {
        IntPtr desktop = GetDC(IntPtr.Zero);
        using (Graphics g = Graphics.FromHdc(desktop)) {
            g.FillRectangle(Brushes.Red, 0, 0, 100, 100);
        }
        ReleaseDC(desktop);
    }
}

Is there any way to resolve my issue so that the rectangle being drawn would stay persistently?...someone, please let me know the way to resolve it. Thanks in advance.

Community
  • 1
  • 1
user1105705
  • 89
  • 1
  • 1
  • 10
  • 3
    You need to redraw the rectangle every time the desktop is invalidated (...i.e. receives a Paint event). – Richard Ev Sep 05 '12 at 13:54
  • oh..yeah i have used the same logic in my application to achieve drawing a rectangle on desktop where as i need that rectangle to be persistent all the time...which I am unable to get now...Is there any way to resolve it?..the answer would be more helpful for me. – user1105705 Sep 05 '12 at 14:01
  • There isn't really a concept of persistent in this case. You will need to redraw the rectangle as required. – Richard Ev Sep 05 '12 at 14:06
  • You could read the wallpaper file, draw to it, then set the wallpaper to the new file. This technique would be tricky to manage if the wallpaper were changing often. Also, in a general consumer application this would be a terrible solution. But I have used this for my own in-house utility type apps and it works well if you understand what is going on (from a user perspective) – Sam Axe Sep 05 '12 at 14:11

0 Answers0