I am attempting to make a Winform application to draw on my screen. My objective is to launch the application, choose a pen and color (which I have not yet implemented), and subsequently draw on my screen. However, I am encountering an issue where, upon setting the form to be transparent, any attempts to draw on the form I just click on the application or desktop located behind it. This is what i have now:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace screendrawer1
{
public partial class Form1 : Form
{
// Define a region for the transparent area
private Region transparentRegion; private Color currentLineColor = Color.Blue;
private float currentLineWidth = 2.0f;
public Form1()
{
InitializeComponent();
// Set the form's initial properties
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.TopMost = true;
this.ShowInTaskbar = false;
// Set the form's opacity to a low value
this.Opacity = 0.5;
// Create a new region to represent the transparent area
transparentRegion = new Region(this.ClientRectangle);
// Subtract the button's region from the transparent area
Rectangle buttonRect = guna2ControlBox1.Bounds;
buttonRect.Offset(guna2ControlBox1.Location);
transparentRegion.Exclude(buttonRect);
// Bind the Form1_MouseMove method to the MouseMove event
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}
private Point? previousPoint = null;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
using (Graphics g = this.CreateGraphics())
{
Pen pen = new Pen(Color.Blue, 2); // Set the pen color and width
Point currentPoint = e.Location;
if (previousPoint != null)
{
g.DrawLine(pen, previousPoint.Value, currentPoint);
}
previousPoint = currentPoint;
}
}
else
{
previousPoint = null;
}
}
private void guna2ControlBox1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
I tried:
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
Which was not helpful because then I couldn't draw on the App. I just clicked trough it.