拖拽生成控件副本
public class Form1 { //计数变量,说明输出了第N个Button private int count = 1; private void Form1_Load(System.Object sender, System.EventArgs e) { //窗体自身支持接受拖拽来的控件 this.AllowDrop = true; } private void Button1_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e) { //左键的话,标志位为true(表示拖拽开始) if ((e.Button == System.Windows.Forms.MouseButtons.Left)) { Button1.DoDragDrop(Button1, DragDropEffects.Copy | DragDropEffects.Move); //形成拖拽效果,移动+拷贝的组合效果 } } private void Form1_DragEnter(System.Object sender, System.Windows.Forms.DragEventArgs e) { //当Button被拖拽到WinForm上时候,鼠标效果出现 if ((e.Data.GetDataPresent(typeof(Button)))) { e.Effect = DragDropEffects.Copy; } } private void Form1_DragDrop(System.Object sender, System.Windows.Forms.DragEventArgs e) { //拖放完毕之后,自动生成新控件 Button btn = new Button(); btn.Size = Button1.Size; btn.Location = this.PointToClient(new Point(e.X, e.Y)); //用这个方法计算出客户端容器界面的X,Y坐标。否则直接使用X,Y是屏幕坐标 this.Controls.Add(btn); btn.Text = "按钮" + count.ToString(); count = count + 1; } public Form1() { DragDrop += Form1_DragDrop; DragEnter += Form1_DragEnter; Load += Form1_Load; } }
把圈子变小,把语言变干净,把成绩往上提,把故事往心里收,现在想要的以后你都会有。