十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
很久没有上这里了,今天看到了这个问题,尝试做了一个;
成都创新互联公司专注于迭部网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供迭部营销型网站建设,迭部网站制作、迭部网页设计、迭部网站官网定制、成都小程序开发服务,打造迭部网络公司原创品牌,更为您提供迭部网站排名全网营销落地服务。
本例以源文本框TextBox1全部文字作为拖放文字为例,实现拖放
1、向一个窗体中添加两个文本框,分别名为TextBox1,TextBox2。注意:把TextBox2控件的AllowDrop属性设置成True,这点不要遗漏。
2、完整的代码如下:
Public Class Form1
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
'设置一个标志以显示鼠标已按下。
MouseIsDown = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If MouseIsDown Then
'开始拖动(将TextBox1的文本内容作为拖放内容)。
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If
MouseIsDown = False
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
'检查正在被拖放的数据的格式。
If (e.Data.GetDataPresent(DataFormats.Text)) Then
'显示复制光标(表示是拖放行为)。
e.Effect = DragDropEffects.Copy
Else
'显示不放置光标(表示不是拖放行为)。
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
'粘贴文本(将拖放内容作为TextBox2的文本内容)。
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
End Class
设置全局变量:
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
假设你想拖动的是Panel1控件,以及此控件上的 Label1(用于显示标题)和PictureBox4(用于显示图标):
Private Sub TitleMove_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown, Label1.MouseDown, PictureBox4.MouseDown
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub
Private Sub TitleMove_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove, Label1.MouseMove, PictureBox4.MouseMove
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub TitleMove_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp, Label1.MouseUp, PictureBox4.MouseUp
drag = False
End Sub
这是一个简单的示例,你只需要新建一个项目,并在项目上放置一个文本框:TEXTBOX1,然后,将以下代码复制到代码区:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
End Sub
''建拖曳事件,实现文件拖放
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
Try
If e.Data.GetDataPresent(DataFormats.FileDrop) = True Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
Try
Dim filepaht As String() = e.Data.GetData(DataFormats.FileDrop)
For Each File As String In filepaht
textBox1.Text = File
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
如此,在文本框里将会出现被拖曳文件的路径信息。
VB6.0写的,代码很简单,无意中写成的。应该可以参考。不需要任何api函数。在无边框窗体顶部中放入一个label标签。然后用label的 mouse down 和mouse move事件实现
Dim a, b As Single
Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
a = X
b = Y
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Form1.Move Left + X - a, Top + Y - b
End If
End Sub