示例代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
public static class ControlDrag
{
static Dictionary<string, Point> _locationList = new Dictionary<string, Point>();
static Dictionary<string, bool> _isDownList = new Dictionary<string, bool>();
//扩展函数
public static void AddDrag(this Control control)
{
if (_locationList.ContainsKey(control.Name)==false)
{
_locationList.Add(control.Name, new Point() );
_isDownList.Add(control.Name, false);
        
        
//按下鼠标
control.MouseDown += (s, ev) =>
{ _isDownList[control.Name] = true;
_locationList[control.Name] = new Point(ev.X, ev.Y);
};
//松开鼠标
control.MouseUp += (s, ev) =>
{ _isDownList[control.Name] = false; };
//鼠标移动
control.MouseMove += (s, ev) =>
{
if (_isDownList[control.Name])
{
int left = control.Left + ev.X - _locationList[control.Name].X;
int top = control.Top + ev.Y - _locationList[control.Name].Y;
control.Location = new Point(left, top);
}
}; } } }

调用方式

private void Form1_Load(object sender, EventArgs e)
{ //可以是任何控件
panel1.AddDrag();
//可以是任何控件
label1.AddDrag();

}

/*

文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放

*/

winform控件拖动的更多相关文章

  1. C# WinForm实现控件拖动实例介绍

    主要是设计控件的MouseDown.MouseLeave.MouseMove事件.一步步来吧:1.定义一个枚举类型,描述光标状态 private enum EnumMousePointPosition ...

  2. WinForm控件使用文章收藏整理完成

    对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. 基础 - 常用控件 C# WinForm开发系列 - CheckBox/Button/Lab ...

  3. C# WinForm控件、自定义控件整理(大全)

    转:http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html 对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, ...

  4. [转载]WPF控件拖动

    这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中间动画) 2.3拖动 ...

  5. 在WPF中使用WinForm控件方法

    1.      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2.      在要使用WinForm控 ...

  6. WPF 调用WinForm控件

    WPF可以使用WindowsFormsHost控件做为容器去显示WinForm控件,类似的用法网上到处都是,就是拖一个WindowsFormsHost控件winHost1到WPF页面上,让后设置win ...

  7. WinForm控件TreeView 只部分节点显示 CheckBox

    WinForm控件TreeView 只部分节点显示  CheckBox 用过asp.net的应该知道,要在treeview中实现上述功能可以使用ShowCheckBox 属性指定那些节点显示check ...

  8. Winform控件重写

    Winform控件重写 因为最近的项目中越来越多的遇到了比较特殊的一些控件,有时候我们自己封装一下可能更加方便我们的使用,下面是我们项目中用到的,简单做一个记录. TextBox控件重写 主要的控制代 ...

  9. 通过WinForm控件创建的WPF控件无法输入的问题

    今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...

随机推荐

  1. CF748A Santa Claus and a Place in a Class 题解

    Content 圣诞老人坐在一个桌子被摆成 \(m\) 行 \(n\) 列的教室里.每个桌子有两个座位,每个位置从左往右都有编号,依次为 \(1,2,3,...,2\times n\times m\) ...

  2. CF1428A Box is Pull 题解

    Content 有一个兔子拖着一个盒子在走,每秒钟可以带着盒子走一个单位,也可以不带着盒子走一个单位.当且仅当兔子和盒子的距离不超过 \(1\) 时可以带着盒子走一个单位.现给出 \(t\) 次询问, ...

  3. java 多线程:Callable接口;FutureTask类实现对象【Thread、Runnable、Callable三种方式实现多线程的区别】

    Callable接口介绍: Java5开始,Java提供了Callable接口,像是Runnable接口的增强版,Callable接口提供了一个 call()方法可以作为线执行体. call()方法比 ...

  4. git 生成ssh

  5. 实体转为json的,如何处理外键情况

    实体转为json的,如何处理外键情况 jc.registerJsonValueProcessor(Userrelation.class, new JsonValueProcessor() {// 此处 ...

  6. GAN 简介

    GAN 原理: ​GAN 的主要灵感来源于博弈论中零和博弈的思想,应用到深度学习神经网络上来说,就是通过生成网络 G(Generator)和判别网络 D(Discriminator)不断博弈,进而使 ...

  7. 百度地图AK密钥申请

    注册登录 :http://lbsyun.baidu.com/apiconsole/key#/home 然后点击提交 这个就是AK密钥

  8. 【九度OJ】题目1024:畅通工程 解题报告

    [九度OJ]题目1024:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅 ...

  9. 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  10. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...