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程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
随机推荐
- 【Flutter学习】基本组件之Webview组件
1.添加依赖 dependencies: flutter_webview_plugin: ^+ 2.导入库 import 'import 'package:flutter_webview_plugin ...
- <三剑客> 老大:awk命令用法
awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一 个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是lin ...
- delphi 解决RichViewEdit乱码问题
⑴ 设置RichViewEdit下面的几个属性: ① RTFReaderProperties → ParaStyleMode → rvrsAddIfNeeded ② RTFReaderProperti ...
- ARC093 F Dark Horse——容斥
题目:https://atcoder.jp/contests/arc093/tasks/arc093_d #include<cstdio> #include<cstring> ...
- Minimal Power of Prime
题目链接 题意:输入n,求所有质因子幂的最小值.n奇大无比. 思路:先对n所有n开五次方根的质因子约完,然后如果没有除尽的话,因子最多也就4个了,所以幂数大于1的情况有p1^4,p1^3, p1^2 ...
- [CSP-S模拟测试]:weight(Kruskal+树链剖分)
题目描述 给你一个$n$个点$m$条边的带边权的无向图(无重边,无自环),现在对于每条边,问你这条边的权值最大可以是多大,使得这条边在无向图的所有最小生成树中?(边权都是整数). 输入格式 第一行包含 ...
- myeclipse2017使用总结
1.之前的myeclipse 2010项目导入后,需要配置项目发布内容,否则class.lib.web.xml等文件不会自动发布到tomcat中:
- Windows10下安装CentOS7双系统
参考: 参考1 参考2 问题1
- C语言基本数据类型大小
C语言基本数据类型占用的字节数可以通过如下例子获取: #include<stdio.h> int main(void) { printf("char size=%d \n&quo ...
- Eclipse Luna安装Hibernate Tools 4.2.3不显示,设置Eclipse运行的JDK
Eclipse Luna安装Hibernate Tools 4.2.3不显示,设置Eclipse运行的JDK,有需要的朋友可以参考下. eclipse-jee-luna-SR2中安装Hibernate ...