`
coconut_zhang
  • 浏览: 543785 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

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

阅读更多

C# 在运行中改变控件大小的类 并获取最后控件的大小收藏
使用方式

   private Zgke.WindowFrom.Window.Controls.ControlMove _Move;
         private void Form1_Load(object sender, EventArgs e)
         {
         
             _Move = new Zgke.WindowFrom.Window.Controls.ControlMove(dataGridView1);

             _Move.Size = true;  //是否能改变控件大小
             _Move.Move = true;  //是否能移动控件
             _Move.Max = true;   //是否能移动大于窗体的位置
             _Move.Min = true;   //是否能移动到窗体的最前面

             _Move.MoveEnd += new Zgke.WindowFrom.Window.Controls.ControlMove.ControlMoveEnd(_Move_MoveEnd);
           
         }

         void _Move_MoveEnd(Control sender)
         {
             this.Text = sender.Location.ToString() + sender.Size.ToString();
         }
 

全部类


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Zgke.WindowFrom.Window.Controls
{
    /// <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 = 3;
        private int m_SamillSizeLeft = 5;


        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 = 8;
            }
            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 <= 0) _PointX = 0;
                        if (m_MoveControl.Location.Y - _PointY <= 0) _PointY = 0;
                    }
                    if (m_Max)
                    {
                        if (m_MoveControl.Location.X - _PointX + m_MoveControl.Width >= m_ParentControl.Width) _PointX = 0;
                        if (m_MoveControl.Location.Y - _PointY + m_MoveControl.Height >= m_ParentControl.Height) _PointY = 0;
                    }

                    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) < 10) 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) < 10) 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)) < 10) break;
                    if (m_Min)
                    {
                        int _Mix = m_MoveControl.Top + (e.Y - m_MousePoint.Y);
                        if (_Mix < 0) 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)) < 10) break;
                    if (m_Min)
                    {
                        int _Mix = m_MoveControl.Left + e.X - m_MousePoint.X;
                        if (_Mix < 0) 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 < 10)        //这里如果错误 换成直接返回break 
                    {
                        _Width = 10;
                        _Left = m_MoveControl.Left;
                    }
                    if (_Height < 10)
                    {
                        _Height = 10;
                        _Top = m_MoveControl.Top;
                    }

                    if (m_Min)
                    {
                        if (_Left < 0)
                        {
                            _Left = 0;
                            _Width = m_MoveControl.Width;

                        }
                        if (_Top < 0)
                        {
                            _Top = 0;
                            _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) < 10) break;
                    if ((m_MoveControl.Height + e.Y - m_MousePoint.Y) < 10) 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)) < 10) break;
                    if ((m_MoveControl.Height - (e.Y - m_MouseRight.Y)) < 10) break;
                    if (m_Min)
                    {
                        if ((m_MoveControl.Top + (e.Y - m_MouseRight.Y)) < 0) 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)) < 10) break;
                    if ((m_MoveControl.Height + e.Y - m_MousePoint.Y) < 10) break;

                    if (m_Min)
                    {
                        if ((m_MoveControl.Left + e.X - m_MouseRight.X) < 0) 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#编程中,开发用户界面(UI)时,经常需要实现控件在运行时的动态调整,包括改变控件的大小和位置。这能够提供更灵活的用户体验,让用户可以根据自己的需求自由布局。本篇文章将深入探讨如何在C#中实现这一功能。...

    c# 动态改变控件位置、控件大小、拖动控件

    在C#编程中,动态改变控件的位置、大小以及实现拖动功能是常见的需求,尤其在设计用户界面(UI)时。以下将详细介绍如何通过C#实现这些功能,并结合提供的"Panel"控件来举例说明。 1. **控件位置的动态改变**: 在...

    C#控件拖动与改变大小

    "C#控件拖动与改变大小" 在本文中,我们将讨论如何在 C# 中实现控件的拖动和改变大小。这种功能在 Visual Studio .Net 中非常有用,特别是在开发桌面应用程序时。我们可以在设计时从控件工具栏拖拽若干个控件放置到...

    C# 自由改变控件大小

    在C#编程中,自由改变控件大小是一个常见的需求,特别是在开发用户界面(UI)时。控件是Windows Forms或WPF应用程序中的基本构建块,它们可以是按钮、文本框、标签等。允许用户自由调整控件大小可以提高应用的交互性...

    利用C#更专业的实现运行时调整控件大小和位置

    在Windows应用程序开发中,我们经常需要在运行时动态地调整控件的大小和位置,以适应用户需求或界面响应。C#作为.NET框架的主要编程语言,提供了丰富的API和方法来帮助开发者实现这一功能。本文将深入探讨如何利用C#...

    C# winform 运行时用户拖动窗体上控件大小位置

    在C# WinForm开发中,实现用户在运行时能够自由拖动窗体上的控件以及改变它们的大小和位置是一项常见的需求,这有助于提供高度可定制的用户界面。以下是一些关于这个主题的重要知识点和详细步骤: 1. **事件处理**...

    c# 控件大小(及字体)随窗口大小改变自动缩放

    本篇文章将深入探讨如何实现C#控件大小及字体随窗口大小改变的自动缩放。 首先,我们要理解Windows Forms和WPF这两个C#中的主要UI框架如何处理控件的大小调整。在Windows Forms中,可以通过设置控件的Anchor和Dock...

    c#用鼠标改变控件大小实例

    在代码中,我们可以使用`SetBounds`方法来改变控件的大小,`Size`属性来获取或设置控件的尺寸,`Location`属性来获取或设置控件的位置。 压缩包中的"WindowsFormsApplication1"可能是一个包含了主应用程序的项目,...

    C# 窗体控件自动适应大小 cs类

    标题中提到的"C# 窗体控件自动适应大小 cs类"很可能是一个专门处理这种情况的类,它可能包含了一些方法,如OnResize事件处理程序,用于在窗体大小改变时动态计算和调整控件的位置和大小。 在事件处理中,开发者通常...

    C# WinForm 窗体中控件的大小自适应

    下载后解压缩即可运行使用,代码...3.为窗体添加SizeChanged事件,并在其方法Form1_SizeChanged中,首次记录窗体和其控件初始位置和大小,之后调用类的自适应方法,完成自适应。 4.Panel,DataGridView等控件同样适用。

    c# 通过控件名称获取控件(7种方法)

    c# 通过控件名称获取控件(7种方法)

    c#运行中,动态调整控件大小和位置

    在C#编程中,动态调整控件的大小和位置是一项常见的需求,特别是在创建自定义用户界面或者响应用户交互时。动态调整控件意味着在程序运行时,根据特定条件或事件改变控件的尺寸和在窗体上的布局。下面将详细探讨如何...

    c#运行中拖动调整控件

    "c#运行中拖动调整控件"这个主题涉及到的是如何让控件在程序运行时允许用户通过拖动来改变其大小,以及在控件外部点击时能够使控件失去焦点。这种功能可以提升用户体验,让用户能够根据自己的需求自由布局界面。 ...

    C#中根据窗口分辨率大小,自动调整控件位置和大小的函数

    在C#编程中,开发图形用户界面(GUI)应用程序时,确保控件能够根据不同的屏幕分辨率自适应地调整其大小和位置是一项重要的功能。这不仅提高了应用的用户体验,还增强了其跨平台兼容性。本文将深入探讨如何在C#中...

    C#控件大小(及字体)随窗口改变自动缩放

    综上所述,要实现C#控件大小及字体随窗口改变自动缩放,需要结合控件属性、布局管理、事件监听以及DPI感知等多个方面进行综合考虑。通过合理的编程和设计,可以创建出适应性强、用户体验良好的跨分辨率应用。

    C# winform 获取当前焦点控件

    C# winfrom获取当前焦点控件,有时候可能需要控制当前焦点控件是否是符合自己的要求而做其他动作.此代码可以获取到当前控件的类型名称

    C#实现根据指定容器和控件名字获得控件的方法

    在C#编程中,有时我们需要动态地访问或操作界面上的控件,特别是当控件数量众多或者嵌套层次复杂时。这时,能够根据指定的容器和控件名字获取到相应的控件就显得尤为重要。本文将详细讲解如何实现这个功能,以及其中...

    运行时改变控件的大小(这可是真正的改变控件大小哦)(35KB)

    标题"运行时改变控件的大小(这可是真正的改变控件大小哦)"所指的就是在应用程序运行过程中,根据需要调整控件的尺寸,以实现更灵活的用户界面设计。这种功能可以应用于多种场景,例如响应用户操作、适应不同设备...

    C#通过窗口名获取程序所有控件句柄

    在IT领域,尤其是在Windows应用程序开发中,经常需要与已运行的进程进行交互,例如获取某个程序的特定控件信息。本话题聚焦于使用C#语言通过窗口名来获取程序的所有控件句柄,并构建出句柄结构树。下面将详细阐述这...

    C#获取其他程序ListView控件中的内容

    在实际项目中,可以参考该文章并结合本文的知识点,来实现C#获取其他程序ListView控件内容的功能。 总结起来,要实现“C#获取其他程序ListView控件中的内容”,需要掌握进程和线程管理、窗口句柄操作以及Win32 API...

Global site tag (gtag.js) - Google Analytics