在Vb.net中,通过拖放事件,控件(按钮)从Panel移动到另一个Panel。
在光标移动时是否有任何方法或选项来绘制控件(按钮)?现在我只能更改光标形状,当拖放动作完成后,控件将在其新的面板父元素中绘制。
提前致谢。
编辑:添加了代码
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
答案 0 :(得分:1)
您必须在MouseDown事件上创建一个bmp /光标。然后,在GiveFeedback事件中,您必须禁用UseDefaultCursors,以使它们在移动鼠标后不会立即变回默认光标。然后,在DragOver事件中,将光标对象(在MouseDown中创建)设置为当前光标。如果当前光标以某种方式重置为另一个光标,这也将用于重新应用您的自定义光标。如果将光标移到指定的拖动区域之外,并且将其更改为“不能在此处放置”图标,则会发生这种情况。
您必须设置这些Subs来处理相应控件上的适当事件。同样,在设置位图时,“ c”是需要绘制的相关控件。这可能是发送者,也可能不是发送者,具体取决于您的情况,因此,由您确定当时要绘制哪个控件。
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