独家原创,拖拽任意控件移动任意目标,拖拽控件移动整个窗体
2015-06-24 15:36
309 查看
独家原创,拖拽任意控件移动任意目标,拖拽控件移动整个窗体,在无边框窗体及其友好的实现拖拽移动窗体
http://www.cnblogs.com/vonly/
only原创首发,vonly.net
http://www.cnblogs.com/vonly/
only原创首发,vonly.net
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Diagnostics; using System.Text; using System.Threading; using System.Windows.Forms; namespace only.Controls { public partial class DragDrop : Component { private bool _enable = false; [Browsable(true)] [Description("打开拖拽功能"), Category("设置")] public bool Enable { get { return _enable; } set { if (value != _enable) { _enable = value; // Start(value); new Thread(() => { Thread.Sleep(100); Start(_enable); }).Start(); } } } [Browsable(true)] [Description("拖拽移动目标"), Category("设置")] public Control TargetControl { get; set; } [Browsable(true)] [Description("拖拽事件源"), Category("设置")] public Control SourceControl { get; set; } public void Start(bool enable = true) { if (SourceControl != null & TargetControl != null) { if (enable) { SourceControl.MouseDown += SourceControlOnMouseDown; SourceControl.MouseMove += SourceControlOnMouseMove; SourceControl.MouseUp += SourceControlOnMouseUp; } else { SourceControl.MouseDown -= SourceControlOnMouseDown; SourceControl.MouseMove -= SourceControlOnMouseMove; SourceControl.MouseUp -= SourceControlOnMouseUp; } } } private bool _mouseDown = false; private int _startX = 0; private int _startY = 0; private void SourceControlOnMouseUp(object sender, MouseEventArgs e) { _mouseDown = false; } private void SourceControlOnMouseMove(object sender, MouseEventArgs e) { if (_mouseDown & _enable) { TargetControl.Left += (e.X - _startX); TargetControl.Top += (e.Y - _startY); } } private void SourceControlOnMouseDown(object sender, MouseEventArgs e) { if (_mouseDown == false) { _mouseDown = true; _startX = e.X; _startY = e.Y; } } } }
相关文章推荐
- MySQL中用decimal的原因
- WPF在资源内嵌入字体
- Go语言Slice操作.
- 终极版android.mk
- mkdir -p 创建多级目录
- 深度学习笔记9 Sparse coding
- 图像处理——卷积、滤波的整理
- Android网络判断
- jquery实现替代iframe的功能
- Android动画学习(一)
- mkdir -p 创建多级目录
- html中<a>标记的href属性不能正确跳转
- IOS学习实例之三---QQ列表
- 每日刷题(2015/6/24):Find the mistake(s) in the following co
- 递归求解整数数组A[n]的最小整数
- arm下adc测试代码
- 浅谈 SOAP
- printk 使用方法
- Struts2 防止重复提交问题
- Java中读取配置文件使用jdbc-c3p0连接池连接sqlserver数据库