C#在50%不透明形状上绘制的透明实心矩形

问题描述 投票:2回答:1

我一直试图模仿Windows 7 Snipping Tool如何用一个半透明的灰色层覆盖屏幕,这个灰色层在选择区域内变得完全透明。我已经非常接近了。我正在展示一个无边框的50%不透明灰色形状,覆盖整个屏幕,并有一个透明度的紫红色。然后在该表格的顶部绘制2个矩形。透明度的固体紫红色矩形和红色边框的另一个矩形。它有效,但只有当我做三件事之一时,其中没有一件是选择。

  1. 禁用双缓冲,使绘图时表单闪烁
  2. 将桌面颜色模式从32位更改为16位
  3. 使表单100%不透明

这是我的代码。有关如何使其工作的任何建议?

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);
    }
}
c# transparency opacity
1个回答
0
投票

您可以使用

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

alpha从0到255,所以你的alpha值为128会给你50%的不实际。

这个解决方案是在this question中找到的

© www.soinside.com 2019 - 2024. All rights reserved.