GetCursorInfo

rbulph

Well-known member
Joined
Feb 17, 2003
Messages
343
Im trying to get hold of basic information about the screen cursor. But the following doesnt work at all. What am I doing wrong?

Code:
Public Class Form1
    Private Declare Function GetCursorInfo Lib "user32.dll" (ByVal pc As CURSORINFO) As Long
    Idea to only click when cursor is a hand. Cant get it to work in .net. Works OK in VB6 though.
    Structure CURSORINFO
        Dim cbsize As Long
        Dim flags As Long
        Dim hCUrsor As Long
        Dim p As Point
    End Structure

    Structure PointAPI
        Dim X As Long
        Dim Y As Long
    End Structure
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim ff As New CURSORINFO

        ff.cbsize = System.Runtime.InteropServices.Marshal.SizeOf(GetType(CURSORINFO))
        GetCursorInfo(ff)
        With ff
            All of the following just show zero.
            Label1.Text = .p.X
            Label2.Text = .p.Y
            Label3.Text = .hCUrsor
        End With

    End Sub

End Class
 
This is probably what your declarations should look like:
Code:
    Private Declare Function GetCursorInfo Lib "user32.dll" ([COLOR="Red"]ByRef [/COLOR]pc As CURSORINFO) As [COLOR="Red"]Integer[/COLOR]

    Structure CURSORINFO
        Dim cbsize As [COLOR="Red"]Integer[/COLOR]
        Dim flags As [COLOR="Red"]Integer[/COLOR]
        Dim hCUrsor As [COLOR="Red"]Integer[/COLOR]
        Dim p As [COLOR="Red"]PointAPI[/COLOR]
    End Structure

    Structure PointAPI
        Dim X As [COLOR="Red"]Integer[/COLOR]
        Dim Y As [COLOR="Red"]Integer[/COLOR]
    End Structure
It seems like on the webs, most Windows API function declarations for VB are for VB6. A VB6 Long is the same thing as a DotNet Integer: 32-bits. A DotNet Long is 64-bits.

Also, the GetCursor function wants a pointer to the CURSORINFO (that is what the "p" in "pc" stands for). When you pass it ByRef, VB passes a pointer (i.e. "reference") to the object. If you pass it ByVal, it would be read-only and the function would be unable to return any data to you.
 
Back
Top