tags:

views:

1128

answers:

3

This question has been asked for other languages, and even for those other languages, I have found their answers lacking in how to exactly do it, cleanly (no messed up screen repaints, etc..).

Is it possible to draw onto the Windows Desktop from C#? I am looking for an example if possible.

+4  A: 

Try the following:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

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);
    }
}
Paolo Tedesco
FYI for those who say you can't, this did work for me, although moving any window over it repaints it immediately :(
esac
A: 

You can try:

Graphics.FromHwnd(IntPtr.Zero)
leppie
A: 

Graphics.FromHwnd(IntPtr.Zero) reutrns a null Pointer.

Bassam Alugili