C# 在运行中拖拽,改变控件大小位置类(转载)
原文地址:https://blog.csdn.net/zgke/article/details/3718989
copy的code
/// <summary>
/// 移动改变控件大小
/// zgke@sina.com
///qq: 116149
/// </summary>
public class ControlMove
{
#region 移动命令
private enum MoveCommand
{
None,
Move,
SizeLeft,
SizeRight,
SizeUp,
SizeDown,
SizeLeftUp,
SizeLeftDown,
SizeRightUp,
SizeRightDown
}
/// <summary>
/// 当前命令
/// </summary>
private MoveCommand m_MoveCommand = MoveCommand.None;
#endregion
private Control m_ParentControl;
private Control m_MoveControl;
private Point m_MousePoint = Point.Empty;
private Point m_MouseRight = Point.Empty;
private int m_SamillSizeTop = ;
private int m_SamillSizeLeft = ; public ControlMove(Control p_MoveControl)
{
if (p_MoveControl.Parent == null) return;
m_ParentControl = p_MoveControl.Parent;
m_MoveControl = p_MoveControl;
p_MoveControl.MouseDown += new MouseEventHandler(p_MoveControl_MouseDown);
p_MoveControl.MouseLeave += new EventHandler(p_MoveControl_MouseLeave);
p_MoveControl.MouseMove += new MouseEventHandler(p_MoveControl_MouseMove);
p_MoveControl.MouseUp += new MouseEventHandler(p_MoveControl_MouseUp);
System.Reflection.PropertyInfo _BorderStyleInfo = p_MoveControl.GetType().GetProperty("BorderStyle");
if (_BorderStyleInfo == null) return;
try
{
if ((BorderStyle)_BorderStyleInfo.GetValue(p_MoveControl, new object[] { }) == BorderStyle.Fixed3D) m_SamillSizeLeft = ;
}
catch
{
}
} void p_MoveControl_MouseUp(object sender, MouseEventArgs e)
{
m_MoveCommand = MoveCommand.None;
m_MoveControl.Cursor = Cursors.Hand;
if (MoveEnd != null) MoveEnd(m_MoveControl);
} void p_MoveControl_MouseMove(object sender, MouseEventArgs e)
{
if (m_MoveCommand == MoveCommand.None)
{
GetCursor(new Point(e.X, e.Y));
return;
}
switch (m_MoveCommand)
{
case MoveCommand.Move:
int _PointX = m_MousePoint.X - e.X;
int _PointY = m_MousePoint.Y - e.Y;
if (m_Min)
{
if (m_MoveControl.Location.X - _PointX <= ) _PointX = ;
if (m_MoveControl.Location.Y - _PointY <= ) _PointY = ;
}
if (m_Max)
{
if (m_MoveControl.Location.X - _PointX + m_MoveControl.Width >= m_ParentControl.Width) _PointX = ;
if (m_MoveControl.Location.Y - _PointY + m_MoveControl.Height >= m_ParentControl.Height) _PointY = ;
}
m_MoveControl.Location = new Point(m_MoveControl.Location.X - _PointX, m_MoveControl.Location.Y - _PointY);
break;
#region 上下左右
case MoveCommand.SizeRight:
if ((m_MoveControl.Width + e.X - m_MousePoint.X) < ) break;
if (m_Max)
{
int _Max = (m_MoveControl.Width + e.X - m_MousePoint.X) + m_MoveControl.Location.X;
if (_Max >= m_ParentControl.Width) break;
}
m_MoveControl.Width = m_MoveControl.Width + e.X - m_MousePoint.X;
m_MousePoint.X = e.X;
m_MousePoint.Y = e.Y;
break;
case MoveCommand.SizeDown:
if ((m_MoveControl.Height + e.Y - m_MousePoint.Y) < ) break;
if (m_Max)
{
int _Max = (m_MoveControl.Height + e.Y - m_MousePoint.Y) + m_MoveControl.Location.Y;
if (_Max >= m_ParentControl.Height) break;
}
m_MoveControl.Height = m_MoveControl.Height + e.Y - m_MousePoint.Y;
m_MousePoint.X = e.X;
m_MousePoint.Y = e.Y;
break;
case MoveCommand.SizeUp:
if ((m_MoveControl.Height - (e.Y - m_MousePoint.Y)) < ) break;
if (m_Min)
{
int _Mix = m_MoveControl.Top + (e.Y - m_MousePoint.Y);
if (_Mix < ) break;
}
m_MoveControl.Top = m_MoveControl.Top + (e.Y - m_MousePoint.Y);
m_MoveControl.Height = m_MoveControl.Height - (e.Y - m_MousePoint.Y);
break;
case MoveCommand.SizeLeft:
if ((m_MoveControl.Width - (e.X - m_MousePoint.X)) < ) break;
if (m_Min)
{
int _Mix = m_MoveControl.Left + e.X - m_MousePoint.X;
if (_Mix < ) break;
}
m_MoveControl.Left = m_MoveControl.Left + e.X - m_MousePoint.X;
m_MoveControl.Width = m_MoveControl.Width - (e.X - m_MousePoint.X);
break;
#endregion #region 四角
case MoveCommand.SizeLeftUp:
int _Left = m_MoveControl.Left + e.X - m_MousePoint.X;
int _Top = m_MoveControl.Top + (e.Y - m_MousePoint.Y);
int _Width = m_MoveControl.Width - (e.X - m_MousePoint.X);
int _Height = m_MoveControl.Height - (e.Y - m_MousePoint.Y);
if (_Width < ) //这里如果错误 换成直接返回break
{
_Width = ;
_Left = m_MoveControl.Left;
}
if (_Height < )
{
_Height = ;
_Top = m_MoveControl.Top;
}
if (m_Min)
{
if (_Left < )
{
_Left = ;
_Width = m_MoveControl.Width;
}
if (_Top < )
{
_Top = ;
_Height = m_MoveControl.Height;
}
}
m_MoveControl.Left = _Left;
m_MoveControl.Top = _Top;
m_MoveControl.Width = _Width;
m_MoveControl.Height = _Height;
break;
case MoveCommand.SizeRightDown:
if ((m_MoveControl.Width + e.X - m_MousePoint.X) < ) break;
if ((m_MoveControl.Height + e.Y - m_MousePoint.Y) < ) break;
if (m_Max)
{
int _Max = (m_MoveControl.Height + e.Y - m_MousePoint.Y) + m_MoveControl.Location.Y;
if (_Max >= m_ParentControl.Height) break;
_Max = (m_MoveControl.Width + e.X - m_MousePoint.X) + m_MoveControl.Location.X;
if (_Max >= m_ParentControl.Width) break;
}
m_MoveControl.Width = m_MoveControl.Width + e.X - m_MousePoint.X;
m_MoveControl.Height = m_MoveControl.Height + e.Y - m_MousePoint.Y;
m_MousePoint.X = e.X;
m_MousePoint.Y = e.Y; //'记录光标拖动的当前点
break;
case MoveCommand.SizeRightUp:
if ((m_MoveControl.Width + (e.X - m_MousePoint.X)) < ) break;
if ((m_MoveControl.Height - (e.Y - m_MouseRight.Y)) < ) break;
if (m_Min)
{
if ((m_MoveControl.Top + (e.Y - m_MouseRight.Y)) < ) break;
}
m_MoveControl.Top = m_MoveControl.Top + (e.Y - m_MouseRight.Y);
m_MoveControl.Width = m_MoveControl.Width + (e.X - m_MousePoint.X);
m_MoveControl.Height = m_MoveControl.Height - (e.Y - m_MouseRight.Y);
m_MousePoint.X = e.X;
m_MousePoint.Y = e.Y; //'记录光标拖动的当前点
break;
case MoveCommand.SizeLeftDown:
if ((m_MoveControl.Width - (e.X - m_MouseRight.X)) < ) break;
if ((m_MoveControl.Height + e.Y - m_MousePoint.Y) < ) break;
if (m_Min)
{
if ((m_MoveControl.Left + e.X - m_MouseRight.X) < ) break;
}
if (m_Max)
{
int _Max = (m_MoveControl.Height + e.Y - m_MousePoint.Y) + m_MoveControl.Location.Y;
if (_Max >= m_ParentControl.Height) break;
}
m_MoveControl.Left = m_MoveControl.Left + e.X - m_MouseRight.X;
m_MoveControl.Width = m_MoveControl.Width - (e.X - m_MouseRight.X);
m_MoveControl.Height = m_MoveControl.Height + e.Y - m_MousePoint.Y;
m_MousePoint.X = e.X;
m_MousePoint.Y = e.Y; //'记录光标拖动的当前点
break;
#endregion
}
} void p_MoveControl_MouseLeave(object sender, EventArgs e)
{
m_MoveControl.Cursor = Cursors.Default;
} void p_MoveControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
m_MoveCommand = GetCommand(new Point(e.X, e.Y));
m_MousePoint = new Point(e.X, e.Y);
m_MouseRight = new Point(e.X, e.Y);
switch (m_MoveCommand)
{
case MoveCommand.Move:
m_MoveControl.Cursor = Cursors.SizeAll;
break;
default:
break;
}
}
} /// <summary>
/// 根据鼠标位置获取执行的命令
/// </summary>
/// <param name="p_MousePoint"></param>
private MoveCommand GetCommand(Point p_MousePoint)
{
#region 四角
if (p_MousePoint.X <= m_SamillSizeTop && p_MousePoint.Y <= m_SamillSizeTop) if (m_Size) return MoveCommand.SizeLeftUp;
if (p_MousePoint.X <= m_SamillSizeTop && p_MousePoint.Y >= m_MoveControl.Height - m_SamillSizeLeft) if (m_Size) return MoveCommand.SizeLeftDown;
if (p_MousePoint.X >= m_MoveControl.Width - m_SamillSizeLeft && p_MousePoint.Y <= m_SamillSizeTop) if (m_Size) return MoveCommand.SizeRightUp;
if (p_MousePoint.X >= m_MoveControl.Width - m_SamillSizeLeft && p_MousePoint.Y >= m_MoveControl.Height - m_SamillSizeLeft) if (m_Size) return MoveCommand.SizeRightDown;
#endregion
#region 上下左右
if (p_MousePoint.X <= m_SamillSizeTop) if (m_Size) return MoveCommand.SizeLeft;
if (p_MousePoint.Y <= m_SamillSizeTop) if (m_Size) return MoveCommand.SizeUp;
if (p_MousePoint.X >= m_MoveControl.Width - m_SamillSizeLeft) if (m_Size) return MoveCommand.SizeRight;
if (p_MousePoint.Y >= m_MoveControl.Height - m_SamillSizeLeft) if (m_Size) return MoveCommand.SizeDown;
#endregion
if (m_Move) return MoveCommand.Move;
return MoveCommand.None;
} /// <summary>
/// 设置鼠标样式
/// </summary>
/// <param name="p_MousePoint"></param>
private void GetCursor(Point p_MousePoint)
{
MoveCommand _Command = GetCommand(p_MousePoint);
switch (_Command)
{
#region 四角
case MoveCommand.Move:
m_MoveControl.Cursor = Cursors.Hand;
return;
case MoveCommand.SizeLeftUp:
m_MoveControl.Cursor = Cursors.SizeNWSE;
return;
case MoveCommand.SizeLeftDown:
m_MoveControl.Cursor = Cursors.SizeNESW;
return;
case MoveCommand.SizeRightUp:
m_MoveControl.Cursor = Cursors.SizeNESW;
return;
case MoveCommand.SizeRightDown:
m_MoveControl.Cursor = Cursors.SizeNWSE;
return;
#endregion
#region 上下左右
case MoveCommand.SizeLeft:
m_MoveControl.Cursor = Cursors.SizeWE;
return;
case MoveCommand.SizeUp:
m_MoveControl.Cursor = Cursors.SizeNS;
return;
case MoveCommand.SizeRight:
m_MoveControl.Cursor = Cursors.SizeWE;
return;
case MoveCommand.SizeDown:
m_MoveControl.Cursor = Cursors.SizeNS;
return;
#endregion
}
} #region 属性
private bool m_Move = true;
/// <summary>
/// 是否能移动控见
/// </summary>
public bool Move { get { return m_Move; } set { m_Move = value; } }
private bool m_Min = true;
/// <summary>
/// 是否移动到最小区域
/// </summary>
public bool Min { get { return m_Min; } set { m_Min = value; } }
private bool m_Max = true;
/// <summary>
/// 是否移动到最大区域
/// </summary>
public bool Max { get { return m_Max; } set { m_Max = value; } }
private bool m_Size = true;
/// <summary>
/// 是否能设置大小
/// </summary>
public bool Size { get { return m_Size; } set { m_Size = value; } }
#endregion public delegate void ControlMoveEnd(Control sender);
public event ControlMoveEnd MoveEnd;
}
C# 在运行中拖拽,改变控件大小位置类(转载)的更多相关文章
- C++ MFC 改变控件大小和位置
用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...
- c# asp.net 鼠标改变控件坐标位置,更改控件坐标,注册表保存读取,打印,查找局域网内打印机等等收集
界面虽然被我弄的很难看,但功能还可以 里边注册表的路径自己设置一下,或者加一个创建注册表的语句,不然会报错 前台: <%@ Page Language="C#" AutoEv ...
- WPF中动态改变控件显示位置
转自 http://blog.csdn.net/lassewang/article/details/6928897 测试环境: Windows XP/Windows 7 开发环境: Microsoft ...
- Android之动态改变控件大小
利用getLayoutParams()方法和setLayoutParams()方法.三步曲:1.首先利用getLayoutParams()方法,获取控件的LayoutParams.eg:LayoutP ...
- pyqt5 动画学习(一) 改变控件大小
入坑pyqt也有半年了,由于人们对事物的审美,静态界面已经不能满足用户,而动画却给人眼前一亮,so,今天来学习pyqt的动画了 由于资料真的是太少,本人也是有啃外国佬的英文进行摸索学习,可能也是触及皮 ...
- C#移动及改变控件大小
//代码比较简单,就不多解析了. #region 移动窗体保存数据 Point mouseOff;//鼠标移动位置变量 bool leftFlag; //标志是否为左键 bool largeFlag; ...
- java实现控件的移动及使用鼠标改变控件大小
package cn.com.test; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; ...
- C:\\MFC控件大小随窗体大小而改变
一.大小和位置都改变 1.首先为窗体类添加CRect m_rect,该成员变量用来记录窗体的当前大小. 2.在类向导里面(Ctrl+W),为窗体添加消息WM_SIZE的响应函数OnSize(): 注意 ...
- BCGcontrolBar(五) 对话框大小改变控件自动适应
改变控件大小 首先在 构造函数中加入 EnableLayout(); 在OnInitDialog()函数中加入 CBCGPStaticLayout* pLayout = (CBCGPStaticLay ...
随机推荐
- Redis操作类型
- Mat转CImage
uchar *pImg=(uchar *)CI.GetBits();//得到CImage数据区地址 ps=img.ptr<uchar>(i); void MatToCImage( Mat ...
- Java中声明泛型方法
泛型是什么意思在这就不多说了,而Java中泛型类的定义也比较简单,例如:public class Test<T>{}.这样就定义了一个泛型类Test,在实例化该类时,必须指明泛型T的具体类 ...
- 简单的django登录项目---带views视图函数(脚本文件)---用Bootstrap
简单的django登录项目 1.首先建立工程,建立工程请参照:https://www.cnblogs.com/effortsing/p/10394511.html 2.在Firstdjango工程项目 ...
- JQ操作select项
jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change(function(){//code...}); //为Se ...
- Spring Aop(十六)——编程式的自定义Advisor
转发:https://www.iteye.com/blog/elim-2399437 https://www.iteye.com/blogs/subjects/springaop 编程式的自定义Adv ...
- AWS 云上安全最佳实践
目录 一.账号及访问管理 1.1.多 VPC 还是多账号模式 1.2.多账户模式,选择主 master 账号 二.系统架构安全 2.1.子网建议 2.2.每个可用区子网划分 2.3.安全组的建议 2. ...
- 岩哥带你飞——Spring 学习1&重要组件
------------------------------------------------------------------------------ 准备工作:1.创建一个web 项目 2.创 ...
- PCL学习(二)三维模型转点云 obj转pcd----PCL实现
#include <pcl/io/io.h> #include <pcl/io/pcd_io.h> #include <pcl/io/obj_io.h> #incl ...
- 【51nod】1776 路径计数
[51nod]1776 路径计数 我们先把前两种数给排好,排好之后会有\(a + b + 1\)个空隙可以填数,我们计算有\(k\)个空隙两端都是相同字母的方案数 可以用枚举把第二种数分成几段插进去来 ...