C#控件拖拽及改变大小

15年初写的一个资源,代码今天下载编译成功,效果如下:

C#控件拖拽及改变大小

代码中具体使用代码如下,遍历界面中的控件,调用注册的相关事件,比如鼠标按下、鼠标移动、鼠标释放等:

foreach (Control ctl in this.Controls)
{
    LSQ.DragHelper.Instance.RegisterDragEvent(ctl);
}

核心代码如下:

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

namespace LSQ.DragHelper
{
    /// <summary>
    /// 拖拽帮助类,控件需要注册该类中的鼠标相关事件
    /// </summary>
    public class Instance
    {
        /// <summary>
        /// 光标状态
        /// </summary>
        private enum EnumMousePointPosition
        {
            无,
            左边框拉伸,
            左上角拉伸,
            上边框拉伸,
            右上角拉伸,
            右边框拉伸,
            右下角拉伸,
            下边框拉伸,
            左下角拉伸,
            拖拽
        }
        /// <summary>
        /// 控件原背景色
        /// </summary>
        private static Color oldColor = default(Color);
        /// <summary>
        /// 控件点击时使用的背景色
        /// </summary>
        private static Color controlFocusedBackcolor = Color.Red;
        /// <summary>
        /// 鼠标拖拽使用的原始点
        /// </summary>
        private static Point mouseDragOldPoint = default(Point);

        /// <summary>
        /// 鼠标上一次改变大小后的位置点
        /// </summary>
        private static Point mouseSizeLastPoint = default(Point);
        /// <summary>
        /// 绘制边框的画笔
        /// </summary>
        private static Pen borderPen = new Pen(Color.Black);
        /// <summary>
        /// 光标变化临界的宽度
        /// </summary>
        private const int BAND = 3;
        /// <summary>
        /// 控件最小宽度
        /// </summary>
        private const int MINWIDTH = 10;
        /// <summary>
        /// 控件最小高度
        /// </summary>
        private const int MINHEIGHT = 10;
        /// <summary>
        /// 当前鼠标操作
        /// </summary>
        private static EnumMousePointPosition mousePointPosition = EnumMousePointPosition.无;


        /// <summary>
        /// 注册控件拖拽事件
        /// </summary>
        /// <param name="ctl">拖拽区域控件</param>
        public static void RegisterDragEvent(Control ctl)
        {
            ctl.MouseDown += LSQ.DragHelper.Instance.DragControl_MouseDown;
            ctl.MouseUp += LSQ.DragHelper.Instance.DragControl_MouseUp;
            ctl.MouseMove += LSQ.DragHelper.Instance.DragControl_MouseMove;
        }

        /// <summary>
        /// 控件鼠标点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void DragControl_MouseDown(object sender, MouseEventArgs e)
        {
            Control currentControl = null;
            mouseDragOldPoint = mouseSizeLastPoint = e.Location;                        //保存控件当前位置

            currentControl = sender as Control;
            oldColor = currentControl.BackColor;        //保存背景色,鼠标弹起时恢复
            currentControl.BackColor = controlFocusedBackcolor;    //改变背景色
            //currentControl.Parent.Paint -= DragControlParent_Paint;  //绘制控件边框
            //currentControl.Parent.Paint += DragControlParent_Paint;
            //currentControl.Parent.Invalidate(true);
        }


