Zi 字媒體
2017-07-25T20:27:27+00:00
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-中如何實現「拖曳元件」/
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;
}
*/
}
}
寫了
5860316篇文章,獲得
23313次喜歡