CreateCursor Function

Declare Function CreateCursor Lib "user32.dll" (ByVal hInstance As Long, ByVal nXhotspot As Long, ByVal nYhotspot As Long, ByVal nWidth As Long, ByVal nHeight As Long, lpANDbitPlane As Any, lpXORbitPlane As Any) As Long

Platforms: Win 32s, Win 95/98, Win NT

CreateCursor creates a new cursor. Its image is formed by using an AND mask and a XOR mask provided by the function, which are used to place the cursor's image wherever it appears. The AND and XOR masks can be provided using any numeric array, such as a Byte array (as the example below uses). The function also specifies the cursor's hotspot, the exact pixel which is considered to be the location of the cursor (such as the exact tip of the arrow cursor). Note that it is usually a better idea to put cursors into some sort of resource or separate file and load them instead of hard-wiring the cursors into the application code via this function. (This is because this function relies on creating a cursor compatible with the display device, instead of generating one from a file or resource which is.) Of course, the cursor size must be one supported by the system; use GetSystemMetrics to check. The cursor created by this function must later be destroyed by using DestroyCursor. The function returns 0 if an error occured, or a handle to the newly created cursor if successful.

hInstance
The instance handle of the application which is calling the function.
nXhotspot
The x-coordinate of the cursor's hotspot, relative to the cursor's upper-left corner.
nYhotspot
The y-coordinate of the cursor's hotspot, relative to the cursor's upper-left corner.
nWidth
The width in pixels of the cursor.
nHeight
The height in pixels of the cursor.
lpANDbitPlane
An array holding the AND mask for the cursor's image.
lpXORbitPlane
An array holding the XOR mask for the cursor's image.

Example:

' Create a 32x32 color cursor shaped somewhat like a yin-yang symbol.
' (The bit masks come from Microsoft's documentation on the API cursors function, just to
' give them their due credit.)  Note how the masks are loaded into the arrays.  The new
' cursor is then set to be the cursor for 10 seconds.
Dim hnewcursor As Long  ' newly created cursor
Dim holdcursor As Long  ' receives handle of default cursor
Dim andbuffer As String, xorbuffer As String  ' buffers for masks
Dim andbits(0 To 127) As Byte  ' stores the AND mask
Dim xorbits(0 To 127) As Byte  ' stores the XOR mask
Dim c As Integer, retval As Long  ' counter and return value

' Unfortunately, VB does not provide a nice way to load lots of information into an array.
' To load the AND and XOR masks, we put the raw hex values into the string buffers
' and use a loop to convert the hex values into numeric values and load them into
' the elements of the array.  Yes, it's ugly, but there's no better way.  Note the
' use of the line-continuation character here.  Each sequence of eight hex
' characters represents one line in the 32x32 cursor.
andbuffer = "FFFC3FFF" & "FFC01FFF" & "FF003FFF" & "FE00FFFF" & _
            "F701FFFF" & "F003FFFF" & "F003FFFF" & "E007FFFF" & _
            "C007FFFF" & "C00FFFFF" & "800FFFFF" & "800FFFFF" & _
            "8007FFFF" & "8007FFFF" & "0003FFFF" & "0000FFFF" & _
            "00007FFF" & "00001FFF" & "00000FFF" & "80000FFF" & _
            "800007FF" & "800007FF" & "C00007FF" & "C0000FFF" & _
            "E0000FFF" & "F0001FFF" & "F0001FFF" & "F8003FFF" & _
            "FE007FFF" & "FF00FFFF" & "FFC3FFFF" & "FFFFFFFF"
xorbuffer = "00000000" & "0003C000" & "003F0000" & "00FE0000" & _
            "0EFC0000" & "07F80000" & "07F80000" & "0FF00000" & _
            "1FF00000" & "1FE00000" & "3FE00000" & "3FE00000" & _
            "3FF00000" & "7FF00000" & "7FF80000" & "7FFC0000" & _
            "7FFF0000" & "7FFF8000" & "7FFFE000" & "3FFFE000" & _
            "3FC7F000" & "3F83F000" & "1F83F000" & "1F83E000" & _
            "0FC7E000" & "07FFC000" & "07FFC000" & "01FF8000" & _
            "00FF0000" & "003C0000" & "00000000" & "00000000"
' Now load these hex values into the proper arrays.
For c = 0 To 127
  andbits(c) = "&H" & Mid(andbuffer, 2 * c + 1)
  xorbits(c) = "&H" & Mid(xorbuffer, 2 * c + 1)
Next c

' Finally, create this cursor!  The hotspot is at (19,2) on the cursor.
hnewcursor = CreateCursor(App.hInstance, 19, 2, 32, 32, andbits(0), xorbits(0))
' Set the new cursor as the current cursor for 10 seconds and then switch back.
holdcursor = SetCursor(hnewcursor)  ' change cursor
Sleep 10000  ' wait for 10 seconds
retval = SetCursor(holdcursor)  ' change cursor back
' Destroy the new cursor.
retval = DestroyCursor(hnewcursor)

See Also: DestroyCursor
Category: Cursor

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/c/createcursor.html