.NET中DragDrop的光标

 

对于windows应用程序来说,控件之间是允许DragDrop的方法实现控件之间值拷贝等操作的,但是vb.netDragDrop的光标却完全不一样。说句心里话,本人更喜欢.net中的光标形式。

 

区别

  vb:控件的外框

   .NET:默认的光标下面加上一个小虚框

 

那么在.NET中想作出跟vb一样的效果该怎么做呢?其实很简单。请看下面的代码

 

Public   Class DragCursorCommon

    
Private ico As Icon
    
Private newCursor As Cursor

    
Public 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), 00, ctl.Width, ctl.Height)
        cur.Draw(g, 
New Rectangle(e.X, e.Y, 3232))
        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() 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(ByVal sender As ObjectByVal 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(ByVal sender As ObjectByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles TextBox2.GiveFeedback, TextBox4.GiveFeedback, TextBox3.GiveFeedback
        e.UseDefaultCursors 
= False
        
Me.Cursor = m_DragCursorCommon.GetDragCursor
    
End Sub

 

其实,我写的Class只能生成最简单的光标,大家可以自己更改光标的样子,比如说,半透明的图形等等。哈哈

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值