1. 程式人生 > >C#兩個控制元件同時移動示例(WinForm)

C#兩個控制元件同時移動示例(WinForm)

本人C#初學者,在實踐的過程中突發奇想,想要實現多個控制元件一起被拖動的案例,隨後經過多次測試之後終於有了成果。一下在移動的過程中會有矩形伴隨提示位置。如果您有更高階的用法請務必告知,在下感激不盡。



話不多說,以下為程式碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyTestForm
{
    /// <summary>
    /// 2018-1-11 蕭頔
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Point mouseDownPoint = Point.Empty;
        Point lblPoint = Point.Empty;
        Point tboxPoint = Point.Empty;
        int x, y;
        Rectangle rect = Rectangle.Empty;
        Rectangle rectLabel = Rectangle.Empty;
        bool isDrag = false;

        private void Form1_Paint(object sender,PaintEventArgs e)
        {
            if(rect != Rectangle.Empty)
            {
                if (isDrag)
                {
                    e.Graphics.DrawRectangle(Pens.Black, rect);
                    e.Graphics.DrawRectangle(Pens.Black, rectLabel);
                }
                else
                {
                    e.Graphics.DrawRectangle(new Pen(this.BackColor), rect);
                    e.Graphics.DrawRectangle(new Pen(this.BackColor), rectLabel);
                }
            }
        }

        private void tBox_MouseDown(object sender,MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                mouseDownPoint = e.Location;
                lblPoint = lbl.Location;
                tboxPoint = tBox.Location;
                x = tboxPoint.X - lblPoint.X;
                y = tboxPoint.Y - lblPoint.Y;
                rect = tBox.Bounds;
                rectLabel = lbl.Bounds;
            }
        }

        private void tBox_MouseMove(object sender,MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                isDrag = true;
                rect.Location = getPoint(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y));
                rectLabel.Location = getPoint(new Point(e.Location.X - mouseDownPoint.X - x, e.Location.Y -  mouseDownPoint.Y - y));
                this.Refresh();
            }
        }

        private void tBox_MouseUp(object sender,MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                if (isDrag)
                {
                    isDrag = false;
                    tBox.Location = rect.Location;
                    lbl.Location = rectLabel.Location;
                    this.Refresh();
                }
                reset();
            }
        }

        private void reset()
        {
            
            mouseDownPoint = Point.Empty;
            lblPoint = Point.Empty;
            tboxPoint = Point.Empty;
            rect = Rectangle.Empty;
            rectLabel = Rectangle.Empty;
            isDrag = false;
        }

        private Point getPoint(Point p)
        {
            return this.PointToClient(tBox.PointToScreen(p));
        }
    }
}