3
votes

I've been trying to mimic the Windows 7 Snipping Tool in how it overlays the screen with a semi-transparent gray layer which becomes completely transparent inside the selection area. I've come pretty close. I'm displaying a borderless 50% opaque gray form which covers the entire screen and has a TransparencyKey of Fuchsia. Then on top of that form I draw 2 rectangles. A solid fuchsia rectangle for the transparency and another rectangle for the red border. It works, but only if I do one of three things, none of which are options.

  1. Disable double buffering which makes the form flicker while drawing
  2. Change the desktop color mode to 16bit from 32bit
  3. Make the form 100% opaque

Here is my code. Any suggestion on how to get this to work?

public partial class frmBackground : Form
{
    Rectangle rect;

    public frmBackground()
    {
        InitializeComponent();

        this.MouseDown += new MouseEventHandler(frmBackground_MouseDown);
        this.MouseMove += new MouseEventHandler(frmBackground_MouseMove);
        this.Paint += new PaintEventHandler(frmBackground_Paint);
        this.DoubleBuffered = true;
        this.Cursor = Cursors.Cross;
    }

    private void frmBackground_MouseDown(object sender, MouseEventArgs e)
    {
        Bitmap backBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
        rect = new Rectangle(e.X, e.Y, 0, 0);
        this.Invalidate();
    }

    private void frmBackground_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
            rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);

        this.Invalidate();
    }

    private void frmBackground_Paint(object sender, PaintEventArgs e)
    {
        Pen pen = new Pen(Color.Red, 3);
        e.Graphics.DrawRectangle(pen, rect);

        SolidBrush brush = new SolidBrush(Color.Fuchsia);
        e.Graphics.FillRectangle(brush, rect);
    }
}
1

1 Answers

0
votes

you can use

Brush brush = new SolidBrush(Color.FromArgb(alpha, red, green, blue))

where alpha goes from 0 to 255, so a value of 128 for your alpha will give you 50% opactity.

this solution was found in this question