Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Dim theText = e.X & " x " & e.Y
Dim textSize As Size = System.Windows.Forms.TextRenderer.MeasureText(theText, System.Drawing.SystemFonts.DefaultFont)
Dim TheWidth As Integer = 16 + textSize.Width + 1
Dim TheHeight As Integer = 16 + textSize.Height + 1
If TheWidth < Cursors.Default.Size.Width Then TheWidth = Cursors.Default.Size.Width
If TheHeight < Cursors.Default.Size.Height Then TheHeight = Cursors.Default.Size.Height
Using DragDropBitmap As New Bitmap(TheWidth, TheHeight)
Using g As Graphics = Graphics.FromImage(DragDropBitmap)
g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
'draw custom stuff here under cursor
'draw origional cursor
Cursors.Default.Draw(g, New Rectangle(0, 0, DragDropBitmap.Width, DragDropBitmap.Height))
'draw custom stuff here over cursor
g.TranslateTransform(16, 16)
Dim rect As New Rectangle(Point.Empty, textSize)
Using BGBrush As New SolidBrush(Color.FromKnownColor(KnownColor.Info))
g.FillRectangle(BGBrush, rect)
End Using
Using BorderPen As New Pen(Color.FromKnownColor(KnownColor.InfoText))
g.DrawRectangle(BorderPen, rect)
End Using
Using TextBrush As New SolidBrush(Color.FromKnownColor(KnownColor.InfoText))
g.DrawString(theText, System.Drawing.SystemFonts.DefaultFont, TextBrush, 0, 0)
End Using
'turn bitmap to cursor and set it to the cursor
Me.Cursor = CreateCursor(DragDropBitmap, Cursors.Default.HotSpot.X, Cursors.Default.HotSpot.Y)
End Using
End Using
End Sub
#Region "Cursor Stuff"
Private Structure IconInfo
Public fIcon As Boolean
Public xHotspot As Int32
Public yHotspot As Int32
Public hbmMask As IntPtr
Public hbmColor As IntPtr
End Structure
<System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32.dll", CharSet:=Runtime.InteropServices.CharSet.Auto)> _
Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("gdi32.dll")> _
Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
Public Shared Function CreateCursor(ByVal bmp As Bitmap, Optional ByVal xHotspot As Int32 = 0, Optional ByVal yHotspot As Int32 = 0) As System.Windows.Forms.Cursor
'Setup the Cursors IconInfo
Dim tmp As New IconInfo
tmp.xHotspot = xHotspot
tmp.yHotspot = yHotspot
tmp.fIcon = False
tmp.hbmMask = bmp.GetHbitmap()
tmp.hbmColor = bmp.GetHbitmap()
'Create the Pointer for the Cursor Icon
Dim pnt As IntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(tmp))
System.Runtime.InteropServices.Marshal.StructureToPtr(tmp, pnt, True)
Dim curPtr As IntPtr = CreateIconIndirect(pnt)
'Clean Up
DestroyIcon(pnt)
DeleteObject(tmp.hbmMask)
DeleteObject(tmp.hbmColor)
Return New System.Windows.Forms.Cursor(curPtr)
End Function
#End Region