Draw on the screen without a form
Yes it is possible to draw to the screen but it may be easier to use a topmost, borderless form.
You can also do this from a console application, if you must, providing you reference the necessary assemblies but this would result in a console window remaining on screen for the life of the application.
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
Alternatively I believe you can create an instance of Window
and call Show
on that.
This answer to a different question explains how to use GDI+ calls to draw directly to the screen.
Method 1: Call the Windows API
You need System.Drawing
and System.Runtime.InteropServices
. You may need to add project references to them.
using System.Runtime.InteropServices;
using System.Drawing;
Add the methods to your class with P/Invoke
[DllImport("User32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);
Get a Graphics
object for the entire screen and draw a rectangle with it:
IntPtr desktopPtr = GetDC(IntPtr.Zero);
Graphics g = Graphics.FromHdc(desktopPtr);
SolidBrush b = new SolidBrush(Color.White);
g.FillRectangle(b, new Rectangle(0, 0, 1920, 1080));
g.Dispose();
ReleaseDC(IntPtr.Zero, desktopPtr);
The problem with this method is that if the screen refreshes at all, the rectangle will be overwritten, making it useless for most practical applications.
Method 2: Create a borderless form
As before, you need a project reference. This time to System.Windows.Forms
. You'll also need System.Drawing
again:
using System.Drawing;
using System.Windows.Forms;
Make the new form, remove its borders, fill the screen with it, and put it on top of the taskbar:
Form f = new Form();
f.BackColor = Color.White;
f.FormBorderStyle = FormBorderStyle.None;
f.Bounds = Screen.PrimaryScreen.Bounds;
f.TopMost = true;
Application.EnableVisualStyles();
Application.Run(f);
A possible issue with this is that the user can just alt+tab away from the window. If you want to do any more complicated graphics, you'll need to write some drawing code like this. To make the form background transparent, set its TransparentKey
to the same as its Backolor
.
I've just tested both of these in .NET 4.5 and Windows 7, so it may be different for earlier versions. More information here and here.