C# 滑鼠 拖拉元件/移動元件/改變元件位置[CS_ControlMoves]

C# 滑鼠 拖拉元件/移動元件/改變元件位置[CS_ControlMoves]

C# 滑鼠 拖拉元件/移動元件/改變元件位置[CS_ControlMoves]


GITHUB: https://github.com/jash-git/CS_ControlMoves


01_DragDrop – https://dotblogs.com.tw/yc421206/archive/2010/06/21/16039.aspx

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

namespace CS_ControlMoves
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button1_Click");
        }

        private void Main_Load(object sender, EventArgs e)
        {
            //step01-為每個控制項加事件
            
            foreach (Control ctrl in Controls)
            {
                ctrl.MouseMove += ctrl_MouseMove;
                ctrl.MouseDown += ctrl_MouseDown;
            }
        }

        void ctrl_MouseMove(object sender, MouseEventArgs e)//step02
        {
            Control ctrl = sender as Control;
            if (ctrl.Capture && e.Button == MouseButtons.Left)
            {
                DoDragDrop(ctrl, DragDropEffects.Move);//定義拖曳圖示
            }
        }

        private MouseEventArgs _pos = null;
        void ctrl_MouseDown(object sender, MouseEventArgs e)//step05
        {
            if (e.Button == MouseButtons.Left)
            {
                _pos = e;//按下時記錄位置
            }
        }

        /*
        private void Form1_DragDrop(object sender, DragEventArgs e)
        {

        }
        */
        private void Main_DragDrop(object sender, DragEventArgs e)//step04
        {
            if (_ctrl != null)
            {
                _ctrl.Top = this.PointToClient(new Point(e.X, e.Y)).Y;
                _ctrl.Left = this.PointToClient(new Point(e.X, e.Y)).X;
            }
        }

        Control _ctrl = null;
        //存放被拖曳的控制項
        /*
        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
        }
        */
        private void Main_DragEnter(object sender, DragEventArgs e)//step03
        {
            //取出被拖曳的控制項
            _ctrl = e.Data.GetData(e.Data.GetFormats(true)[0]) as Control;
            if (_ctrl != null)
                e.Effect = (_ctrl == null) ? DragDropEffects.None : DragDropEffects.Move;
        }
    }
}


02_MouseDownMouseMove – https://georgiosky2000.wordpress.com/2014/04/28/c-winform-%E4%B8%AD%E5%A6%82%E4%BD%95%E5%AF%A6%E7%8F%BE%E3%80%8C%E6%8B%96%E6%9B%B3%E5%85%83%E4%BB%B6%E3%80%8D/

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

namespace CS_ControlMoves
{
    public partial class main : Form
    {
        public main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button1_Click");
        }

        private void main_Load(object sender, EventArgs e)
        {
            foreach (Control ctrl in Controls)
            {
                if (ctrl.Name != "pictureBox1")//底圖不綁選擇事件
                {
                    ctrl.MouseDown += ctrl_MouseDown;
                }
            }
            pictureBox1.MouseMove += ctrl_MouseMove;

            
            blnselect = false;
            ctrlbuf = null;
        }

        bool blnselect;
        Control ctrlbuf;
        public void ctrl_MouseDown(object sender, MouseEventArgs e)
        {
            Control ctrl = sender as Control;
            if (ctrl.Capture==true && e.Button == MouseButtons.Left)//選擇物件
            {
                ctrlbuf = ctrl;
                blnselect = true;
            }
            
            if (e.Button == MouseButtons.Right)//放開物件
            {
                blnselect = false;
                ctrlbuf = null;
            }
            
        }
        public void ctrl_MouseMove(object sender, MouseEventArgs e)
        {
            if ((blnselect == true) && (ctrlbuf != null))
            {
                ctrlbuf.Top = e.Y;
                ctrlbuf.Left = e.X;
            }
        }

        /*
        public void ctrl_MouseUp(object sender, MouseEventArgs e)
        {

            blnselect = false;
            ctrlbuf = null;
        }
        */ 
    }
}

One thought on “C# 滑鼠 拖拉元件/移動元件/改變元件位置[CS_ControlMoves]

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *