C# 在运行中改变控件大小的类 并获取最后控件的大小

26 篇文章 0 订阅

使用方式

  1.    private Zgke.WindowFrom.Window.Controls.ControlMove _Move;
  2.          private void Form1_Load(object sender, EventArgs e)
  3.          {
  4.          
  5.              _Move = new Zgke.WindowFrom.Window.Controls.ControlMove(dataGridView1);
  6.              _Move.Size = true;  //是否能改变控件大小
  7.              _Move.Move = true;  //是否能移动控件
  8.              _Move.Max = true;   //是否能移动大于窗体的位置
  9.              _Move.Min = true;   //是否能移动到窗体的最前面
  10.              _Move.MoveEnd += new Zgke.WindowFrom.Window.Controls.ControlMove.ControlMoveEnd(_Move_MoveEnd);
  11.            
  12.          }
  13.          void _Move_MoveEnd(Control sender)
  14.          {
  15.              this.Text = sender.Location.ToString() + sender.Size.ToString();
  16.          }

 

全部类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. namespace Zgke.WindowFrom.Window.Controls
  7. {
  8.     /// <summary>
  9.     /// 移动改变控件大小
  10.     /// zgke@sina.com
  11.     ///qq: 116149
  12.     /// </summary>
  13.     public class ControlMove
  14.     {
  15.         #region 移动命令
  16.         private enum MoveCommand
  17.         {
  18.             None,
  19.             Move,
  20.             SizeLeft,
  21.             SizeRight,
  22.             SizeUp,
  23.             SizeDown,
  24.             SizeLeftUp,
  25.             SizeLeftDown,
  26.             SizeRightUp,
  27.             SizeRightDown
  28.         }
  29.         /// <summary>
  30.         /// 当前命令
  31.         /// </summary>
  32.         private MoveCommand m_MoveCommand = MoveCommand.None;
  33.         #endregion
  34.         private Control m_ParentControl;
  35.         private Control m_MoveControl;
  36.         private Point m_MousePoint = Point.Empty;
  37.         private Point m_MouseRight = Point.Empty;
  38.         private int m_SamillSizeTop = 3;
  39.         private int m_SamillSizeLeft = 5;
  40.         public ControlMove(Control p_MoveControl)
  41.         {
  42.             if (p_MoveControl.Parent == nullreturn;
  43.             m_ParentControl = p_MoveControl.Parent;
  44.             m_MoveControl = p_MoveControl;
  45.             p_MoveControl.MouseDown += new MouseEventHandler(p_MoveControl_MouseDown);
  46.             p_MoveControl.MouseLeave += new EventHandler(p_MoveControl_MouseLeave);
  47.             p_MoveControl.MouseMove += new MouseEventHandler(p_MoveControl_MouseMove);
  48.             p_MoveControl.MouseUp += new MouseEventHandler(p_MoveControl_MouseUp);
  49.             System.Reflection.PropertyInfo _BorderStyleInfo = p_MoveControl.GetType().GetProperty("BorderStyle");
  50.             if (_BorderStyleInfo == nullreturn;
  51.             try
  52.             {
  53.                 if ((BorderStyle)_BorderStyleInfo.GetValue(p_MoveControl, new object[] { }) == BorderStyle.Fixed3D) m_SamillSizeLeft = 8;
  54.             }
  55.             catch
  56.             {
  57.             }
  58.         }
  59.         void p_MoveControl_MouseUp(object sender, MouseEventArgs e)
  60.         {
  61.             m_MoveCommand = MoveCommand.None;
  62.             m_MoveControl.Cursor = Cursors.Hand;
  63.             if (MoveEnd != null) MoveEnd(m_MoveControl);
  64.         }
  65.         void p_MoveControl_MouseMove(object sender, MouseEventArgs e)
  66.         {
  67.             if (m_MoveCommand == MoveCommand.None)
  68.             {
  69.                 GetCursor(new Point(e.X, e.Y));
  70.                 return;
  71.             }
  72.             switch (m_MoveCommand)
  73.             {
  74.                 case MoveCommand.Move:
  75.                     int _PointX = m_MousePoint.X - e.X;
  76.                     int _PointY = m_MousePoint.Y - e.Y;
  77.                     if (m_Min)
  78.                     {
  79.                         if (m_MoveControl.Location.X - _PointX <= 0) _PointX = 0;
  80.                         if (m_MoveControl.Location.Y - _PointY <= 0) _PointY = 0;
  81.                     }
  82.                     if (m_Max)
  83.                     {
  84.                         if (m_MoveControl.Location.X - _PointX + m_MoveControl.Width >= m_ParentControl.Width) _PointX = 0;
  85.                         if (m_MoveControl.Location.Y - _PointY + m_MoveControl.Height >= m_ParentControl.Height) _PointY = 0;
  86.                     }
  87.                     m_MoveControl.Location = new Point(m_MoveControl.Location.X - _PointX, m_MoveControl.Location.Y - _PointY);
  88.                     break;
  89.                 #region 上下左右
  90.                 case MoveCommand.SizeRight:
  91.                     if ((m_MoveControl.Width + e.X - m_MousePoint.X) < 10) break;
  92.                     if (m_Max)
  93.                     {
  94.                         int _Max = (m_MoveControl.Width + e.X - m_MousePoint.X) + m_MoveControl.Location.X;
  95.                         if (_Max >= m_ParentControl.Width) break;
  96.                     }
  97.                     m_MoveControl.Width = m_MoveControl.Width + e.X - m_MousePoint.X;
  98.                     m_MousePoint.X = e.X;
  99.                     m_MousePoint.Y = e.Y;
  100.                     break;
  101.                 case MoveCommand.SizeDown:
  102.                     if ((m_MoveControl.Height + e.Y - m_MousePoint.Y) < 10) break;
  103.                     if (m_Max)
  104.                     {
  105.                         int _Max = (m_MoveControl.Height + e.Y - m_MousePoint.Y) + m_MoveControl.Location.Y;
  106.                         if (_Max >= m_ParentControl.Height) break;
  107.                     }
  108.                     m_MoveControl.Height = m_MoveControl.Height + e.Y - m_MousePoint.Y;
  109.                     m_MousePoint.X = e.X;
  110.                     m_MousePoint.Y = e.Y;
  111.                     break;
  112.                 case MoveCommand.SizeUp:
  113.                     if ((m_MoveControl.Height - (e.Y - m_MousePoint.Y)) < 10) break;
  114.                     if (m_Min)
  115.                     {
  116.                         int _Mix = m_MoveControl.Top + (e.Y - m_MousePoint.Y);
  117.                         if (_Mix < 0) break;
  118.                     }
  119.                     m_MoveControl.Top = m_MoveControl.Top + (e.Y - m_MousePoint.Y);
  120.                     m_MoveControl.Height = m_MoveControl.Height - (e.Y - m_MousePoint.Y);
  121.                     break;
  122.                 case MoveCommand.SizeLeft:
  123.                     if ((m_MoveControl.Width - (e.X - m_MousePoint.X)) < 10) break;
  124.                     if (m_Min)
  125.                     {
  126.                         int _Mix = m_MoveControl.Left + e.X - m_MousePoint.X;
  127.                         if (_Mix < 0) break;
  128.                     }
  129.                     m_MoveControl.Left = m_MoveControl.Left + e.X - m_MousePoint.X;
  130.                     m_MoveControl.Width = m_MoveControl.Width - (e.X - m_MousePoint.X);
  131.                     break;
  132.                 #endregion
  133.                 #region 四角
  134.                 case MoveCommand.SizeLeftUp:
  135.                     int _Left = m_MoveControl.Left + e.X - m_MousePoint.X;
  136.                     int _Top = m_MoveControl.Top + (e.Y - m_MousePoint.Y);
  137.                     int _Width = m_MoveControl.Width - (e.X - m_MousePoint.X);
  138.                     int _Height = m_MoveControl.Height - (e.Y - m_MousePoint.Y);
  139.                     if (_Width < 10)        //这里如果错误 换成直接返回break 
  140.                     {
  141.                         _Width = 10;
  142.                         _Left = m_MoveControl.Left;
  143.                     }
  144.                     if (_Height < 10)
  145.                     {
  146.                         _Height = 10;
  147.                         _Top = m_MoveControl.Top;
  148.                     }
  149.                     if (m_Min)
  150.                     {
  151.                         if (_Left < 0)
  152.                         {
  153.                             _Left = 0;
  154.                             _Width = m_MoveControl.Width;
  155.                         }
  156.                         if (_Top < 0)
  157.                         {
  158.                             _Top = 0;
  159.                             _Height = m_MoveControl.Height;
  160.                         }
  161.                     }
  162.                     m_MoveControl.Left = _Left;
  163.                     m_MoveControl.Top = _Top;
  164.                     m_MoveControl.Width = _Width;
  165.                     m_MoveControl.Height = _Height;
  166.                     break;
  167.                 case MoveCommand.SizeRightDown:
  168.                     if ((m_MoveControl.Width + e.X - m_MousePoint.X) < 10) break;
  169.                     if ((m_MoveControl.Height + e.Y - m_MousePoint.Y) < 10) break;
  170.                     if (m_Max)
  171.                     {
  172.                         int _Max = (m_MoveControl.Height + e.Y - m_MousePoint.Y) + m_MoveControl.Location.Y;
  173.                         if (_Max >= m_ParentControl.Height) break;
  174.                         _Max = (m_MoveControl.Width + e.X - m_MousePoint.X) + m_MoveControl.Location.X;
  175.                         if (_Max >= m_ParentControl.Width) break;
  176.                     }
  177.                     m_MoveControl.Width = m_MoveControl.Width + e.X - m_MousePoint.X;
  178.                     m_MoveControl.Height = m_MoveControl.Height + e.Y - m_MousePoint.Y;
  179.                     m_MousePoint.X = e.X;
  180.                     m_MousePoint.Y = e.Y; //'记录光标拖动的当前点
  181.                     break;
  182.                 case MoveCommand.SizeRightUp:
  183.                     if ((m_MoveControl.Width + (e.X - m_MousePoint.X)) < 10) break;
  184.                     if ((m_MoveControl.Height - (e.Y - m_MouseRight.Y)) < 10) break;
  185.                     if (m_Min)
  186.                     {
  187.                         if ((m_MoveControl.Top + (e.Y - m_MouseRight.Y)) < 0) break;
  188.                     }
  189.                     m_MoveControl.Top = m_MoveControl.Top + (e.Y - m_MouseRight.Y);
  190.                     m_MoveControl.Width = m_MoveControl.Width + (e.X - m_MousePoint.X);
  191.                     m_MoveControl.Height = m_MoveControl.Height - (e.Y - m_MouseRight.Y);
  192.                     m_MousePoint.X = e.X;
  193.                     m_MousePoint.Y = e.Y; //'记录光标拖动的当前点
  194.                     break;
  195.                 case MoveCommand.SizeLeftDown:
  196.                     if ((m_MoveControl.Width - (e.X - m_MouseRight.X)) < 10) break;
  197.                     if ((m_MoveControl.Height + e.Y - m_MousePoint.Y) < 10) break;
  198.                     if (m_Min)
  199.                     {
  200.                         if ((m_MoveControl.Left + e.X - m_MouseRight.X) < 0) break;
  201.                     }
  202.                     if (m_Max)
  203.                     {
  204.                         int _Max = (m_MoveControl.Height + e.Y - m_MousePoint.Y) + m_MoveControl.Location.Y;
  205.                         if (_Max >= m_ParentControl.Height) break;
  206.                     }
  207.                     m_MoveControl.Left = m_MoveControl.Left + e.X - m_MouseRight.X;
  208.                     m_MoveControl.Width = m_MoveControl.Width - (e.X - m_MouseRight.X);
  209.                     m_MoveControl.Height = m_MoveControl.Height + e.Y - m_MousePoint.Y;
  210.                     m_MousePoint.X = e.X;
  211.                     m_MousePoint.Y = e.Y; //'记录光标拖动的当前点
  212.                     break;
  213.                 #endregion
  214.             }
  215.         }
  216.         void p_MoveControl_MouseLeave(object sender, EventArgs e)
  217.         {
  218.             m_MoveControl.Cursor = Cursors.Default;
  219.         }
  220.         void p_MoveControl_MouseDown(object sender, MouseEventArgs e)
  221.         {
  222.             if (e.Button == MouseButtons.Left)
  223.             {
  224.                 m_MoveCommand = GetCommand(new Point(e.X, e.Y));
  225.                 m_MousePoint = new Point(e.X, e.Y);
  226.                 m_MouseRight = new Point(e.X, e.Y);
  227.                 switch (m_MoveCommand)
  228.                 {
  229.                     case MoveCommand.Move:
  230.                         m_MoveControl.Cursor = Cursors.SizeAll;
  231.                         break;
  232.                     default:
  233.                         break;
  234.                 }
  235.             }
  236.         }
  237.         /// <summary>
  238.         /// 根据鼠标位置获取执行的命令
  239.         /// </summary>
  240.         /// <param name="p_MousePoint"></param>
  241.         private MoveCommand GetCommand(Point p_MousePoint)
  242.         {
  243.             #region 四角
  244.             if (p_MousePoint.X <= m_SamillSizeTop && p_MousePoint.Y <= m_SamillSizeTop) if (m_Size) return MoveCommand.SizeLeftUp;
  245.             if (p_MousePoint.X <= m_SamillSizeTop && p_MousePoint.Y >= m_MoveControl.Height - m_SamillSizeLeft) if (m_Size) return MoveCommand.SizeLeftDown;
  246.             if (p_MousePoint.X >= m_MoveControl.Width - m_SamillSizeLeft && p_MousePoint.Y <= m_SamillSizeTop) if (m_Size) return MoveCommand.SizeRightUp;
  247.             if (p_MousePoint.X >= m_MoveControl.Width - m_SamillSizeLeft && p_MousePoint.Y >= m_MoveControl.Height - m_SamillSizeLeft) if (m_Size) return MoveCommand.SizeRightDown;
  248.             #endregion
  249.             #region 上下左右
  250.             if (p_MousePoint.X <= m_SamillSizeTop) if (m_Size) return MoveCommand.SizeLeft;
  251.             if (p_MousePoint.Y <= m_SamillSizeTop) if (m_Size) return MoveCommand.SizeUp;
  252.             if (p_MousePoint.X >= m_MoveControl.Width - m_SamillSizeLeft) if (m_Size) return MoveCommand.SizeRight;
  253.             if (p_MousePoint.Y >= m_MoveControl.Height - m_SamillSizeLeft) if (m_Size) return MoveCommand.SizeDown;
  254.             #endregion
  255.             if (m_Move) return MoveCommand.Move;
  256.             return MoveCommand.None;
  257.         }
  258.         /// <summary>
  259.         /// 设置鼠标样式
  260.         /// </summary>
  261.         /// <param name="p_MousePoint"></param>
  262.         private void GetCursor(Point p_MousePoint)
  263.         {
  264.             MoveCommand _Command = GetCommand(p_MousePoint);
  265.             switch (_Command)
  266.             {
  267.                 #region 四角
  268.                 case MoveCommand.Move:
  269.                     m_MoveControl.Cursor = Cursors.Hand;
  270.                     return;
  271.                 case MoveCommand.SizeLeftUp:
  272.                     m_MoveControl.Cursor = Cursors.SizeNWSE;
  273.                     return;
  274.                 case MoveCommand.SizeLeftDown:
  275.                     m_MoveControl.Cursor = Cursors.SizeNESW;
  276.                     return;
  277.                 case MoveCommand.SizeRightUp:
  278.                     m_MoveControl.Cursor = Cursors.SizeNESW;
  279.                     return;
  280.                 case MoveCommand.SizeRightDown:
  281.                     m_MoveControl.Cursor = Cursors.SizeNWSE;
  282.                     return;
  283.                 #endregion
  284.                 #region 上下左右
  285.                 case MoveCommand.SizeLeft:
  286.                     m_MoveControl.Cursor = Cursors.SizeWE;
  287.                     return;
  288.                 case MoveCommand.SizeUp:
  289.                     m_MoveControl.Cursor = Cursors.SizeNS;
  290.                     return;
  291.                 case MoveCommand.SizeRight:
  292.                     m_MoveControl.Cursor = Cursors.SizeWE;
  293.                     return;
  294.                 case MoveCommand.SizeDown:
  295.                     m_MoveControl.Cursor = Cursors.SizeNS;
  296.                     return;
  297.                 #endregion
  298.             }
  299.         }
  300.         #region 属性
  301.         private bool m_Move = true;
  302.         /// <summary>
  303.         /// 是否能移动控见
  304.         /// </summary>
  305.         public bool Move { get { return m_Move; } set { m_Move = value; } }
  306.         private bool m_Min = true;
  307.         /// <summary>
  308.         /// 是否移动到最小区域
  309.         /// </summary>
  310.         public bool Min { get { return m_Min; } set { m_Min = value; } }
  311.         private bool m_Max = true;
  312.         /// <summary>
  313.         /// 是否移动到最大区域
  314.         /// </summary>
  315.         public bool Max { get { return m_Max; } set { m_Max = value; } }
  316.         private bool m_Size = true;
  317.         /// <summary>
  318.         /// 是否能设置大小
  319.         /// </summary>
  320.         public bool Size { get { return m_Size; } set { m_Size = value; } }
  321.         #endregion
  322.         public delegate void ControlMoveEnd(Control sender);
  323.         public event ControlMoveEnd MoveEnd;
  324.     }
  325.    
  326. }

  • 0
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值