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

原文地址:http://www.cnblogs.com/bklsj/p/16912142.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性