This is a function hCursor convert to Icon class.
Public Shared Function CursorToIcon(hCursor As IntPtr) As Icon
Dim info As New ICONINFO()
If GetIconInfo(hCursor, info) Then
Dim newIcon As IntPtr = CreateIconIndirect(info)
Return Icon.FromHandle(newIcon)
Else
Throw New Win32Exception()
End If
End Function
<StructLayout(LayoutKind.Sequential)>
Private Class ICONINFO
Public fIcon As Integer
Public xHotspot As Integer
Public yHotspot As Integer
Public hbmMask As IntPtr
Public hbmColor As IntPtr
End Class
<DllImport("User32.dll", SetLastError:=True)>
Private Shared Function GetIconInfo(hicon As IntPtr, iconinfo As ICONINFO) _
As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll")>
Private Shared Function CreateIconIndirect(iconinfo As ICONINFO) As IntPtr
End Function
How to use:
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Public Class Form1
<StructLayout(LayoutKind.Sequential)>
Private Class CURSORINFO
Public cbSize As Integer
Public flags As Integer
Public hCursor As IntPtr
Public ptScreenPos As System.Drawing.Point
Public Sub New()
cbSize = Marshal.SizeOf(Me)
End Sub
End Class
<DllImport("User32.dll", SetLastError:=True)>
Private Shared Function GetCursorInfo(pci As CURSORINFO) _
As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Invalidate()
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
Dim info As New CURSORINFO
If GetCursorInfo(info) Then
Using icon = CursorToIcon(info.hCursor)
e.Graphics.DrawIcon(icon, New Rectangle(Point.Empty, icon.Size))
End Using
Else
Throw New Win32Exception()
End If
End Sub
Public Shared Function CursorToIcon(hCursor As IntPtr) As Icon
Dim info As New ICONINFO()
If GetIconInfo(hCursor, info) Then
Dim newIcon As IntPtr = CreateIconIndirect(info)
Return Icon.FromHandle(newIcon)
Else
Throw New Win32Exception()
End If
End Function
<StructLayout(LayoutKind.Sequential)>
Private Class ICONINFO
Public fIcon As Integer
Public xHotspot As Integer
Public yHotspot As Integer
Public hbmMask As IntPtr
Public hbmColor As IntPtr
End Class
<DllImport("User32.dll", SetLastError:=True)>
Private Shared Function GetIconInfo(hicon As IntPtr, iconinfo As ICONINFO) _
As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll")>
Private Shared Function CreateIconIndirect(iconinfo As ICONINFO) As IntPtr
End Function
End Class