如何在拖放事件中移动时绘制控件 [英] How to draw control while is moved in a Drag-and-drop event

查看:25
本文介绍了如何在拖放事件中移动时绘制控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Vb.net 中,通过拖放事件,控件(按钮)从一个面板移动到另一个面板.

In Vb.net, through Drag and Drop events, a control (button) is moved from a Panel to another Panel.

有什么方法或选项可以在光标移动时绘制控件(按钮)?现在我只是改变了光标形状,当拖放操作完成时,控件被绘制在其新的面板父级中.

Is there any way or option to draw the control (button) while is being moved by cursor? Now I have only reached to change cursor shape, and when the action of Drag and Drop is finished, the control is drawn in its new panel parent.

提前致谢.

添加代码

public Sub New()
    InitializeComponent()
    '….
    Panel1.AllowDrop = True
    Panel2.AllowDrop = True

    AddHandler Panel1.DragEnter, AddressOf panel_DragEnter
    AddHandler Panel2.DragEnter, AddressOf panel_DragEnter

    AddHandler Panel1.DragDrop, AddressOf panel_DragDrop
    AddHandler Panel2.DragDrop, AddressOf panel_DragDrop

    AddHandler Button1.MouseDown, AddressOf button1_MouseDown

    Panel1.Controls.Add(Button1)

 End Sub

 Sub button1_MouseDown (ByVal sender As Object, e As MouseEventArgs)
     sender.dodragdrop(sender, DragDropEffects.Move)
 End Sub

 Sub panel_DragEnter (ByVal sender As Object, e As DragEventArgs)
     e.Effect = DragDropEffects.Move
 End Sub

 Sub panel_DragDrop (ByVal sender As Object, e As DragEventArgs)
     Dim aButton As Button = DirectCast(e.Data.GetData(GetType(Button)), Button)
     Dim aPanel As Panel = DirectCast(sender, Panel)

     button.Parent = aPanel
 End Sub

推荐答案

您必须在 MouseDown 事件上创建 bmp/cursor.然后,在 GiveFeedback 事件中,您必须禁用 UseDefaultCursors,以便它们在移动鼠标时不会立即变回默认光标.然后,在 DragOver 事件中,将光标对象(在 MouseDown 中创建)设置为当前光标.如果当前光标以某种方式重置为另一个光标,这也将用于重新应用您的自定义光标.如果您将光标移出指定的拖动区域,并且它变为不能将其放置在此处"图标,就会发生这种情况.

You have to create a bmp/cursor on the MouseDown event. Then, in the GiveFeedback event, you have to disable UseDefaultCursors so that it doesn't change back to the default cursor as soon as they move the mouse. Then, in the DragOver event, you set your cursor object (created in MouseDown) to be the current cursor. This will also serve to reapply your custom cursor if the current cursor got reset somehow to another cursor. This would happen if you moved your cursor off of the designated dragging area and it changed to the "can't drop it here" icon.

您必须设置这些 Subs 以处理相应控件上的适当事件.此外,在设置位图时,c"是需要绘制的相关控件.这可能是也可能不是发件人,具体取决于您的情况,因此您可以决定当时要绘制哪个控件.

You have to setup these Subs to Handle the appropriate events on the corresponding controls. Also, when setting up the bitmap, "c" is the control in question that it needs to draw. This may or may not be the sender, depending on your circumstances, so it's up to you to determine which control you want drawn at that time.

Private cur As Cursor

Private Sub GiveFeedback(sender As Object, e As GiveFeedbackEventArgs)
    e.UseDefaultCursors = False
End Sub

Private Sub MouseDown(sender As Object, e As MouseEventArgs)
    Dim bmp As Bitmap = New Bitmap(c.Width, c.Height)
    c.DrawToBitmap(bmp, New Rectangle(Point.Empty, bmp.Size))
    cur = New Cursor(bmp.GetHicon())
End Sub

Private Sub DragOver(sender As Object, e As DragEventArgs)
    If Cursor.Current <> cur Then Cursor.Current = cur
End Sub  

这篇关于如何在拖放事件中移动时绘制控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