加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

winforms – 拖放Windows窗体按钮

发布时间:2020-12-14 02:09:06 所属栏目:Windows 来源:网络整理
导读:我创建了一个新的 Windows窗体应用程序.我将一个按钮拖到表单上. 我需要在运行时将此按钮拖放到此表单中的其他位置. 任何代码片段或链接都表示赞赏. 我来这里前花了半个小时的时间. 解决方法 你可以从这样的事情开始: bool isDragged = false; Point ptOffs
我创建了一个新的 Windows窗体应用程序.我将一个按钮拖到表单上.
我需要在运行时将此按钮拖放到此表单中的其他位置.
任何代码片段或链接都表示赞赏.

我来这里前花了半个小时的时间.

解决方法

你可以从这样的事情开始:

bool isDragged = false;
  Point ptOffset;
  private void button1_MouseDown( object sender,MouseEventArgs e )
  {
     if ( e.Button == MouseButtons.Left )
     {
        isDragged = true;
        Point ptStartPosition = button1.PointToScreen(new Point(e.X,e.Y));

        ptOffset = new Point();
        ptOffset.X = button1.Location.X - ptStartPosition.X;
        ptOffset.Y = button1.Location.Y - ptStartPosition.Y;
     }
     else
     {
        isDragged = false;
     }
  }

  private void button1_MouseMove( object sender,MouseEventArgs e )
  {
     if ( isDragged )
     {
        Point newPoint = button1.PointToScreen(new Point(e.X,e.Y));
        newPoint.Offset(ptOffset);
        button1.Location = newPoint;
     }
  }

  private void button1_MouseUp( object sender,MouseEventArgs e )
  {
     isDragged = false;
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读