#Region "CreateIconIndirect"
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
<DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
Private Shared Function CreateIconIndirect( _
ByVal iconInfo As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function DestroyIcon( _
ByVal handle As IntPtr) As Boolean
End Function
<DllImport("gdi32.dll")> _
Public Shared Function DeleteObject( _
ByVal hObject As IntPtr) As Boolean
End Function
Private curPtr As IntPtr
Public Function CreateCursor(ByVal bmp As Bitmap) As Cursor
If _gCursorImage IsNot Nothing Then
_gCursorImage.Dispose()
End If
If curPtr <> IntPtr.Zero Then
DestroyIcon(curPtr)
End If
'Setup the Cursors IconInfo
Dim tmp As New IconInfo
tmp.xHotspot = _gHotSpotPt.X
tmp.yHotspot = _gHotSpotPt.Y
tmp.fIcon = False
If _gBlackBitBack Then
tmp.hbmMask = bmp.GetHbitmap(Color.FromArgb(0, 0, 0, 0))
tmp.hbmColor = bmp.GetHbitmap(Color.FromArgb(0, 0, 0, 0))
Else
tmp.hbmMask = bmp.GetHbitmap()
tmp.hbmColor = bmp.GetHbitmap()
End If
'Create the Pointer for the Cursor Icon
Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(tmp))
Marshal.StructureToPtr(tmp, pnt, True)
curPtr = CreateIconIndirect(pnt)
'Save the image of the cursor with the _gBlackBitBack effect
'Not really needed for normal use.
'I use it to create a screen shot with the gCursor included
_gCursorImage = Icon.FromHandle(curPtr).ToBitmap
'Clean Up
If pnt <> IntPtr.Zero Then DestroyIcon(pnt)
pnt = Nothing
If tmp.hbmMask <> IntPtr.Zero Then DeleteObject(tmp.hbmMask)
If tmp.hbmColor <> IntPtr.Zero Then DeleteObject(tmp.hbmColor)
tmp = Nothing
Return New Cursor(curPtr)
End Function
#End Region '