gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:39:23 25 4
gpt4 key购买 nike

我一直在尝试模仿 Windows 7 截图工具如何用半透明灰色层覆盖屏幕,该灰色层在选择区域内变得完全透明。我已经很接近了。我正在显示一个无边框的 50% 不透明灰色表单,它覆盖整个屏幕并具有紫红色的 TransparencyKey。然后在那个表格的顶部我画了 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);
}
}

最佳答案

你可以使用

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

其中 alpha 从 0 到 255,因此 alpha 值为 128 将为您提供 50% 的不透明度。

此解决方案在 this question 中找到

关于C# 在 50% 不透明窗体上绘制的透明实心矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9960228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com