tags:

views:

203

answers:

4

Hi,

Could someone provide an example of drawing graphics without a windows form? I have an app that doesn't have a console window or windows form, but i need to draw some basic graphics (lines and rectangles etc.)

Hope that makes sense.

Thanks in advance.. J

A: 

What is point in drawing without display ? Are you looking for a control library project ?

Seb
Im doing a mouse gestures app so i want to draw to the screen without having a visible window.
teishu
A: 

The question is a little unfocused. Specifically - where do you want to draw the lines and rectangles? Generally speaking, you need a drawing surface, usually provided by a windows form.

Where does the need to avoid windows forms come from?

Are you using another kind of window?

For a windows form you could use code similar to this:

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);

            e.Graphics.DrawLine(new Pen(Color.DarkGreen), 1,1, 3, 20 );
            e.Graphics.DrawRectangle(new Pen(Color.Black), 10, 10, 20, 32 );
        }
    }
}

You can generally do this with any object that lets you get a handle for a "Graphics" object (like a printer).

Hershi
+3  A: 

This should give you a good start:

  [TestFixture]
  public class DesktopDrawingTests {
    private const int DCX_WINDOW = 0x00000001;
    private const int DCX_CACHE = 0x00000002;
    private const int DCX_LOCKWINDOWUPDATE = 0x00000400;

    [DllImport("user32.dll")]
    private static extern IntPtr GetDesktopWindow();

    [DllImport("user32.dll")]
    private static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgn, uint flags);

    [Test]
    public void TestDrawingOnDesktop() {
      IntPtr hdc = GetDCEx(GetDesktopWindow(),
                           IntPtr.Zero,
                           DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);

      using (Graphics g = Graphics.FromHdc(hdc)) {
        g.FillEllipse(Brushes.Red, 0, 0, 400, 400);
      }
    }
  }
stmax
This will be a flicker-hell
Qua
A: 

Right, the way i've done it is with a windows form, but make the background transparent, and then get rid of all the borders...

Thanks for the replys anyway..

J

teishu