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程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
随机推荐
- ceph安装过程
创建群集[2019-03-20 18:35:04,232][ceph_deploy.conf][DEBUG ] found configuration file at: /home/sceph/.ce ...
- CSS-美化checkbox
注意:css3 的用: checked 伪类选择器会去检查元素属性(`input[checked]`),而不是 dom 节点上的属性( ``).所以要使用 jquery 的 prop 而非 attr ...
- 强哥新周报SQL
因为数据口径的更改,所以.强哥的SQL 比较好用.不会出麻烦. 总共有四个 日常记录下,好好看. -- 2019年4月核销新客 SELECT yzm2.consignee_phone AS `会员手机 ...
- Android深度探索-卷1第三章心得体会
第三章整章介绍了git,git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.通过配置git后可以很方便的找到需要的资源,更多的是代码和包,可以在本地建立版本库,为了 ...
- java中四种访问修饰符区别及详解全过程
客户端程序员:即在其应用中使用数据类型的类消费者,他的目标是收集各种用来实现快速应用开发的类. 类创建者:即创建新数据类型的程序员,目标是构建类. 访问控制存在的原因:a.让客户端程序员无法触及他们不 ...
- ASP.NET MVC4获取当前系统时间
<p>当前时间是:@ViewBag.CurrentDate.ToLongDateString()</p>
- centOS发布.Net Core 2.0 API
1.dotnet xxx.dll & & 放在启动参数后面表示设置此进程为后台进程.(目前测试无效) 2.ps -ef | grep xxx ps:将某个进程显示出来 -A 显示所有 ...
- 教你如何创建vue环境
教你如何创建vue的环境 wdnmd我操作了一万年,终于成功创建了vue的环境,现在就来讲一下,到底应该怎么操作才能成功创建vue的 第一步 : 我们应该做的不是别的,而是去官方网站下载文件 node ...
- fieldset与legend,label
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Apache2.2学习笔记全集
一.基本介绍 待补充... 二.Apache默认配置文件解释 # 定义apache运行的目录,即程序所在的位置 ServerRoot "/usr/local/apache2" # ...