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程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
随机推荐
- 组合的输出(回溯、dfs)
问题 O: [回溯法]组合的输出 题目描述 排列与组合是常用的数学方法,其中组合就是从n个元素中抽出r个元素(不分顺序且r<=n),我们可以简单地将n个元素理解为自然数1,2,…,n,从中任取r ...
- BZOJ 4710: [Jsoi2011]分特产(容斥)
传送门 解题思路 首先所有物品是一定要用完的,那么可以按照物品考虑,就是把每种物品分给\(n\)个人,每个人分得非负整数,可以用隔板法计算.设物品有\(m\)个,方案数为\(C(n+m-1,n-1)\ ...
- 华为交换机telnet配置
1.在路由器上和交换机相连的借口上配置一个IP地址:比如192.168.1.1 24 2.在交换机上配置如下:<switch>system-view[switch]vlan 10[swit ...
- php理解非对称SSL加密解密、验证及签名
加密方式分为对称加密和非对称加密,对称加密只使用一个秘钥,加密和解密都使用该秘钥:非对称加密则使用一对秘钥,使用公钥加密,私钥解密. 需要通过原生的openssl_public_encrypt加密,o ...
- Cent OS (三)vi文本编辑操作
序号 命令 命令含义 1 echo 2 vi/vim 编辑 3 cat cat 命令用于连接文件并打印到标准输出设备上. 4 more 分屏显示文本内容 5 l ...
- 120、TensorFlow创建计算图(二)
#创建一个计算流图 #大多数tensorflow程序开始于dataflow graph 的构造函数 #在这个命令中你执行了tensorflow api的函数 #创建了新的操作tf.Operation ...
- Cocos2d之FlyBird开发---简介
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 开发FlyBird其实非常的简单,在游戏的核心部分,我们需要实现的只有: 创建一个物理世界(世界设置重力加速度) 在物理世界中添加一个动态 ...
- VS2013编译程序出现error C4996: 'std::_Fill_n': Function call with parameters that may be unsafe
最近按照BiliBil网站Visual C++网络项目实战视频教程,使用VS2013编写一个基于MFC的对话框程序HttpSourceViewer,采用了WinHttp库.Boost xpressiv ...
- K8S入门系列之必备扩展组件--> coredns(四)
摘要: 集群其他组件全部完成后我们应当部署集群 DNS 使 service 等能够正常解析,1.11版本coredns已经取代kube-dns成为集群默认dns. https://github.com ...
- c# 自定义控件之 ComboBox
winform 自带的 combobox 无法支持根据输入文本匹配列表中的项目,需要使用自定义控件. public class MyCombobox : ComboBox { //初始化数据项 pri ...