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

C#在50%不透明形式上绘制的透明实心矩形,c#,transparency,opacity,C#,Transparency,Opacity,我一直在尝试模仿Windows7的剪贴工具,在屏幕上覆盖一个半透明的灰色层,在选择区域内变得完全透明。我已经很接近了。我正在显示一个无边界的50%不透明的灰色表格,它覆盖了整个屏幕,并有一个透明的紫红色键。然后在这个表格的上面我画了两个矩形。一个实心的紫红色矩形表示透明度,另一个矩形表示红色边框。这是可行的,但前提是我做了三件事中的一件,没有一件是可选的 禁用双缓冲,使窗体在绘图时闪烁 将桌面颜色模式从32位更改为16位 使窗体100%不透明 这是我的密码。有没有关于如何让它发挥作用的建议 pu

我一直在尝试模仿Windows7的剪贴工具,在屏幕上覆盖一个半透明的灰色层,在选择区域内变得完全透明。我已经很接近了。我正在显示一个无边界的50%不透明的灰色表格,它覆盖了整个屏幕,并有一个透明的紫红色键。然后在这个表格的上面我画了两个矩形。一个实心的紫红色矩形表示透明度,另一个矩形表示红色边框。这是可行的,但前提是我做了三件事中的一件,没有一件是可选的

  • 禁用双缓冲,使窗体在绘图时闪烁
  • 将桌面颜色模式从32位更改为16位
  • 使窗体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);
        }
    }
    
    你可以用

    Brush brush = new SolidBrush(Color.FromArgb(alpha, red, green, blue))
    
    其中alpha从0到255,因此alpha的值为128将提供50%的不透明度


    此解决方案在

    中找到。不,不会。如果他的窗体不透明度为0.2,画笔的alpha为128,则矩形上的alpha为0.2*128