简体   繁体   English

使用C#在屏幕上动态绘制填充矩形

[英]Draw a fill rectangle dynamically on screen in C#

I would like to draw a fill rectangle dynamically on my screen, with an opacity at 0.1. 我想在屏幕上动态绘制一个填充矩形,其不透明度为0.1。 The problem is that when i move the mouse, the previous rectangles aren't clear. 问题是当我移动鼠标时,先前的矩形不清楚。

This is the drawing methods. 这是绘制方法。

    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));

    }

This is my selectorForm 这是我的选择器

    internal class SelectorForm : Form
    {
        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }
    }

An example when I draw a rectangle (several overlapping rectangles) 当我绘制一个矩形(几个重叠的矩形)时的示例

And Invalidate() doesn't work because I override OnPaintBackground . 而且Invalidate()不起作用,因为我重写了OnPaintBackground But if I don't do this override, when I do this.selectorForm.Show() , my screen becomes gray . 但是,如果我不执行此覆盖,则在执行this.selectorForm.Show()我的屏幕将变为灰色

So how can I draw a rectangle with an opacity 0.1 on my screen? 那么如何在屏幕上绘制不透明度为0.1的矩形呢?

Thank you ! 谢谢 !

This is an example that works for me. 这是一个对我有用的例子。

The crucial parts are: 关键部分是:

  • using the Paint event and its Graphics 使用Paint事件及其 Graphics
  • adding a Clear(BackgroundColor) or else I get the same artifacts you see 添加一个Clear(BackgroundColor) ,否则我会看到与您看到的相同的工件
  • for transparency the TransparencyKey property should be used. 为了透明起见,应该使用TransparencyKey属性。 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!
    }
}

} }

You can try following code: 您可以尝试以下代码:

    g.Clear();
    g.FillRectangle(brush, this.getRectangle(x, y, e.X, e.Y));
    g.DrawRectangle(pen, this.getRectangle(x, y, e.X, e.Y));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM