在Windows窗体上拖放

编程入门 行业动态 更新时间:2024-10-12 14:20:53
本文介绍了在Windows窗体上拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从Treeview节点拖动项目,并且在拖动时应在Windows窗体上创建可移动图片框的数量. 但是在我的程序中,我们也可以拖动一个父节点,并且只创建一个图片框. 这是我的代码:

I am trying to drag an item from treeview node and number of movable picturebox should be created on windows form as we drag. But in my programm, we can drag a parent node also, and only one picturebox is created. Here is my code:

Public Class Form1 Dim pic As New PictureBox Public drag As Boolean = False Public mousex, mousey As Integer Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop With pic .Size = New Size(50, 50) Me.Controls.Add(pic) .BackColor = Color.Black End With AddHandler pic.MouseDown, AddressOf pic_MouseDown AddHandler pic.MouseMove, AddressOf pic_Mousemove AddHandler pic.MouseUp, AddressOf pic_Mouseup End Sub Private Sub Form1_DragEnter1(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.AllowDrop = True End Sub Private Sub TreeView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragOver If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _ True) = False Then Exit Sub Dim selectedTreeview As TreeView = CType(sender, TreeView) Dim pt As Point = _ CType(sender, TreeView).PointToClient(New Point(e.X, e.Y)) Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt) If Not (selectedTreeview.SelectedNode Is targetNode) Then 'Select the node currently under the cursor selectedTreeview.SelectedNode = targetNode Dim dropNode As TreeNode = _ CType(e.Data.GetData("System.Windows.Forms.TreeNode"), _ TreeNode) Do Until targetNode Is Nothing If targetNode Is dropNode Then e.Effect = DragDropEffects.None Exit Sub End If targetNode = targetNode.Parent Loop End If e.Effect = DragDropEffects.Copy End Sub Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag DoDragDrop(e.Item, DragDropEffects.Copy) End Sub Private Sub pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = Windows.Forms.MouseButtons.Left Then Me.Cursor = Cursors.Hand drag = True mousex = -e.X mousey = -e.Y Dim left As Integer = Me.PointToClient(MousePosition).X - sender.location.x Dim right As Integer = Me.PointToClient(MousePosition).Y - sender.location.y Dim width As Integer = Me.ClientSize.Width - (sender.width - left) Dim height As Integer = Me.ClientSize.Height - (sender.width - right) Windows.Forms.Cursor.Clip = Me.RectangleToScreen(New Rectangle(left, right, width, height)) sender.invalidate() ElseIf e.Button = Windows.Forms.MouseButtons.Right Then Me.Controls.Remove(pic) End If End Sub Private Sub pic_Mousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) If drag Then Dim mposition As New Point() mposition = Me.PointToClient(MousePosition) mposition.Offset(mousex, mousey) sender.location = mposition End If End Sub Private Sub pic_Mouseup(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) drag = False Windows.Forms.Cursor.Clip = Nothing Me.Cursor = Cursors.Arrow sender.invalidate() End Sub End Class

推荐答案

尝试一下,我已经在两个事件中更改了代码,其余的实现是完整的. give a try to this, I have changed code in two events rest of the implementation is intact. Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag If (CType(e.Item, TreeNode).Parent Is Nothing) Then Dim nodes As TreeNodeCollection = CType(e.Item, TreeNode).Nodes For Each node As TreeNode In nodes DoDragDrop(node, DragDropEffects.Copy) Next Else DoDragDrop(e.Item, DragDropEffects.Copy) End If End Sub Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop pic = New PictureBox ' every time create a new pictureBox control With pic .Size = New Size(100, 100) .BackColor = Color.Black Me.Controls.Add(pic) End With AddHandler pic.MouseDown, AddressOf pic_MouseDown AddHandler pic.MouseMove, AddressOf pic_Mousemove AddHandler pic.MouseUp, AddressOf pic_Mouseup End Sub

更多推荐

在Windows窗体上拖放

本文发布于:2023-10-08 01:11:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   拖放   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!