        /// <summary>
        /// 鼠标在控件上移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void DragControl_MouseMove(object sender, MouseEventArgs e)
        {
            Control currentControl = null;

            currentControl = sender as Control;

            if (e.Button != MouseButtons.Left)
            {
                mousePointPosition = GetMousePointPosition(currentControl.Size, e);
                switch (mousePointPosition)
                {
                    case EnumMousePointPosition.左边框拉伸:
                    case EnumMousePointPosition.右边框拉伸:
                        Cursor.Current = Cursors.SizeWE;
                        break;
                    case EnumMousePointPosition.上边框拉伸:
                    case EnumMousePointPosition.下边框拉伸:
                        Cursor.Current = Cursors.SizeNS;
                        break;
                    case EnumMousePointPosition.左上角拉伸:
                    case EnumMousePointPosition.右下角拉伸:
                        Cursor.Current = Cursors.SizeNWSE;
                        break;
                    case EnumMousePointPosition.右上角拉伸:
                    case EnumMousePointPosition.左下角拉伸:
                        Cursor.Current = Cursors.SizeNESW;
                        break;
                    case EnumMousePointPosition.拖拽:
                        Cursor.Current = Cursors.SizeAll;
                        break;
                    default:
                        Cursor.Current = null;
                        break;
                }
                return;
            }

            switch (mousePointPosition)
            {
                case EnumMousePointPosition.拖拽:
                    currentControl.Left += e.X - mouseDragOldPoint.X;
                    currentControl.Top += e.Y - mouseDragOldPoint.Y;
                    break;
                case EnumMousePointPosition.下边框拉伸:
                    currentControl.Height += e.Y - mouseSizeLastPoint.Y;
                    mouseSizeLastPoint = e.Location;                    //记录光标拖动的当前点
                    break;
                case EnumMousePointPosition.右下角拉伸:
                    currentControl.Width += e.X - mouseSizeLastPoint.X;
                    currentControl.Height = e.Y - mouseSizeLastPoint.Y;
                    mouseSizeLastPoint = e.Location;                    //记录光标拖动的当前点
                    break;
                case EnumMousePointPosition.右边框拉伸:
                    currentControl.Width += e.X - mouseSizeLastPoint.X;
                    mouseSizeLastPoint = e.Location;                    //记录光标拖动的当前点
                    break;
                case EnumMousePointPosition.上边框拉伸:
                    currentControl.Top += e.Y - mouseDragOldPoint.Y;
                    currentControl.Height -= e.Y - mouseDragOldPoint.Y;
                    break;
                case EnumMousePointPosition.左边框拉伸:
                    currentControl.Left += e.X - mouseDragOldPoint.X;
                    currentControl.Width -= e.X - mouseDragOldPoint.X;
                    break;
                case EnumMousePointPosition.左下角拉伸:
                    currentControl.Left += e.X - mouseDragOldPoint.X;
                    currentControl.Width -= e.X - mouseDragOldPoint.X;
                    currentControl.Height += e.Y - mouseSizeLastPoint.Y;
                    mouseSizeLastPoint = e.Location;                    //记录光标拖动的当前点
                    break;
                case EnumMousePointPosition.右上角拉伸:
                    currentControl.Top += e.Y - mouseDragOldPoint.Y;
                    currentControl.Width += e.X - mouseSizeLastPoint.X;
                    currentControl.Height -= e.Y - mouseDragOldPoint.Y;
                    mouseSizeLastPoint = e.Location;                    //记录光标拖动的当前点
                    break;
                case EnumMousePointPosition.左上角拉伸:
                    currentControl.Left += e.X - mouseDragOldPoint.X;
                    currentControl.Top += e.Y - mouseDragOldPoint.Y;
                    currentControl.Width -= e.X - mouseDragOldPoint.X;
                    currentControl.Height -= e.Y - mouseDragOldPoint.Y;
                    break;
                default: break;
            }
            if (currentControl.Width < MINWIDTH)
            {
                currentControl.Width = MINWIDTH;
            }
            if (currentControl.Height < MINHEIGHT)
            {
                currentControl.Height = MINHEIGHT;
            }
        }


        /// <summary>
        /// 控件鼠标弹起事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void DragControl_MouseUp(object sender, MouseEventArgs e)
        {
            Control currentControl = null;

            currentControl = sender as Control;
            Cursor.Current = null;
            mousePointPosition = EnumMousePointPosition.无;
            currentControl.BackColor = oldColor;                     //恢复控件背景色

            //currentControl.Parent.Paint -= DragControlParent_Paint;  //取消绘制边框
        }

