C#¶
Daniel Weschke
November 20, 2019
1 Desktop application - transparent, click-throgh, always on top, graphics¶
Visual Studio 2019 File / New / Project… / C# - Windows Forms-App (.NET Framework)
Note: .exe created with a “Windows Forms App (.NET Core)” project are not standalone, see also https://stackoverflow.com/a/44201386.
WindowsFormsApp1/WindowsFormsApp1/Form1.cs¶using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
// https://stackoverflow.com/a/11152884
this.TopMost = true; // make the form always on top
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; // hidden border
this.WindowState = FormWindowState.Maximized; // maximized (fullscreen; overflows the taskbar)
this.MinimizeBox = this.MaximizeBox = false; // not allowed to be minimized
//this.MinimumSize = this.MaximumSize = this.Size; // not allowed to be resized. this. size is not full size (e. g. 800x450) therefore see below
this.MinimumSize = this.MaximumSize; // not allowed to be resized
this.TransparencyKey = this.BackColor = Color.Red; // the color key to be transparent, choose a color that you do not use
}
// override CreateParams to set the Window Form click-through
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
// Set the form click-through
cp.ExStyle |= 0x80000 /* WS_EX_LAYERED */ | 0x20 /* WS_EX_TRANSPARENT */;
return cp;
}
}
// draw on the Windows Form
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// draw what you want
Pen p = new Pen(Brushes.Blue, 2);
int x = 1143;
int y = 623;
int w = 20;
e.Graphics.FillEllipse(Brushes.Blue, x - 4 / 2, y - 4 / 2, 4, 4);
e.Graphics.DrawEllipse(p, x - w / 2, y - w / 2, w, w);
}
}
}
Create .exe file by changing from Debug to Release and click
start. The .exe can be found in
WindowsFormsApp1/WindowsFormsApp1/bin/Release/WindowsFormsApp1.exe.