Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw graphics in C# without a form

I currently have a Console application. How would I draw graphics to the screen without having to have a form.

like image 726
Marcus S Avatar asked Oct 26 '11 00:10

Marcus S


3 Answers

EDIT - based on CuddleBunny's comment, I have created a class that will basically "draw graphics on the screen."

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
    class test : Form
    {
        public test() : base()
        {
            this.TopMost = true;
            this.DoubleBuffered = true;
            this.ShowInTaskbar = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.BackColor = Color.Purple;
            this.TransparencyKey = Color.Purple;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, 200, 200);
            this.Invalidate(); //cause repaint
        }
        public static void Main(String[] args)
        {
            Application.Run(new test());
        }
    }
}

Hope it helps.

old faulty answer


You can get the hwnd of another window and draw on that. I'm not sure how to draw on the entire screen though, I've always wondered that myself.

A simple example :

            Process p = Process.GetProcessById(0); //id of the process or some other method that can get the desired process
        using (Graphics g = Graphics.FromHwnd(p.MainWindowHandle))
        {
            g.DrawRectangle(Pens.Black, 0, 0, 100, 100);
        }
like image 54
Zhanger Avatar answered Oct 13 '22 01:10

Zhanger


You have to create a window of some kind to draw graphics to. You can't just draw directly to the screen.

like image 36
NickLH Avatar answered Oct 13 '22 02:10

NickLH


You can draw on the entire screen without a window using directx if you create a full screen directdrawsurface. The screen is all yours (no windows desktop at all).

like image 26
MartyTPS Avatar answered Oct 13 '22 03:10

MartyTPS