在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
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

最新文章

  1. AngularJS 模块&amp; 表单
  2. Git命令----放弃本地修改使用服务器上的代码
  3. python更新后yum问题
  4. go语言 匿名变量
  5. SharePoint 更新文档库文档标题(Title)字段
  6. 暴力枚举 UVA 10976 Fractions Again?!
  7. 深入学习netty系列(1)
  8. easyui commbox嵌入一个checkbox的实现
  9. NET Core站点部署到Linux服务器
  10. asp.net webapi参数绑定
  11. [TYVJ] P1006 ISBN
  12. js面向对象学习总结
  13. [ios2]判断retina 屏幕
  14. Windows脚本相关
  15. Android支付——支付宝支付总结
  16. C++三目运算符的增强
  17. hihocoder--1384 -- Genius ACM (倍增 归并)
  18. Qt 编程指南 4 单行编辑控件
  19. kubectl命令自动补全
  20. jq 命名空间

热门文章

  1. 高阶 CSS 技巧在复杂动效中的应用
  2. csp每日习题
  3. Centos下使用containerd管理容器:5分钟从docker转型到containerd
  4. 华南理工大学 Python第7章课后小测-2
  5. C++ 左值引用与一级指针
  6. day03-代码实现02
  7. ssh访问控制,阻断异常IP,防止暴力破解
  8. MySQL集群搭建(5)-MHA高可用架构
  9. Pytorch及Yolov5环境配置及踩坑
  10. 220501 T1 困难的图论 (tarjan 点双)