        /// <summary>
        /// 绘制控件边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void DragControlParent_Paint(object sender, PaintEventArgs e)
        {
            int borderLeft, borderRight, borderTop, borderBottom;
            Control currentControl = null;

            currentControl = sender as Control;
            return;
            if (currentControl == null)
            {
                return;
            }

            borderLeft = currentControl.Location.X - BAND;
            borderTop = currentControl.Location.Y - BAND;
            borderRight = currentControl.Location.X + currentControl.Width + BAND;
            borderBottom = currentControl.Location.Y + currentControl.Height + BAND;

            e.Graphics.DrawPolygon(borderPen, new Point[] {new Point(borderLeft+BAND/2,borderTop+BAND/2)
                                                ,new Point(borderRight-BAND/2,borderTop+BAND/2)
                                                ,new Point(borderRight-BAND/2,borderBottom-BAND/2)
                                                ,new Point(borderLeft+BAND/2,borderBottom-BAND/2)});

            e.Graphics.DrawEllipse(borderPen, new Rectangle(borderLeft, borderTop, BAND, BAND));                 //左上角圆圈
            e.Graphics.DrawEllipse(borderPen, new Rectangle(borderRight - BAND, borderTop, BAND, BAND));  //右上角圆圈
            e.Graphics.DrawEllipse(borderPen, new Rectangle(borderRight - BAND, borderBottom - BAND, BAND, BAND)); //右下角圆圈
            e.Graphics.DrawEllipse(borderPen, new Rectangle(borderLeft, borderBottom - BAND, BAND, BAND));  //左下角圆圈

            e.Graphics.DrawEllipse(borderPen, new Rectangle(currentControl.Location.X + currentControl.Width / 2 - BAND, borderTop, BAND, BAND));                 //上中圆圈
            e.Graphics.DrawEllipse(borderPen, new Rectangle(borderRight - BAND, (currentControl.Location.Y - currentControl.Height / 2 - BAND) / 2, BAND, BAND));                 //右中圆圈
            e.Graphics.DrawEllipse(borderPen, new Rectangle(currentControl.Location.X + currentControl.Width / 2 - BAND, borderBottom - BAND, BAND, BAND)); //下中圆圈
            e.Graphics.DrawEllipse(borderPen, new Rectangle(borderLeft, (currentControl.Location.Y - currentControl.Height / 2 - BAND) / 2, BAND, BAND)); //左中圆圈
        }

        /// <summary>
        /// 获取鼠标位置
        /// </summary>
        /// <param name="size">控件大小</param>
        /// <param name="e">鼠标位置</param>
        /// <returns>鼠标光标位置</returns>
        private static EnumMousePointPosition GetMousePointPosition(Size size, MouseEventArgs e)
        {
            if ((e.X < (-1 * BAND)) || (e.X > size.Width) || (e.Y < (-1 * BAND)) || (e.Y > size.Height))//光标越界
            {
                return EnumMousePointPosition.无;
            }

            if (e.X < BAND)
            {
                if (e.Y < BAND)
                {
                    return EnumMousePointPosition.左上角拉伸;
                }
                else
                {
                    if (e.Y > ((-1 * BAND) + size.Height))
                    {
                        return EnumMousePointPosition.左下角拉伸;
                    }
                    else
                    {
                        return EnumMousePointPosition.左边框拉伸;
                    }
                }
            }
            else
            {
                if (e.X > (-1 * BAND + size.Width))
                {
                    if (e.Y < BAND)
                    {
                        return EnumMousePointPosition.右上角拉伸;
                    }
                    else
                    {
                        if (e.Y > (-1 * BAND + size.Height))
                        {
                            return EnumMousePointPosition.右下角拉伸;
                        }
                        else
                        {
                            return EnumMousePointPosition.右边框拉伸;
                        }
                    }
                }
                else
                {
                    if (e.Y < BAND)
                    {
                        return EnumMousePointPosition.上边框拉伸;
                    }
                    else
                    {
                        if (e.Y > (-1 * BAND + size.Height))
                        {
                            return EnumMousePointPosition.下边框拉伸;
                        }
                        else
                        {
                            return EnumMousePointPosition.拖拽;
                        }
                    }
                }
            }
        }
    }
}

资源见链接: https://download.csdn.net/download/henrymoore/8349207

除非注明,文章均由 Dotnet9 整理发布,欢迎转载。

转载请注明:
作者:Dotnet9
链接:https://dotnet9.com/3083.html
来源:Dotnet9
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

发表评论

登录后才能评论

评论列表(1条)

  • 站长-Dotnet9
    Dotnet9 2019年12月10日 20:50

    Winform的代码,用于WPF只需要修改相应事件就可以了,没时间转。