Winform 实现控件点击拖拽移动位置

 winForm开发中有时会遇到需要设计自定义布局的情况,我们可以让用户自定义控件位置,最后保存这些位置作为预览方案。

使用方法

 new ControlMoveResize(button1,this);

辅助类代码如下:

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

namespace TestTool
{
    /// <summary>
    /// 使窗口的中的指定控件支持运行时移动
    /// TODO:运行时缩放
    /// </summary>
    public class ControlMoveResize
    {
        #region 私有成员
        bool isMoving = false;
        Point pCtrlLastCoordinate = new Point(0, 0);
        Point pCursorOffset = new Point(0, 0);
        Point pCursorLastCoordinate = new Point(0, 0);
        private Control ctrl = null;
        private ScrollableControl containe = null;
        #endregion
        #region 私有方法
        /// <summary>
        /// 在鼠标左键按下的状态记录鼠标当前的位置,以及被移动组件的当前位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MouseDown(object sender, MouseEventArgs e)
        {
            if (containe == null)
            {
                return;
            }
            if (e.Button == MouseButtons.Left)
            {
                isMoving = true;
                pCtrlLastCoordinate.X = ctrl.Left;
                pCtrlLastCoordinate.Y = ctrl.Top;
                pCursorLastCoordinate.X = Cursor.Position.X;
                pCursorLastCoordinate.Y = Cursor.Position.Y;

               
            }
        }
        private void MouseMove(object sender, MouseEventArgs e)
        {
            if (containe == null)
            {
                return;
            }

            if (e.Button == MouseButtons.Left)
            {
                if (this.isMoving)
                {
                    ctrl.BringToFront();
                    Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);

                    pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X;

                    pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y;
                    ctrl.Left = pCtrlLastCoordinate.X + pCursorOffset.X;
                    ctrl.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y;
                    ctrl.Parent.Refresh();
                }

            }
        }

        private void MouseUp(object sender, MouseEventArgs e)
        {
            if (containe == null)
            {
                return;
            }
            if (this.isMoving)
            {
                if (pCursorOffset.X == 0 && pCursorOffset.Y == 0)
                {
                    return;
                }
                if ((pCtrlLastCoordinate.X + pCursorOffset.X + ctrl.Width) > 0)
                {
                    ctrl.Left = pCtrlLastCoordinate.X + pCursorOffset.X;
                }
                else
                {
                    ctrl.Left = 0;
                }
                if ((pCtrlLastCoordinate.Y + pCursorOffset.Y + ctrl.Height) > 0)
                {
                    ctrl.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y;
                }
                else
                {
                    ctrl.Top = 0;
                }
                pCursorOffset.X = 0;
                pCursorOffset.Y = 0;
            }
            moveDownAction?.Invoke();
        }
        #endregion

        public Action moveDownAction;

        #region 构造函数
        /// <summary>
        /// 获取被移动控件对象和容器对象
        /// </summary>
        /// <param name="c">被设置为可运行时移动的控件</param>
        /// <param name="parentContain">可移动控件的容器</param>
        public ControlMoveResize(Control c, ScrollableControl parentContain)
        {
            ctrl = c;
            this.containe = parentContain;
            
            ctrl.MouseDown += new MouseEventHandler(MouseDown);
            ctrl.MouseMove += new MouseEventHandler(MouseMove);
            ctrl.MouseUp += new MouseEventHandler(MouseUp);
        }
        #endregion
    }
    }
 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

11eleven

你的鼓励是我创作的动力 !

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值