WinForm拖拽控件生成副本的解决方法
作者:shichen2014 时间:2022-07-02 13:28:26
本文讲述了WinForm中实现拖拽效果的功能,即在WinForm中有一个Button,可以实现拖拽这个Button到目标位置后生成一个该控件的副本的功能。具体操作步骤如下:
要实现该功能主要分成如下三步:
1)确定被拖拽的对象:这里是Button(要使得Button被单击之后可以拖拽,那么必须处理其MouseDown事件,同时调用其DoDragDrop——该函数接受两个参数:i)要拖动的数据。ii)拖动的效果(该效果是2“目标位置”所能够接受的效果,是一个枚举值):
C#代码如下:
Button1.DoDragDrop(Button1, DragDropEffects.Copy || DragDropEffects.Move); //形成拖拽效果,移动+拷贝的组合效果
VB.NET页面代码如下:
Button1.DoDragDrop(Button1, DragDropEffects.Copy Or DragDropEffects.Move) '形成拖拽效果,移动+拷贝的组合效果
2)目标位置:这里是Form窗体自身。为了使得和Windows资源管理器中实现的文件拖拽效果一样(即拖拽一个文件到目标位置的中途,鼠标出现“+”号的效果)。那么应当处理DragEnter事件——即拖拽控件途中进入Form体内把效果设置成Copy的效果。
C#代码如下:
private void Form1_DragEnter(System.Object sender, System.Windows.Forms.DragEventArgs e)
{
//当Button被拖拽到WinForm上时候,鼠标效果出现
if ((e.Data.GetDataPresent(typeof(Button)))) {
e.Effect = DragDropEffects.Copy;
}
}
VB.NET页面代码如下:
Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
If (e.Data.GetDataPresent(GetType(Button))) Then '当Button被拖拽到WinForm上时候,鼠标效果出现
e.Effect = DragDropEffects.Copy
End If
End Sub
同时,为了使得Form自身支持接受拖拽传来的控件,必须设置其AllowDrag=True:
另外,一旦松开鼠标,那么拖拽过程结束。此时应当处理DragDrop事件,复制一个按钮:
C#代码如下:
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;
}
VB.NET页面代码如下:
Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
'拖放完毕之后,自动生成新控件
Dim btn As New Button
btn.Size = Button1.Size
btn.Location = Me.PointToClient(New Point(e.X, e.Y)) '用这个方法计算出客户端容器界面的X,Y坐标。否则直接使用X,Y是屏幕坐标
Me.Controls.Add(btn)
btn.Text = "按钮" + count.ToString
count = count + 1
End Sub
这里需要注意点:Location属性(指定控件放置位置的起始点)不能直接用e.X或e.Y——因为这是屏幕坐标,要根据实际的控件界面坐标进行适度转换,最简单方法是——PointToClient方法。
下面给出完整代码:
【界面如下所示】
C#代码如下:
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;
}
}
VB.NET页面代码如下:
Public Class Form1
'计数变量,说明输出了第N个Button
Private count As Integer = 1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True '窗体自身支持接受拖拽来的控件
End Sub
Private Sub Button1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
'左键的话,标志位为true(表示拖拽开始)
If (e.Button = Windows.Forms.MouseButtons.Left) Then
Button1.DoDragDrop(Button1, DragDropEffects.Copy Or DragDropEffects.Move) '形成拖拽效果,移动+拷贝的组合效果
End If
End Sub
Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
If (e.Data.GetDataPresent(GetType(Button))) Then '当Button被拖拽到WinForm上时候,鼠标效果出现
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
'拖放完毕之后,自动生成新控件
Dim btn As New Button
btn.Size = Button1.Size
btn.Location = Me.PointToClient(New Point(e.X, e.Y)) '用这个方法计算出客户端容器界面的X,Y坐标。否则直接使用X,Y是屏幕坐标
Me.Controls.Add(btn)
btn.Text = "按钮" + count.ToString
count = count + 1
End Sub
End Class
标签:WinForm,拖拽,控件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot在Controller层接收参数的n种姿势(超详细)
2023-01-28 00:54:39
![](https://img.aspxhome.com/file/2023/0/60600_0s.jpg)
Java 八种基本类型和基本类型封装类
2023-11-26 14:15:16
![](https://img.aspxhome.com/file/2023/8/59898_0s.jpg)
maven中配置项目的jdk版本无效的排查方式
2023-07-18 21:43:42
![](https://img.aspxhome.com/file/2023/9/57509_0s.png)
Java实现颜色渐变效果
2023-08-25 11:10:45
![](https://img.aspxhome.com/file/2023/2/58062_0s.png)
springboot如何使用logback-spring配置日志格式,并分环境配置
2023-11-10 04:37:34
![](https://img.aspxhome.com/file/2023/6/58766_0s.png)
Java JDBC连接数据库常见操作总结
2021-12-01 23:01:40
![](https://img.aspxhome.com/file/2023/6/63746_0s.png)
Java实现动态获取文件的绝对路径
2022-12-19 00:34:14
![](https://img.aspxhome.com/file/2023/2/61392_0s.png)
java反编译工具Bytecode-Viewer分享
2021-05-29 04:39:07
![](https://img.aspxhome.com/file/2023/9/69289_0s.png)
详解Java内部类与对象的打印概念和流程
2021-10-10 21:36:56
![](https://img.aspxhome.com/file/2023/2/71002_0s.png)
Java Servlet3.0异步处理问题
2023-08-12 00:52:05
SpringBoot框架RESTful接口设置跨域允许
2021-12-31 13:40:19
详解JAVA 内存管理
2023-01-13 04:35:51
![](https://img.aspxhome.com/file/2023/8/74748_0s.png)
IO中flush()函数的使用代码示例
2023-11-27 03:49:00
![](https://img.aspxhome.com/file/2023/4/60044_0s.gif)
Java实现解出世界最难九宫格问题
2022-06-14 19:47:10
Java多线程实现四种方式原理详解
2022-05-30 02:55:10
基于Hadoop实现Knn算法
2023-11-27 04:01:20
![](https://img.aspxhome.com/file/2023/8/60048_0s.jpg)
SpringBoot后端上传文件类型检测方式
2021-07-02 05:00:04
![](https://img.aspxhome.com/file/2023/6/63296_0s.jpg)
SpringBoot配置全局异常处理器捕获异常详解
2023-11-28 05:08:07
解析Java的Spring框架的BeanPostProcessor发布处理器
2021-11-21 17:16:13
![](https://img.aspxhome.com/file/2023/0/65170_0s.png)
Java详解Swing中的几种常用按钮的使用
2023-12-04 21:44:30
![](https://img.aspxhome.com/file/2023/7/76197_0s.png)