原文地址: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# 在运行中拖拽,改变控件大小位置类(转载)的更多相关文章

  1. C++ MFC 改变控件大小和位置

    用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...

  2. c# asp.net 鼠标改变控件坐标位置,更改控件坐标,注册表保存读取,打印,查找局域网内打印机等等收集

    界面虽然被我弄的很难看,但功能还可以 里边注册表的路径自己设置一下,或者加一个创建注册表的语句,不然会报错 前台: <%@ Page Language="C#" AutoEv ...

  3. WPF中动态改变控件显示位置

    转自 http://blog.csdn.net/lassewang/article/details/6928897 测试环境: Windows XP/Windows 7 开发环境: Microsoft ...

  4. Android之动态改变控件大小

    利用getLayoutParams()方法和setLayoutParams()方法.三步曲:1.首先利用getLayoutParams()方法,获取控件的LayoutParams.eg:LayoutP ...

  5. pyqt5 动画学习(一) 改变控件大小

    入坑pyqt也有半年了,由于人们对事物的审美,静态界面已经不能满足用户,而动画却给人眼前一亮,so,今天来学习pyqt的动画了 由于资料真的是太少,本人也是有啃外国佬的英文进行摸索学习,可能也是触及皮 ...

  6. C#移动及改变控件大小

    //代码比较简单,就不多解析了. #region 移动窗体保存数据 Point mouseOff;//鼠标移动位置变量 bool leftFlag; //标志是否为左键 bool largeFlag; ...

  7. java实现控件的移动及使用鼠标改变控件大小

    package cn.com.test; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; ...

  8. C:\\MFC控件大小随窗体大小而改变

    一.大小和位置都改变 1.首先为窗体类添加CRect m_rect,该成员变量用来记录窗体的当前大小. 2.在类向导里面(Ctrl+W),为窗体添加消息WM_SIZE的响应函数OnSize(): 注意 ...

  9. BCGcontrolBar(五) 对话框大小改变控件自动适应

    改变控件大小 首先在 构造函数中加入 EnableLayout(); 在OnInitDialog()函数中加入 CBCGPStaticLayout* pLayout = (CBCGPStaticLay ...

随机推荐

  1. Flutter移动电商实战 --(49)详细页_Stack制作底部工具栏

    一直悬浮在最下面的 Stack层叠组件.里面用Row 可以横向布局 开始 stack如果想定位就要用position去定位. 修改return返回值的这个地方 大R刷新查看效果,可以看到固定的在左下角 ...

  2. LoadRunner11下载、安装与破解

    目前LoadRunner最新版本已经更新到LR12了,但是只有试用版本,最多只支持50人的并发,所以我们还是只能用LR11的破解版,毕竟商用 版是真的很贵,一般企业怕是都不会去购买. 1.LoadRu ...

  3. 问题:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile)

    一:问题 今天编译maven 项目构建失败,提示内容如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler ...

  4. springboot之freemarker 和thymeleaf模板web开发

    Spring Boot 推荐使用Thymeleaf.FreeMarker.Velocity.Groovy.Mustache等模板引擎.不建议使用JSP. 一.Spring Boot 中使用Thymel ...

  5. PorterDuffXfermode的模式取值

    PorterDuffXfermode(Mode mode) PorterDuff.mode.XXX取值有: 1.PorterDuff.Mode.CLEAR 所绘制不会提交到画布上. 2.PorterD ...

  6. 通过Redis的list来实现 Server - Client 的同步通信

    Redis实现类似同步方法调用的功能(一) 首先声明,这么干纯粹是为了好玩. 通常我们用Redis主要是为了存储一些数据,由于数据在内存里,所以查询更新很快.同时我们也可以利用 Pub/Sub 功能来 ...

  7. Java多线程中thread.getname()和thread.currentThread().getName();的去别

    首先要明白 this.XXX 的使用场景 使用Thread.currentThread().getName()和使用this.getName()和对象实例.getName(),都可以得到线程的名称,但 ...

  8. osg fbx 模型树结构

    void Test::printOsgGroup(osg::ref_ptr<osg::Group> &groupParam) { qDebug() <<groupPar ...

  9. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_14-认证接口开发-需求分析

    4 认证接口开发 4.1 需求分析 用户登录的流程图如下: 执行流程: 1.用户登录,请求认证服务 2.认证服务认证通过,生成jwt令牌,将jwt令牌及相关信息写入Redis,并且将身份令牌写入coo ...

  10. asp.net怎样实现批量下载文件(非打包形式下载)

    问题: 我想实现的是一个一个的下载. 比如我有一个文件列表.通过checkbox选择.通过单击下载按钮下载选中文件. 百度到都是用打包形式实现批量下载. 这是我自己写的代码,但是点击下载后只能下载一个 ...