C#winfrom调整任意控件宽和高

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

posted @ 2022-11-21 17:49  兮去  阅读(760)  评论(0编辑  收藏  举报