Ludovic Baudin
Ludovic Baudin

Reputation: 23

Draw a fill rectangle dynamically on screen in C#

I would like to draw a fill rectangle dynamically on my screen, with an opacity at 0.1. The problem is that when i move the mouse, the previous rectangles aren't clear.

This is the drawing methods.

    private void OnMouseDown(object sender, MouseEventArgs e)
    {
        isMouseDown = true;
        x = e.X;
        y = e.Y;

        g = this.selectorForm.CreateGraphics();
    }

    private void OnMouseMove(object sender, MouseEventArgs e)
    {
        if (!isMouseDown) return;

        this.selectorForm.Invalidate();
        g.FillRectangle(brush, this.getRectangle(x, y, e.X, e.Y));
        g.DrawRectangle(pen, this.getRectangle(x, y, e.X, e.Y));

    }

This is my selectorForm

    internal class SelectorForm : Form
    {
        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }
    }

An example when I draw a rectangle (several overlapping rectangles)

And Invalidate() doesn't work because I override OnPaintBackground. But if I don't do this override, when I do this.selectorForm.Show(), my screen becomes gray.

So how can I draw a rectangle with an opacity 0.1 on my screen?

Thank you !

Upvotes: 2

Views: 5709

Answers (2)

TaW
TaW

Reputation: 54453

This is an example that works for me.

The crucial parts are:

  • using the Paint event and its Graphics
  • adding a Clear(BackgroundColor) or else I get the same artifacts you see
  • for transparency the TransparencyKey property should be used. There is a certain choice of colors:
    • Common colors may conflict with other things on the Form
    • Fuchsia works if you want to click through the Form
    • Other not so common colors are suitable; I chose a light green

You may want to adapt to your way of setting up events, I just did it this way for faster testing.

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        DoubleBuffered = true;
        Opacity = 0.1f;
        // a color that will allow using the mouse on the form:
        BackColor = Color.GreenYellow;
        TransparencyKey = BackColor;
    }

    Point mDown = Point.Empty;
    Point mCur = Point.Empty;

    private void Form2_MouseDown(object sender, MouseEventArgs e)
    {
        mDown = e.Location;
    }

    private void Form2_MouseUp(object sender, MouseEventArgs e)
    {
        mDown = Point.Empty;
    }

    private void Form2_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
        mCur = e.Location;
        Invalidate();
    }

    private void Form2_Paint(object sender, PaintEventArgs e)
    {
       if (mDown == Point.Empty) return;
       Size s = new System.Drawing.Size(Math.Abs(mDown.X - mCur.X),
                                        Math.Abs(mDown.Y - mCur.Y)  );
       Point topLeft = new Point(Math.Min(mDown.X, mCur.X), 
                                 Math.Min(mDown.Y, mCur.Y));
       Rectangle r = new Rectangle(topLeft, s);
       e.Graphics.Clear(this.BackColor);                // <--- necessary!
       e.Graphics.FillRectangle(Brushes.Bisque, r );   // <--- pick your..
       e.Graphics.DrawRectangle(Pens.Red, r);         // <--- colors!
    }
}

}

Upvotes: 3

Yuri Dorokhov
Yuri Dorokhov

Reputation: 726

You can try following code:

    g.Clear();
    g.FillRectangle(brush, this.getRectangle(x, y, e.X, e.Y));
    g.DrawRectangle(pen, this.getRectangle(x, y, e.X, e.Y));

Upvotes: 0

Related Questions