在运行时通过鼠标拖动移动控件位置及改变控件的大小

public class ReSize
    {
        bool IsMoving = false;
        int ctrlLastWidth = 0;
        int ctrlLastHeight = 0;
        int ctrlWidth;
        int ctrlHeight;
        int ctrlLeft;
        int ctrlTop;
        int cursorL;
        int cursorT;
        int ctrlLastLeft;
        int ctrlLastTop;
        int Htap;
        int Wtap;
        bool ctrlIsResizing = false;
        System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();
        private Control ctrl;
        private Form frm;
        public ReSize(Control c, Form frm)
        {
            ctrl = c;
            this.frm = frm;
            this.Htap = this.frm.Height - this.frm.ClientRectangle.Height;
            this.Wtap = this.frm.Width - this.frm.ClientRectangle.Width;
            ctrl.MouseDown += new MouseEventHandler(MouseDown);
            ctrl.MouseMove += new MouseEventHandler(MouseMove);
            ctrl.MouseUp += new MouseEventHandler(MouseUp);
        }
        private void MouseMove(object sender, MouseEventArgs e)
        {
            if (frm == null)
                return;
            if (e.Button == MouseButtons.Left)
            {
                if (this.IsMoving)
                {
                    if (ctrlLastLeft == 0)
                        ctrlLastLeft = ctrlLeft;
                    if (ctrlLastTop == 0)
                        ctrlLastTop = ctrlTop;
                    int locationX = (Cursor.Position.X - this.cursorL + this.frm.DesktopLocation.X + this.Wtap + this.ctrl.Location.X);
                    int locationY = (Cursor.Position.Y - this.cursorT + this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Location.Y);
                    if (locationX < this.frm.DesktopLocation.X + this.Wtap)
                        locationX = this.frm.DesktopLocation.X + this.Wtap;
                    if (locationY < this.frm.DesktopLocation.Y + this.Htap)
                        locationY = this.frm.DesktopLocation.Y + this.Htap;
                    this.ctrlLeft = locationX;
                    this.ctrlTop = locationY;
                    ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft, this.ctrlLastTop);
                    ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
                    ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
                    ctrlLastLeft = ctrlLeft;
                    ctrlLastTop = ctrlTop;
                    ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft, ctrlTop);
                    ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
                    ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
                    return;
                }
                int sizeageX = (Cursor.Position.X - this.frm.DesktopLocation.X - this.Wtap - this.ctrl.Location.X);
                int sizeageY = (Cursor.Position.Y - this.frm.DesktopLocation.Y - this.Htap - this.ctrl.Location.Y);
                if (sizeageX < 2)
                    sizeageX = 1;
                if (sizeageY < 2)
                    sizeageY = 1;
                ctrlWidth = sizeageX;
                ctrlHeight = sizeageY;
                if (ctrlLastWidth == 0)
                    ctrlLastWidth = ctrlWidth;
                if (ctrlLastHeight == 0)
                    ctrlLastHeight = ctrlHeight;
                if (ctrlIsResizing)
                {
                    ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X + this.ctrl.Left + this.Wtap, this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Top);
                    ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth, ctrlLastHeight);
                }
                ctrlIsResizing = true;
                ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
                ctrlLastWidth = ctrlWidth;
                ctrlLastHeight = ctrlHeight;
                ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X + this.Wtap + this.ctrl.Left, this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Top);
                ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
                ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
            }
        }
        private void MouseDown(object sender, MouseEventArgs e)
        {
            if (frm == null)
                return;
            if (e.X < this.ctrl.Width - 10 || e.Y < this.ctrl.Height - 10)
            {
                this.IsMoving = true;
                this.ctrlLeft = this.frm.DesktopLocation.X + this.Wtap + this.ctrl.Left;
                this.ctrlTop = this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Top;
                this.cursorL = Cursor.Position.X;
                this.cursorT = Cursor.Position.Y;
                this.ctrlWidth = this.ctrl.Width;
                this.ctrlHeight = this.ctrl.Height;
            }
            ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft, this.ctrlTop);
            ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
            ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
        }
        private void MouseUp(object sender, MouseEventArgs e)
        {
            if (frm == null)
                return;
            ctrlIsResizing = false;
            if (this.IsMoving)
            {
                ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft, this.ctrlTop);
                ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
                ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
                this.ctrl.Left = this.ctrlLeft - this.frm.DesktopLocation.X - this.Wtap;
                this.ctrl.Top = this.ctrlTop - this.frm.DesktopLocation.Y - this.Htap;
                this.IsMoving = false;
                this.ctrl.Refresh();
                return;
            }
            ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X + this.Wtap + this.ctrl.Left, this.frm.DesktopLocation.Y + this.Htap + this.ctrl.Top);
            ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
            ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
            this.ctrl.Width = ctrlWidth;
            this.ctrl.Height = ctrlHeight;
            this.ctrl.Refresh();
        }
    }

在 WPF 中,可以使用鼠标事件和 Canvas.SetLeft / Canvas.SetTop 方法来实现拖动控件在 Canvas 上移动的效果。以下是一个示例: ```xaml <Canvas> <Button Content="Drag me!" Canvas.Left="50" Canvas.Top="50" MouseLeftButtonDown="Button_MouseLeftButtonDown" MouseMove="Button_MouseMove" MouseLeftButtonUp="Button_MouseLeftButtonUp"/> </Canvas> ``` 在代码中,我们为 Button 控件注册了三个鼠标事件:MouseLeftButtonDown、MouseMove 和 MouseLeftButtonUp。这三个事件将分别在鼠标按下、移动和松开时触发。 ```csharp private bool isDragging; private Point startPoint; private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // 开始拖动 isDragging = true; startPoint = e.GetPosition(null); ((UIElement)sender).CaptureMouse(); } private void Button_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { // 计算拖动距离 Point mousePos = e.GetPosition(null); Vector diff = startPoint - mousePos; // 移动控件 Button button = sender as Button; double left = Canvas.GetLeft(button); double top = Canvas.GetTop(button); Canvas.SetLeft(button, left - diff.X); Canvas.SetTop(button, top - diff.Y); // 更新起始点 startPoint = mousePos; } } private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // 结束拖动 isDragging = false; ((UIElement)sender).ReleaseMouseCapture(); } ``` 在这个示例中,我们首先定义了两个变量:isDragging 和 startPoint。isDragging 变量表示当前是否正在拖动控件,startPoint 变量表示拖动开始时鼠标位置。 在 MouseLeftButtonDown 事件中,我们将 isDragging 设为 true,并记录当前鼠标位置。然后,我们调用 CaptureMouse 方法来捕获鼠标,以确保鼠标移动事件被正确处理。 在 MouseMove 事件中,我们首先判断当前是否正在拖动。如果是,就计算出当前鼠标位置和上一次鼠标位置的差值,然后使用 Canvas.SetLeft 和 Canvas.SetTop 方法更新控件位置。最后,我们将当前鼠标位置保存到 startPoint 变量中,以便下一次计算差值。 在 MouseLeftButtonUp 事件中,我们将 isDragging 设为 false,并调用 ReleaseMouseCapture 方法来释放鼠标捕获。 通过这种方式,我们就可以实现在 Canvas 上拖动控件移动的效果。如果需要支持多个控件同时拖动,可以使用类似的方法为每个控件注册鼠标事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值