How to get the handle of the mouse cursor icon with API (GetCursorInfo)

Mansour_Dalir 1,676 Reputation points
2024-05-18T15:37:53.0233333+00:00

hi

User's image

I need a function to put in the timer and check the status of the mouse cursor every 500 milliseconds.

    Private Declare Function GetCursorInfo Lib "user32" (ByRef pCI As cursorInfo) As Integer
    Public Structure POINTAPI
        Dim x As UInt32
        Dim y As UInt32
    End Structure
    Public Structure cursorInfo
        Dim cbSize As Integer
        Dim flags As Integer
        Dim hCursor As Integer
        Dim ptScreenPos As POINTAPI
    End Structure
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,471 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,625 questions
{count} votes

Accepted answer
  1. KOZ6.0 5,820 Reputation points
    2024-05-18T22:41:34.2833333+00:00

    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
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful