winform 控件拖拽和缩放
核心类:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace ControlTest
public class AddControlDragAttr
{
//委托事件 用于支持将其他的可拖拉的控件描点置隐藏
public event EventHandler OtherLabelsVisibleEvent; /// <summary>
/// 当前控件
/// </summary>
public Control CurrControl { get; private set; } /// <summary>
/// 当前鼠标点击坐标
/// </summary>
Point downPoint;
/// <summary>
/// 当前控件的左右宽高
/// </summary>
int normalLeft, normalTop, normalWidth, normalHeight; /// <summary>
/// 是否允许拖拉
/// </summary>
bool draging; /// <summary>
/// 控件最小值
/// </summary>
public int ControlMinSize => ; /// <summary>
/// 是否已点击
/// </summary>
bool isDown = false;
/// <summary>
/// 点击可拖动的描点的控件
/// </summary>
Label[] labels = new Label[]; private Color barColor = Color.Black;
/// <summary>
/// 拖动块的颜色
/// </summary>
public Color BarColor
{
get => this.barColor;
set
{
for (int i = ; i < labels.Length; i++)
{
labels[i].BackColor = value;
}
this.barColor = value;
}
}
private bool labsVisible = false;
/// <summary>
/// 是否显示拖动块的描点
/// </summary>
public bool LabsVisible
{
get => labsVisible;
set
{
foreach (var lab in labels)
{
lab.Visible = value;
} labsVisible = value;
}
} private int barSize = ;
/// <summary>
/// 拖动块描点的大小
/// </summary>
public int BarSize
{
get => this.barSize;
set
{
foreach (var item in labels)
{
item.Size = new Size(value, value);
}
this.barSize = value;
}
} /// <summary>
///系统自带的可拖动描点
/// </summary>
Cursor[] cursors = new Cursor[]
{
//获取双向对角线(西北/东南)大小调整光标
Cursors.SizeNWSE,
//获取双向垂直(北/南)大小调整光标
Cursors.SizeNS,
//获取双向对角线(东北/西南)大小调整光标
Cursors.SizeNESW,
//获取双向水平(西/东)大小调整光标
Cursors.SizeWE,
Cursors.SizeNWSE,
Cursors.SizeNS,
Cursors.SizeNESW,
Cursors.SizeWE,
}; public AddControlDragAttr(Control control)
{
CurrControl = control;
CurrControl.MouseDown += control_MouseDown;
CurrControl.MouseUp += control_MouseUp;
CurrControl.MouseMove += control_MouseMove;
for (int i = ; i < labels.Length; i++)
{
labels[i] = new Label
{
TabIndex = i,
BackColor = BarColor,
Cursor = cursors[i],
Text = "",
Visible = false,
//置顶
};
labels[i].MouseDown += label_MouseDown;
labels[i].MouseMove += label_MouseMove;
labels[i].MouseUp += label_MouseUp; CurrControl.Parent.Controls.Add(labels[i]);
}
} //控件事件
private void control_MouseDown(object sender, MouseEventArgs e)
{
downPoint = e.Location;
isDown = true;
LabsVisible = false; } private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDown)
{
int x = CurrControl.Location.X + e.Location.X - downPoint.X;
int y = CurrControl.Location.Y + e.Location.Y - downPoint.Y; CurrControl.Location = new Point(x, y);
LabsVisible = false;
}
} private void control_MouseUp(object sender, MouseEventArgs e)
{
int x = CurrControl.Location.X + e.Location.X - downPoint.X;
int y = CurrControl.Location.Y + e.Location.Y - downPoint.Y; CurrControl.Location = new Point(x, y); SetBarBounds();
isDown = false; LabsVisible = true; OtherLabelsVisibleEvent?.Invoke(null,null);
} //控件拖动块描点事件
private void label_MouseDown(object sender, MouseEventArgs e)
{
this.draging = true;
this.normalLeft = CurrControl.Left;
this.normalTop = CurrControl.Top;
this.normalWidth = CurrControl.Width;
this.normalHeight = CurrControl.Height;
this.LabsVisible = false;
} /// <summary>
/// 0 1 2
/// 7 3
/// 6 5 4
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label_MouseMove(object sender, MouseEventArgs e)
{
int left = CurrControl.Left;
int width = CurrControl.Width;
int top = CurrControl.Top;
int height = CurrControl.Height;
if (draging)
{
switch (((Control)sender).TabIndex)
{
case :
left = normalLeft + e.X < normalLeft + normalWidth - ControlMinSize ? normalLeft + e.X : normalLeft + normalWidth - ControlMinSize;
top = normalTop + e.Y < normalTop + normalHeight - ControlMinSize ? normalTop + e.Y : normalTop + normalHeight - ControlMinSize;
width = normalLeft + normalWidth - CurrControl.Left;
height = normalTop + normalHeight - CurrControl.Top;
break;
case :
top = normalTop + e.Y < normalTop + normalHeight - ControlMinSize ? normalTop + e.Y : normalTop + normalHeight - ControlMinSize;
height = normalTop + normalHeight - CurrControl.Top;
break;
case :
width = normalWidth + e.X > ControlMinSize ? normalWidth + e.X : ControlMinSize;
top = normalTop + e.Y < normalTop + normalHeight - ControlMinSize ? normalTop + e.Y : normalTop + normalHeight - ControlMinSize;
height = normalTop + normalHeight - CurrControl.Top;
break;
case :
width = normalWidth + e.X > ControlMinSize ? normalWidth + e.X : ControlMinSize;
break;
case :
width = normalWidth + e.X > ControlMinSize ? normalWidth + e.X : ControlMinSize;
height = normalHeight + e.Y > ControlMinSize ? normalHeight + e.Y : ControlMinSize;
break;
case :
height = normalHeight + e.Y > ControlMinSize ? normalHeight + e.Y : ControlMinSize;
break;
case :
left = normalLeft + e.X < normalLeft + normalWidth - ControlMinSize ? normalLeft + e.X : normalLeft + normalWidth - ControlMinSize;
width = normalLeft + normalWidth - CurrControl.Left;
height = normalHeight + e.Y > ControlMinSize ? normalHeight + e.Y : ControlMinSize;
break;
case :
left = normalLeft + e.X < normalLeft + normalWidth - ControlMinSize ? normalLeft + e.X : normalLeft + normalWidth - ControlMinSize;
width = normalLeft + normalWidth - CurrControl.Left;
break;
}
left = (left < ) ? : left;
top = (top < ) ? : top;
CurrControl.SetBounds(left, top, width, height);
}
} private void label_MouseUp(object sender, MouseEventArgs e)
{
draging = false;
SetBarBounds();
this.LabsVisible = true;
} //设置拖动块描点的位置
private void SetBarBounds()
{
int x = CurrControl.Left - BarSize;
int y = CurrControl.Top - BarSize;
int w = CurrControl.Width + BarSize;
int h = CurrControl.Height + BarSize;
int b = BarSize / ;
var px = new List<int>
{
x + b,
x + w / ,
x + w - b,
x + w - b,
x + w - b,
x + w / ,
x + b,
x + b,
};
var py = new List<int>
{
y + b,
y + b,
y + b,
y + h / ,
y + h - b,
y + h - b,
y + h - b,
y + h /
}; for (int i = ; i < labels.Length; i++)
{
labels[i].SetBounds(px[i], py[i], BarSize, BarSize);
}
}
}
}
调用实例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace ControlTest
{
public partial class Form1 : Form
{
List<AddControlDragAttr> listcontrolsattr = new List<AddControlDragAttr>(); public Form1()
{
InitializeComponent();
} protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Controls.AddRange(new Control[] {
new UserControl1{ Name = "usercontrol1" },
new Button{ Name ="btn1", Size = new Size(,)
,Text= "马克思和恩格斯对社会主义发展出了他们的理论体系,亦认为社会主义社会是资本主义社会向共产主义社会过渡的社会形态" },
new PictureBox{ Name="pic1", Size = new Size(,), Location = new Point(,)
,BackgroundImage = Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "img.jpg"))
,BackgroundImageLayout = ImageLayout.Stretch}
}); foreach (Control control in this.Controls)
{
var controlattr = new AddControlDragAttr(control); //将其他控件的描点隐藏
controlattr.OtherLabelsVisibleEvent += (sender1,e1) => {
SetOtherLabelVisible(control);
}; listcontrolsattr.Add(controlattr);
}
} public void SetOtherLabelVisible(Control control)
{
foreach (AddControlDragAttr item in listcontrolsattr)
{
if (item.CurrControl.Name != control.Name)
item.LabsVisible = false;
}
} }
}
winform 控件拖拽和缩放的更多相关文章
- Duilib的控件拖拽排序,支持跨容器拖拽(网易云信版本)
完整代码见:https://github.com/netease-im/NIM_Duilib_Framework/pull/151 核心代码(思路): appitem.h #pragma once # ...
- duilib中控件拖拽功能的实现方法(附源码)
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41144283 duilib库中原本没有显示的对控件增加拖拽的功能,而实际 ...
- Winform图片拖拽与缩放
最近做项目的时候遇到上传施工平面布置图,查看,因为图片比较大,一般的显示器分辨率无法显示全,然后还需要放大看清楚图片里面的文字内容,所以需要用到图片的拖拽与缩放功能.这里整理下具体操作. 首先新建一个 ...
- WinForms拖控件拖到天荒地老
更新记录: 2022年4月15日:本文迁移自Panda666原博客,原发布时间:2021年4月18日. 2022年4月15日:更新自动生成Web CURD工具. 说明 Winforms的控件拖起来是真 ...
- WPF 调用WinForm控件
WPF可以使用WindowsFormsHost控件做为容器去显示WinForm控件,类似的用法网上到处都是,就是拖一个WindowsFormsHost控件winHost1到WPF页面上,让后设置win ...
- 在WPF中使用WinForm控件方法
1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2. 在要使用WinForm控 ...
- WinForm控件TreeView 只部分节点显示 CheckBox
WinForm控件TreeView 只部分节点显示 CheckBox 用过asp.net的应该知道,要在treeview中实现上述功能可以使用ShowCheckBox 属性指定那些节点显示check ...
- Winform控件重写
Winform控件重写 因为最近的项目中越来越多的遇到了比较特殊的一些控件,有时候我们自己封装一下可能更加方便我们的使用,下面是我们项目中用到的,简单做一个记录. TextBox控件重写 主要的控制代 ...
- 通过WinForm控件创建的WPF控件无法输入的问题
今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
随机推荐
- LDD3 第9章 与硬件通信
一.I/O端口和I/O内存 每种外设都通过读写寄存器进行控制.大部分外设都有几个寄存器,不管是在内村地址空间还是在I/O地址空间,这些寄存器的访问地址都是连续的. 在硬件层,内存区域和I/O区域没有区 ...
- 【LeetCode 75】颜色分类
题目链接 [题解] 维护一个左边界l和一个右边界r 其中0..l-1都是'0' 而 r+1..n-1都是'2' 我们令i=l;i<=r; 枚举每一个a[i]; ①如果a[i]=2.那么把a[i] ...
- 安装uWebSocketIO
https://github.com/uNetworking/uWebSockets sudo apt-get install libuv1-dev git clone https://github. ...
- JS当中的无限分类递归树
列表转换成树形结构方法定义: //javascript 树形结构 function toTree(data) { // 删除 所有 children,以防止多次调用 data.forEach(func ...
- Nuget-Swagger-Swashbuckle:Swashbuckle
ylbtech-Nuget-Swagger-Swashbuckle:Swashbuckle 1.返回顶部 1. Seamlessly adds a Swagger to WebApi projects ...
- CentOS7 图形化方式安装Oracle 18c 安装配置
下载 Oracle 数据库,zip 包 https://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.h ...
- mybatis 插入
实体类 Mapper接口 void addUser(User user); Mapper.xml <insert id="addUser" useGeneratedKeys= ...
- 基于Java Agent的premain方式实现方法耗时监控(转),为了找到结论执行:premain在jvm启动的时候执行,所有方法前,会执行MyAgent的premain方法
Java Agent是依附于java应用程序并能对其字节码做相关更改的一项技术,它也是一个Jar包,但并不能独立运行,有点像寄生虫的感觉.当今的许多开源工具尤其是监控和诊断工具,很多都是基于Java ...
- HTML表单实例
HTML表单 表单用于搜集不同类型的用户输入,表单由不同类型的标签组成,实现一个特定功能的表单区域(比如:注册), 首先应该用<form>标签来定义表单区域整体,在此标签中再使用不同的表单 ...
- bootstrap知识点
首先,声明本次笔记是来自biaoyansu.com表严肃老师的bootstrap课程视频. 1.基本知识:1-1.首先,Html(理解:骨骼).Css(理解:皮肤).Js(理解:神经)分工不同.1-2 ...