在winform项目开发中,偶尔需要用到边框拖拽。度娘也没找到相关的轮子(可能是我不配,没推给我)。只能自己造一个

上效果图(鼠标没录制上,问题不大)

上代码

  private void Form1_Load(object sender, EventArgs e)
{//使用方式
panelLeft.SetContorlMove(this, ContorlMove.Left);
panelRight.SetContorlMove(this, ContorlMove.Right);
panelTop.SetContorlMove(this, ContorlMove.Top);
panelDown.SetContorlMove(this, ContorlMove.Down);
dataGridView1.SetContorlMove(this, ContorlMove.All);
}

  

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace MoveControlBorder
{
public static class ContorlExt
{
#region 设置控件拖大拖小
public static void SetContorlMove(this DataGridView con, Form form, ContorlMove moveEnum, Size? maxSize = null, Size? minSize = null)
{
new SetContorlMove(con, form, moveEnum, maxSize, minSize);
}
public static void SetContorlMove(this Panel con, Form form, ContorlMove moveEnum, Size? maxSize = null, Size? minSize = null)
{
new SetContorlMove(con, form, moveEnum, maxSize, minSize);
}
#endregion
}
public enum ContorlMove
{
/// <summary>
/// 左边可以拉动宽度调整
/// </summary>
Left,
/// <summary>
/// 右边可以拉动宽度调整
/// </summary>
Right,
/// <summary>
/// 上边框可以拉动高度调整
/// </summary>
Top,
/// <summary>
/// 下边框可以拉动高度调整
/// </summary>
Down,
/// <summary>
/// 四个边都可以调整
/// </summary>
All
/// <summary>
/// 左上斜角
/// </summary>
//LeftTop,
//LeftDown,
//RightTop,
//RightDown
}
public class SetContorlMove
{
private Control CON;
private Form FORM;
private ContorlMove MOVEENUM;
private Size MaxSize;
private Size MinSize;
private bool IsAll = false;
public SetContorlMove(Control _con, Form _form, ContorlMove _moveEnum, Size? _maxSize, Size? _minSize)
{
CON = _con;
FORM = _form;
MOVEENUM = _moveEnum;
if (_moveEnum==ContorlMove.All)
{
IsAll = true;
}
if (_maxSize != null)
{
MaxSize = (Size)_maxSize;
}
else
{
MaxSize = new Size()
{
Height = 1000,
Width = 1000
};
}
if (_minSize != null)
{
MinSize = (Size)_minSize;
}
else
{
MinSize = new Size() { Height = 100, Width = 100 };
}
_con.MouseDown += new System.Windows.Forms.MouseEventHandler(MouseDown);
_con.MouseLeave += new System.EventHandler(MouseLeave);
_con.MouseMove += new System.Windows.Forms.MouseEventHandler(MouseMove);
_con.MouseUp += new System.Windows.Forms.MouseEventHandler(MouseUp);
}
private void MouseUp(object sender, MouseEventArgs e)
{
if (IsAll)
{
MOVEENUM = ContorlMove.All; Now_MOVEENUM = ContorlMove.All;
}
moveflag = false;
CON.Cursor = Cursors.Default;
}
bool moveflag = false;
ContorlMove Now_MOVEENUM = ContorlMove.All;
private void MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && (CON.Cursor == Cursors.SizeWE || CON.Cursor == Cursors.SizeNS))
{
if(MOVEENUM== ContorlMove.All)//判定当前按下了哪个的边
{
Point ms = Control.MousePosition;
Point p;
p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y));
if (ms.X > p.X - 5 && ms.X < p.X + 5)//left
{
if (ms.Y >= p.Y)
{
Now_MOVEENUM = ContorlMove.Left;
}
}
else if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)//top
{
if (ms.X >= p.X)
{
Now_MOVEENUM = ContorlMove.Top;
}
}
p = FORM.PointToScreen(new Point(CON.Location.X + CON.Width, CON.Location.Y));
if (ms.X > p.X - 5 && ms.X < p.X + 5)//right
{
if (ms.Y >= p.Y)
{
Now_MOVEENUM = ContorlMove.Right;
}
}
p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y + CON.Height)); if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)//down
{
if (ms.X >= p.X)
{
Now_MOVEENUM = ContorlMove.Down;
}
} } this.moveflag = true;
}
}
private void MouseMove(object sender, MouseEventArgs e)
{
Point ms = Control.MousePosition;
bool b = false;
Point p;
switch (MOVEENUM)
{
case ContorlMove.Left:
p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y));
if (ms.X > p.X - 5 && ms.X < p.X + 5)
{
if (ms.Y >= p.Y)
{
b = true;
CON.Cursor = Cursors.SizeWE;
}
}
break;
case ContorlMove.Right:
p = FORM.PointToScreen(new Point(CON.Location.X + CON.Width, CON.Location.Y));
if (ms.X > p.X - 5 && ms.X < p.X + 5)
{
if (ms.Y >= p.Y)
{
b = true;
CON.Cursor = Cursors.SizeWE;
}
}
break;
case ContorlMove.Top:
p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y)); if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)
{
if (ms.X >= p.X)
{
b = true;
CON.Cursor = Cursors.SizeNS;
}
}
break;
case ContorlMove.Down:
p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y + CON.Height)); if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)
{
if (ms.X >= p.X)
{
b = true;
CON.Cursor = Cursors.SizeNS;
}
}
break; case ContorlMove.All://设置鼠标
p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y));
if (ms.X > p.X - 5 && ms.X < p.X + 5)//left
{
if (ms.Y >= p.Y)
{
b = true;
CON.Cursor = Cursors.SizeWE;
break;
}
}
else if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)//top
{
if (ms.X >= p.X)
{
b = true;
CON.Cursor = Cursors.SizeNS;
break;
}
}
p = FORM.PointToScreen(new Point(CON.Location.X + CON.Width, CON.Location.Y));
if (ms.X > p.X - 5 && ms.X < p.X + 5)//right
{
if (ms.Y >= p.Y)
{
b = true;
CON.Cursor = Cursors.SizeWE;
break;
}
}
p = FORM.PointToScreen(new Point(CON.Location.X, CON.Location.Y + CON.Height)); if (ms.Y > p.Y - 5 && ms.Y < p.Y + 5)//down
{
if (ms.X >= p.X)
{
b = true;
CON.Cursor = Cursors.SizeNS; break;
}
}
break;
default:
break;
} if (!b && e.Button == MouseButtons.None)
{
CON.Cursor = Cursors.Default;
} if (e.Button == MouseButtons.Left && moveflag)
{
if (Now_MOVEENUM!=ContorlMove.All)
{
MOVEENUM = Now_MOVEENUM;
}
switch (MOVEENUM)
{
case ContorlMove.Left:
if (CON.Width + -e.X > MinSize.Width && CON.Width + -e.X < MaxSize.Width)
{
InvokeInt(WH.Width, -e.X);//修改宽度
}
break;
case ContorlMove.Right:
if (e.X > MinSize.Width && e.X < MaxSize.Width)
{
InvokeInt(WH.Width, e.X);//修改宽度
}
break;
case ContorlMove.Top:
if (CON.Height + -e.Y > MinSize.Height && CON.Width + -e.Y < MaxSize.Height)
{
InvokeInt(WH.Height, -e.Y);
}
break;
case ContorlMove.Down:
if (e.Y > MinSize.Height && e.Y < MaxSize.Height)
{
InvokeInt(WH.Height, e.Y);
}
break;
default:
break;
} }
}
private enum WH
{
Width,
Height
} private void InvokeInt(WH wh, int val)
{
if (CON.InvokeRequired)
{
Action<int> actionDelegate = (v) =>
{
switch (wh)
{
case WH.Width:
if (MOVEENUM == ContorlMove.Right)
{
CON.Width = v;
}
else
{
CON.Width += v;
CON.Location = new Point(CON.Location.X - v, CON.Location.Y); }
break;
case WH.Height:
if (MOVEENUM == ContorlMove.Down)
{
CON.Height = v;
}
else
{
CON.Height += v;
CON.Location = new Point(CON.Location.X, CON.Location.Y - v);
}
break;
default:
break;
}
};
CON.BeginInvoke(actionDelegate, val); //BeginInvoke方法是异步的, 它会另起一个子线程去完成工作线程
}
else
{
switch (wh)
{
case WH.Width:
if (MOVEENUM == ContorlMove.Right)
{
CON.Width = val;
}
else
{
CON.Width += val;
CON.Location = new Point(CON.Location.X - val, CON.Location.Y);
}
break;
case WH.Height:
if (MOVEENUM == ContorlMove.Down)
{
CON.Height = val;
}
else
{
CON.Height += val; CON.Location = new Point(CON.Location.X, CON.Location.Y - val); }
break;
default:
break;
}
}
}
private void MouseLeave(object sender, EventArgs e)
{
if (CON.Cursor == Cursors.SizeWE || CON.Cursor == Cursors.SizeNS)
{
CON.Cursor = Cursors.Default;
}
}
}
}

   作者:兮去┓( ´∀` )┏博客
出处:https://www.cnblogs.com/bklsj/p/16784749.html
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

C#winfrom调整任意控件宽和高的更多相关文章

  1. [转]C#鼠标拖动任意控件

    C#鼠标拖动任意控件(winform) 分类: c#2011-08-15 22:51 178人阅读 评论(0) 收藏 举报 winformc#userwindowsobjectapi using Sy ...

  2. 使用Aspose.Cell控件实现Excel高难度报表的生成(三)

    在之前几篇文章中,介绍了关于Apsose.cell这个强大的Excel操作控件的使用,相关文章如下: 使用Aspose.Cell控件实现Excel高难度报表的生成(一) 使用Aspose.Cell控件 ...

  3. 使用Aspose.Cell控件实现Excel高难度报表的生成(二)

    继续在上篇<使用Aspose.Cell控件实现Excel高难度报表的生成(一)>随笔基础上,研究探讨基于模板的Aspose.cell报表实现,其中提到了下面两种报表的界面,如下所示: 或者 ...

  4. 使用Aspose.Cell控件实现Excel高难度报表的生成

    1.使用Aspose.Cell控件实现Excel高难度报表的生成(一) http://www.cnblogs.com/wuhuacong/archive/2011/02/23/1962147.html ...

  5. android屏幕适配的全攻略3-动态获取手机屏幕宽高及动态设置控件宽高

    1.获取手机屏幕宽高: DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetr ...

  6. android获取屏幕宽高与获取控件宽高

    获取屏幕宽高 // 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素 ...

  7. winfrom获取用户控件里的控件对象

    如何获取用户控件里的控件对象呢,其实思路也是很简单的, 比如有一个panel 用户控件 里面有许多的其他控件. 那么要找出一个Label控件怎么找呢,好的.现在我们就开始 首先,一个foreach循环 ...

  8. Winfrom动态创建控件

    FlowLayoutPanel flowLayoutPanel1 = new FlowLayoutPanel();for (int i = 0; i < 9; i++){    Button b ...

  9. IOS中调整UI控件位置和尺寸

    1.frame(修改位置和尺寸):以父控件左上角为坐标原点,在其父控件中的位置和尺寸. //frame属性中的坐标点不能直接修改 CGRect tempFrame = self.v.frame; // ...

  10. 调整ListBox控件的行间距及设置文本格式

    首先要将该控件的DrawMode属性为OwnerDrawVariable 添加DrawItem重绘事件:private void listBox1_DrawItem(object sender, Dr ...

随机推荐

  1. Enable_hint_table 使用

    KingbaseES enable_hint_table 可以看成类似 oracle outline 工具,可以在不修改SQL 的情况下,通过hint 改变SQL 的执行计划. 一.启用enable_ ...

  2. 系统无法启动inaccessible boot device

    近日有一台Windows 2016遇到了系统无法启动的问题,出现错误inaccessible boot device.发现系统可以进入故障恢复模式,看来问题还不大.因为进入故障恢复模式的时候自动识别的 ...

  3. 图-kruskal算法,prim算法

    要求无向图 最小生成树: 连通性,累加和最小 并查集 结构 K算法 从最小的边开始,加上有没有形成环,没有就加,加上有环就不要 难点:如何判断加上一条边,有没有形成环. P算法 从点的角度开始

  4. 数论进阶&#160;

    数论进阶 扩展欧几里得算法 裴蜀定理(Bézout's identity) \(1\) :对于任意整数 \(a\),\(b\) ,存在一对整数 \(x\) ,\(y\) ,满足 \(ax+by=GCD ...

  5. Form表单数据

    官方文档地址:https://fastapi.tiangolo.com/zh/tutorial/request-forms/ 接收的不是 JSON,而是表单字段时,要使用 Form 要使用表单,需预先 ...

  6. Elasticsearch:使用_update_by_query更新文档

    转载自: https://blog.csdn.net/UbuntuTouch/article/details/105564270 在很多的情况下,我们我们想更新我们所有的文档: 添加一个新的field ...

  7. ingress-nginx自带认证功能【nginx自带】

    问题:通过nginx可以给某些web网站设置登录使用的用户名和密码,现在网站部署到k8s中,通过配置nginx-ingress->service->pod来访问的,怎么给这个网站再配置上访 ...

  8. DevExpress弹框、右键菜单、Grid的使用

    很重要!!!Dev为了区分winform的命名,会把一些新添加的属性放在Properties对象里!!找不到想要的属性,记得到里面找找哦! 一.下拉框 在这里假设我们的数据源是db.List(),在这 ...

  9. [基础] BS/CS 区别 Http/Https 区别 中间件请求

    BS和CS的区别:   1.BS结构:Browser-Server-从浏览器到服务器,浏览器打开的所有内容都属于BS(三大主流浏览器Safari.Chrome和Firefo)   2.CS结构:Cli ...

  10. 【面试题】Vue2动态添加路由 router.addRoute()

    Vue2动态添加路由 点击打开视频讲解更加详细 场景: 一般结合VueX和localstorage一起使用 router.addRoutes vue-router4后 已废弃:使用 router.ad ...