在C#中动态在屏幕上绘制填充矩形 [英] Draw a fill rectangle dynamically on screen in C#
问题描述
我想在屏幕上动态绘制一个填充矩形,其不透明度为0.1.问题是当我移动鼠标时,先前的矩形不清晰.
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.
这是绘制方法.
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));
}
这是我的选择器表单
internal class SelectorForm : Form
{
protected override void OnPaintBackground(PaintEventArgs e)
{
}
}
并且Invalidate()
不起作用,因为我覆盖了OnPaintBackground
.但是,如果我不执行此覆盖,则在执行this.selectorForm.Show()
时,我的屏幕变成灰色.
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.
那么如何在屏幕上绘制不透明度为0.1的矩形?
So how can I draw a rectangle with an opacity 0.1 on my screen?
谢谢!
推荐答案
这是一个对我有用的示例.
This is an example that works for me.
关键部分是:
- 使用
Paint
事件及其事件Graphics
- 添加
Clear(BackgroundColor)
否则我会得到与您看到的相同的工件 - 为了透明起见,应使用TransparencyKey属性.有一定的颜色选择:
- 常用颜色可能与表单上的其他内容冲突
- 倒挂金钟可以奏效,如果您要点击表格
- 其他不太常见的颜色也适用;我选择了浅绿色
- using the
Paint
event and itsGraphics
- 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! } }
}
这篇关于在C#中动态在屏幕上绘制填充矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!