对于windows应用程序来说,控件之间是允许DragDrop的方法实现控件之间值拷贝等操作的,但是vb和.net的DragDrop的光标却完全不一样。说句心里话,本人更喜欢.net中的光标形式。
区别
vb:控件的外框
.NET:默认的光标下面加上一个小虚框
那么在.NET中想作出跟vb一样的效果该怎么做呢?其实很简单。请看下面的代码
Public
Class DragCursorCommon
Class DragCursorCommon
Private ico As Icon
Private newCursor As Cursor
Public Sub CreateDragCursor()Sub CreateDragCursor(ByVal ctl As Control, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim cur As Cursor = Cursors.Arrow
Dim bmp As Bitmap = New Bitmap(ctl.Width + 4, ctl.Height + 4)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawRectangle(New Pen(Color.Black, 2), 0, 0, ctl.Width, ctl.Height)
cur.Draw(g, New Rectangle(e.X, e.Y, 32, 32))
ico = Icon.FromHandle(bmp.GetHicon)
newCursor = New Cursor(ico.Handle)
Dim p As Point = newCursor.Position
p.X += ctl.Width / 2 - e.X
p.Y += ctl.Height / 2 - e.Y
newCursor.Position = p
End Sub
Public Function GetDragCursor()Function GetDragCursor() As Cursor
If Not newCursor Is Nothing Then
Return newCursor
End If
Return Cursors.Default
End Function
End Class
Private ico As Icon
Private newCursor As Cursor
Public Sub CreateDragCursor()Sub CreateDragCursor(ByVal ctl As Control, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim cur As Cursor = Cursors.Arrow
Dim bmp As Bitmap = New Bitmap(ctl.Width + 4, ctl.Height + 4)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawRectangle(New Pen(Color.Black, 2), 0, 0, ctl.Width, ctl.Height)
cur.Draw(g, New Rectangle(e.X, e.Y, 32, 32))
ico = Icon.FromHandle(bmp.GetHicon)
newCursor = New Cursor(ico.Handle)
Dim p As Point = newCursor.Position
p.X += ctl.Width / 2 - e.X
p.Y += ctl.Height / 2 - e.Y
newCursor.Position = p
End Sub
Public Function GetDragCursor()Function GetDragCursor() As Cursor
If Not newCursor Is Nothing Then
Return newCursor
End If
Return Cursors.Default
End Function
End Class
上面的代码是一个class,目的就是为了生成跟DragDrop源控件的外框一样的光标,这个Class的用法如下:
Dim
m_DragCursorCommon
As
New
DragCursorCommon
Private Sub TextBox2_MouseDown() Sub TextBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseDown, TextBox4.MouseDown, TextBox3.MouseDown
m_DragCursorCommon.CreateDragCursor(TextBox2, e)
TextBox2.DoDragDrop(TextBox2.Text, DragDropEffects.All)
Me.Cursor = Cursors.Default
End Sub
Private Sub TextBox2_GiveFeedback() Sub TextBox2_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles TextBox2.GiveFeedback, TextBox4.GiveFeedback, TextBox3.GiveFeedback
e.UseDefaultCursors = False
Me.Cursor = m_DragCursorCommon.GetDragCursor
End Sub
Private Sub TextBox2_MouseDown() Sub TextBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseDown, TextBox4.MouseDown, TextBox3.MouseDown
m_DragCursorCommon.CreateDragCursor(TextBox2, e)
TextBox2.DoDragDrop(TextBox2.Text, DragDropEffects.All)
Me.Cursor = Cursors.Default
End Sub
Private Sub TextBox2_GiveFeedback() Sub TextBox2_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles TextBox2.GiveFeedback, TextBox4.GiveFeedback, TextBox3.GiveFeedback
e.UseDefaultCursors = False
Me.Cursor = m_DragCursorCommon.GetDragCursor
End Sub
其实,我写的Class只能生成最简单的光标,大家可以自己更改光标的样子,比如说,半透明的图形等等。哈哈