C# 实现控件拖拽功能(Winform)

C# 实现控件拖拽功能(Winform)

1、关键知识点说明:

通过DragEnter事件获得被拖入窗口的“信息”(可以是若干文件,一些文字等等),在DragDrop事件中对“信息”进行解析。窗体的AllowDrop属性必须设置成true;且必须有DragEnter事件(单独写DragDrop事件是不会具有拖拽功能的)。
2、属性设置
在这里插入图片描述
3、代码

private void button4_MouseDown(object sender, MouseEventArgs e)
        {
            button4.Tag = e.Location;
            DoDragDrop(button4, DragDropEffects.Move);
        }

        private void button5_MouseDown(object sender, MouseEventArgs e)
        {
            button5.Tag = e.Location;
            DoDragDrop(button5, DragDropEffects.Move);
        }

        private void button6_MouseDown(object sender, MouseEventArgs e)
        {
            button6.Tag = e.Location;
            DoDragDrop(button6, DragDropEffects.Move);
        }

        private void button7_MouseDown(object sender, MouseEventArgs e)
        {
            button7.Tag = e.Location;
            DoDragDrop(button7, DragDropEffects.Move);
        }

        private void button8_MouseDown(object sender, MouseEventArgs e)
        {
            button8.Tag = e.Location;
            this.DoDragDrop(button8, DragDropEffects.Move);
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            object data = e.Data.GetData(typeof(Button));
            if (data == button4)
            {
                //设置button距离容器上边缘之间的距离
                button4.Top = this.PointToClient(new Point(e.X, e.Y)).Y - ((Point)button4.Tag).Y;

                //设置button距离容器左边缘之间的距离
                button4.Left = this.PointToClient(new Point(e.X, e.Y)).X - ((Point)button4.Tag).X;
            }

            if (data == button5)
            {
                button5.Top = this.PointToClient(new Point(e.X, e.Y)).Y - ((Point)button5.Tag).Y;
                button5.Left = this.PointToClient(new Point(e.X, e.Y)).X - ((Point)button5.Tag).X;
            }

            if (data == button6)
            {
                button6.Top = this.PointToClient(new Point(e.X, e.Y)).Y - ((Point)button6.Tag).Y;
                button6.Left = this.PointToClient(new Point(e.X, e.Y)).X - ((Point)button6.Tag).X;
            }

            if (data == button7)
            {
                button7.Top = this.PointToClient(new Point(e.X, e.Y)).Y - ((Point)button7.Tag).Y;
                button7.Left = this.PointToClient(new Point(e.X, e.Y)).X - ((Point)button7.Tag).X;
            }
            if (data ==button8 )
            {
                button8.Top = this.PointToClient(new Point(e.X, e.Y)).Y - ((Point)button8.Tag).Y;
                button8.Left = this.PointToClient(new Point(e.X, e.Y)).X - ((Point)button8.Tag).X;
            }
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move; //将拖动源的数据移动到放置目标
        }

        private void Form1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            e.UseDefaultCursors = false;
            Cursor.Current = Cursor;
        }

4、效果展示
在这里插入图片描述

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五菱萌主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值