0

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.

  • Use a colour with an alpha very close to but not quite zero. The user won't be able to see it but the system will. – jmcilhinney May 05 '23 at 07:19
  • Thanks for quick response @jmcilhinney. If I try this `this.BackColor = Color.FromArgb(10, 255, 255, 255);` I get an error. i need to add this `SetStyle(ControlStyles.SupportsTransparentBackColor, true);` but that doesen't work either. it's just all white. What did I do wrong? – Nicocosaurus May 05 '23 at 07:41
  • https://stackoverflow.com/a/32783251/14171304 & https://stackoverflow.com/q/4448771/14171304 – dr.null May 05 '23 at 09:56
  • @dr.null. this is a good point. the problem here is that the outline of my line (the outer 1-2 pixels) are the color which is -or should be- transparent. I'm working in a better solution but this works. Thanks. Oh and remember that the red and blue channel can't be the same. – Nicocosaurus May 05 '23 at 12:45
  • Note, no anti-aliasing with `Region`. Your second code box is completely wrong. You can't set `SupportsTransparentBackColor` style nor `Color.Transparent` for a Form. Also, don't do this `this.CreateGraphics()`. Take the points from the mouse events and override the Form's `OnPaint` method to draw using the provided device context. With your code as is, draw something and minimize then restore the Form to see the result. – dr.null May 05 '23 at 13:34
  • See also [Layered Windows](https://stackoverflow.com/a/33531201/14171304). – dr.null May 05 '23 at 13:38

0 Answers0