拖放 Windows 窗体按钮 [英] Drag and Drop Windows Forms Button

查看:35
本文介绍了拖放 Windows 窗体按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的 Windows 窗体应用程序.我将一个按钮拖到表单上.我需要在运行时将此按钮拖放到此表单中的另一个位置.感谢您提供任何代码片段或链接.

I create a new Windows Forms Application. I drag a button on to the form. I need to drag and drop this button to another location within this form at run time. any code snippets or links are appreciated.

我在来这里之前花了半个小时寻找.

I spent half an hour searching before coming here.

推荐答案

你可以这样开始:

  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;
  }

这篇关于拖放 Windows 窗体按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