C# 从屏幕上擦除DrawReversibleFrame

C# 从屏幕上擦除DrawReversibleFrame,c#,.net,C#,.net,从屏幕上删除DrawReversibleFrame时遇到问题。文档说要输入两次函数才能删除它,但它似乎不适合我的逻辑布局 using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp6 { public partial class Form1 : Form { private Point? _start; private Rectangle _previousBounds; pu

从屏幕上删除DrawReversibleFrame时遇到问题。文档说要输入两次函数才能删除它,但它似乎不适合我的逻辑布局

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp6
{
public partial class Form1 : Form
{
    private Point? _start;
    private Rectangle _previousBounds;

    public Form1()
    {
        InitializeComponent();
    }

    private void OnMouseDown(object sender,MouseEventArgs e)
    {
        _start = e.Location;
    }

    private void OnMouseMove(object sender,MouseEventArgs e)
    {
        if (_start.HasValue)
        {
            ReverseFrame();
            DrawFrame(e.Location);
        }
    }

    private void OnMouseUp(object sender,MouseEventArgs e)
    {
        ReverseFrame();
        _start = null;
    }

    private void ReverseFrame()
    {
        ControlPaint.DrawReversibleFrame(_previousBounds, Color.Red, FrameStyle.Dashed);

    }
    private void DrawFrame(Point end)
    {
        ReverseFrame();

        var size = new Size(end.X - _start.Value.X, end.Y - _start.Value.Y);
        _previousBounds = new Rectangle(_start.Value, size);
        _previousBounds = RectangleToScreen(_previousBounds);
        ControlPaint.DrawReversibleFrame(_previousBounds, Color.Red, FrameStyle.Dashed);
    }
}
}
这是我在窗体上拖动鼠标时的结果

pesudo代码应该是这样工作的,我觉得我正在这样做

鼠标向下:

画矩形

保存矩形边界

--

鼠标移动(按下左键):

重新绘制要删除的上一个矩形

计算下一个矩形

绘制下一个矩形

保存矩形边界

冲洗/重复(无限期)

--

鼠标:

重新绘制要删除的最后一个矩形。

查看

示例中的MouseUp处理程序是关注点

 private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // If the MouseUp event occurs, the user is not dragging.
            isDrag = false;

            // Draw the rectangle to be evaluated. Set a dashed frame style 
            // using the FrameStyle enumeration.
            ControlPaint.DrawReversibleFrame(theRectangle, this.BackColor, FrameStyle.Dashed);

            // Find out which controls intersect the rectangle and 
            // change their color. The method uses the RectangleToScreen  
            // method to convert the Control's client coordinates 
            // to screen coordinates.
            Rectangle controlRectangle;
            for (int i = 0; i < Controls.Count; i++)
            {
                controlRectangle = Controls[i].RectangleToScreen(Controls[i].ClientRectangle);
                if (controlRectangle.IntersectsWith(theRectangle))
                {
                    Controls[i].BackColor = Color.BurlyWood;
                }
            }

            // Reset the rectangle.
            theRectangle = new Rectangle(0, 0, 0, 0);
        }
更新


我测试了你的代码。我认为您的矩形_previousBounds不是第二次绘制。

我尝试了microsoft示例,效果更好。我现在遇到的问题是,每移动一帧,它都会闪烁。您必须记住,对ReverseFrame()的第一次调用是无效的。因为还没有什么可以逆转的。
// ControlPaint.DrawReversibleFrame(theRectangle, this.BackColor, FrameStyle.Dashed);