using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
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;
}
}
原文地址:http://www.cnblogs.com/ServiceboyNew/archive/2012/04/29/2476154.html
WinForm支持拖拽效果
最新推荐文章于 2024-04-19 10:51:42 发布