C#-如何在屏幕上擦除绘制矩形?

时间:2018-09-26 19:36:39

标签: c# draw erase

我正在使用此代码绘制一个矩形,它将在鼠标位置500ms之后再次重新绘制。

一切都很好,矩形绘制效果很好,但是使用此代码,已经在屏幕上绘制的旧矩形将永远显示在屏幕上,不会被擦除。

我需要在制作新矩形后删除旧矩形。新的矩形由while循环组成。

int i = 0;
while (i != 1)
{
    int x = Cursor.Position.X;
    int y = Cursor.Position.Y;
    Graphics g = Graphics.FromHwnd(IntPtr.Zero);
    Rectangle mouseNewRect = new Rectangle(new Point(x, y), new Size(30, 30));
    g.DrawRectangle(new Pen(Brushes.Chocolate), mouseNewRect);
    Wait(500);
}

编辑:我需要删除既在窗体内又在窗体外的矩形。想法?

3 个答案:

答案 0 :(得分:0)

您需要使要绘制的窗口在迭代之间重新绘制自身。另一种解决方案是让您捕获到内存中图像的窗口;在该图像的副本上绘制矩形,然后将图像绘制到窗口。每次重复此过程,以使目标窗口不包含先前迭代中的工件。

答案 1 :(得分:0)

您可以调用Invalidate()方法,该方法将通过再次调用paint方法来请求重绘该区域。

请参见MSDN上的Control.Invalidate()

在您的情况下,是这样的吗?

int i = 0;
while (i != 1)
{
    int x = Cursor.Position.X;
    int y = Cursor.Position.Y;
    Graphics g = Graphics.FromHwnd(IntPtr.Zero);
    Rectangle mouseNewRect = new Rectangle(new Point(x, y), new Size(30, 30));
    g.DrawRectangle(new Pen(Brushes.Chocolate), mouseNewRect);
    Wait(500);
    Invalidate();
}

关于刷新整个屏幕,您可以使用此p /调用。 https://www.pinvoke.net/default.aspx/user32.invalidaterect

[DllImport("user32.dll")]
static extern bool InvalidateRect(IntPtr hWnd, RECT lpRect, bool bErase);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
   public int Left, Top, Right, Bottom;

   public RECT(int left, int top, int right, int bottom)
   {
     Left = left;
     Top = top;
     Right = right;
     Bottom = bottom;
   }

   public RECT(System.Drawing.Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { }

   public int X
   {
     get { return Left; }
     set { Right -= (Left - value); Left = value; }
   }

   public int Y
   {
     get { return Top; }
     set { Bottom -= (Top - value); Top = value; }
   }

   public int Height
   {
     get { return Bottom - Top; }
     set { Bottom = value + Top; }
   }

   public int Width
   {
     get { return Right - Left; }
     set { Right = value + Left; }
   }

   public System.Drawing.Point Location
   {
     get { return new System.Drawing.Point(Left, Top); }
     set { X = value.X; Y = value.Y; }
   }

   public System.Drawing.Size Size
   {
     get { return new System.Drawing.Size(Width, Height); }
     set { Width = value.Width; Height = value.Height; }
   }

   public static implicit operator System.Drawing.Rectangle(RECT r)
   {
     return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
   }

   public static implicit operator RECT(System.Drawing.Rectangle r)
   {
     return new RECT(r);
   }

   public static bool operator ==(RECT r1, RECT r2)
   {
     return r1.Equals(r2);
   }

   public static bool operator !=(RECT r1, RECT r2)
   {
     return !r1.Equals(r2);
   }

   public bool Equals(RECT r)
   {
     return r.Left == Left && r.Top == Top && r.Right == Right && r.Bottom == Bottom;
   }

   public override bool Equals(object obj)
   {
     if (obj is RECT)
       return Equals((RECT)obj);
     else if (obj is System.Drawing.Rectangle)
       return Equals(new RECT((System.Drawing.Rectangle)obj));
     return false;
   }

   public override int GetHashCode()
   {
     return ((System.Drawing.Rectangle)this).GetHashCode();
   }

   public override string ToString()
   {
     return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{{Left={0},Top={1},Right={2},Bottom={3}}}", Left, Top, Right, Bottom);
   }
}

然后通过调用

使用它
InvalidateRect(IntPtr.Zero, null, true);

传递null和true作为第二个和第三个参数应该会重绘整个屏幕。

答案 2 :(得分:0)

@Emcrank发布的解决方案有效。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Load += async delegate
        {
            var g = Graphics.FromHwnd(IntPtr.Zero);
            var mouseNewRect = new Rectangle(Point.Empty, new Size(30, 30));
            var pen = new Pen(Brushes.Chocolate);
            while (true)
            {
                mouseNewRect.Location = Cursor.Position;
                g.DrawRectangle(pen, mouseNewRect);
                await Task.Delay(500);
                InvalidateRect(IntPtr.Zero, IntPtr.Zero, true);
            }
        };
    }

    [DllImport("user32.dll")]
    static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
}

无论如何,如果要在窗体内绘制鼠标位置,则有更好的解决方案。

public partial class Form1 : Form
{
    private Rectangle _rect = new Rectangle(0, 0, 30, 30);
    private readonly Pen _pen = new Pen(Brushes.Chocolate);
    public Form1()
    {
        InitializeComponent();
        SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        _rect.Location = e.Location;
        Invalidate(ClientRectangle);
        base.OnMouseMove(e);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        //--improve graphics quality
        var g = e.Graphics;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.AntiAlias;

        _rect.Offset(-15, -15); //--center rect

        e.Graphics.DrawRectangle(_pen, _rect);

        base.OnPaint(e);
    }
}

希望对您有帮助。