您的位置:首页 > 其它

独家原创,拖拽任意控件移动任意目标,拖拽控件移动整个窗体

2015-06-24 15:36 309 查看
独家原创,拖拽任意控件移动任意目标,拖拽控件移动整个窗体,在无边框窗体及其友好的实现拖拽移动窗体

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: