winform控件拖动
示例代码
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控件拖动的更多相关文章
- C# WinForm实现控件拖动实例介绍
主要是设计控件的MouseDown.MouseLeave.MouseMove事件.一步步来吧:1.定义一个枚举类型,描述光标状态 private enum EnumMousePointPosition ...
- WinForm控件使用文章收藏整理完成
对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, 不断补充充实, 完善这方面. 基础 - 常用控件 C# WinForm开发系列 - CheckBox/Button/Lab ...
- C# WinForm控件、自定义控件整理(大全)
转:http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html 对C# WinForm开发系列收集的控件使用方面进行整理, 加入了一些文章, ...
- [转载]WPF控件拖动
这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中间动画) 2.3拖动 ...
- 在WPF中使用WinForm控件方法
1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2. 在要使用WinForm控 ...
- WPF 调用WinForm控件
WPF可以使用WindowsFormsHost控件做为容器去显示WinForm控件,类似的用法网上到处都是,就是拖一个WindowsFormsHost控件winHost1到WPF页面上,让后设置win ...
- WinForm控件TreeView 只部分节点显示 CheckBox
WinForm控件TreeView 只部分节点显示 CheckBox 用过asp.net的应该知道,要在treeview中实现上述功能可以使用ShowCheckBox 属性指定那些节点显示check ...
- Winform控件重写
Winform控件重写 因为最近的项目中越来越多的遇到了比较特殊的一些控件,有时候我们自己封装一下可能更加方便我们的使用,下面是我们项目中用到的,简单做一个记录. TextBox控件重写 主要的控制代 ...
- 通过WinForm控件创建的WPF控件无法输入的问题
今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
随机推荐
- UVA10079 Pizza Cutting 题解
Content 求用 \(n\) 条直线最多能将平面分成多少块区域. 多组输入,以一个负数结束. 数据范围:\(0\leqslant n\leqslant 2.1\times 10^8\). Solu ...
- CF1080B Margarite and the best present 题解
Content 有 \(t\) 次询问,每次询问给定两个整数 \(l,r\),求 \(\sum\limits_{i=l}^r (-1)^i\times i\). 数据范围:\(1\leqslant t ...
- Ribbon——负载均衡
一.什么是Ribbon Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接超 ...
- c++设计模式概述之状态
代码写的不够规范,目的是为了缩短篇幅,实际中请不要这样做 参看:https://www.runoob.com/design-pattern/state-pattern.html 1.概述 这个有点抽象 ...
- 一个简单的基于epoll的udp接收
--- 1 #include <iostream> 2 #include <sys/epoll.h> 3 #include <fcntl.h> 4 #include ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- codeforces626D . Jerry's Protest
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds ...
- CONTRASTIVE REPRESENTATION DISTILLATION
目录 概 主要内容 超参数的选择 代码 Tian Y., Krishnan D., Isola P. CONTRASTIVE REPRESENTATION DISTILLATION. arXiv pr ...
- Java得到当前时间并格式化输出
代码: SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间 sdf.applyPattern("yyyy-MM-dd HH:mm:s ...
- 论文翻译:2020_Generative Adversarial Network based Acoustic Echo Cancellation
论文地址:http://www.interspeech2020.org/uploadfile/pdf/Thu-1-10-5.pdf 基于GAN的回声消除 摘要 生成对抗网络(GANs)已成为语音增强( ...