删除拖放矩形

时间:2017-02-10 02:17:49

标签: c# drag-and-drop

我可以绘制拖放矩形,但拖放后,前一个矩形副本仍然在其位置。我怎么能删除它?

namespace dragAndDropRect
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Rectangle rect = new Rectangle(30, 30, 30, 30);
            g.FillRectangle(Brushes.Aqua, rect);

            if (e.Button == MouseButtons.Left)
            {
                Refresh();
                g.FillRectangle(Brushes.AliceBlue, e.X, e.Y, 30, 30);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先处理旧矩形,然后创建一个新矩形

namespace dragAndDropRect
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        Graphics g = this.CreateGraphics();
        Rectangle rect = new Rectangle(30, 30, 30, 30);
        g.FillRectangle(Brushes.Aqua, rect);

        if (e.Button == MouseButtons.Left)
        {
            Refresh();
            g.Invalidate();
            g.FillRectangle(Brushes.AliceBlue, e.X, e.Y, 30, 30);
        }
    }
}
}