Sometimes you might need to create your own cursor at runtime without using any image or resource. You can achive this functionality using CreateCursor API. Here is the basic guidelines for creating your own cursor. When you call CreateCursor you have to pass Application handle, Cursor Width, Cursor Height, Hot Spot X and Y Cordinates, array of AND Bits and XOR Bits.
In the cursor, a pixel called the hot spot marks the exact screen location that is affected by a mouse event, such as clicking a mouse button.
Here is the table to determine Cursor Appearance from AND and XOR bits |
ANDmask|XORmask|Display0|0|Black0|1|White1|0|Screen1|1|Reversescree Step-By-Step Example
- Create a standard exe project - Add 2 commandbutton controls - Add the following code in the form1 |
Click here to copy the following block | Private Declare Function CreateCursor Lib "user32" ( _ 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
Private Declare Function DestroyCursor Lib "user32" ( _ ByVal hCursor As Long) As Long
Private Declare Function SetCursor Lib "user32" ( _ ByVal hCursor As Long) As Long
Dim hNewCursor As Long Dim hOldCursor As Long Dim andbuffer As String, xorbuffer As String Dim andbits(0 To 127) As Byte Dim xorbits(0 To 127) As Byte Dim retval As Long
Private Sub CreateMyCursor()
andbuffer = "" andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,00,00,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "00,00,00,00," andbuffer = andbuffer & "00,00,00,00," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,7E,7E,FF," andbuffer = andbuffer & "FF,00,00,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF," andbuffer = andbuffer & "FF,FE,7F,FF"
xorbuffer = "" xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,FF,FF,00," xorbuffer = xorbuffer & "00,FF,FF,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,01,80,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00," xorbuffer = xorbuffer & "00,00,00,00"
Dim strAndBuffArr() As String Dim strXOrBuffArr() As String
strAndBuffArr = Split(andbuffer, ",") strXOrBuffArr = Split(xorbuffer, ",")
For i = 0 To 127 andbits(i) = Val("&H" & strAndBuffArr(i)) xorbits(i) = Val("&H" & strXOrBuffArr(i)) Next
hNewCursor = CreateCursor(App.hInstance, 16, 16, 32, 32, andbits(0), xorbits(0))
End Sub
Sub DestroyMyCursor() If hNewCursor <> 0 Then retval = SetCursor(hOldCursor)
retval = DestroyCursor(hNewCursor) End If End Sub
Private Sub Command1_Click() DestroyMyCursor
CreateMyCursor End Sub
Private Sub Command2_Click() DestroyMyCursor End Sub
Private Sub Form_Load() Command1.Caption = "Create Cursor" Command2.Caption = "Destroy Cursor"
CreateMyCursor End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) hOldCursor = SetCursor(hNewCursor) End Sub
Private Sub Form_Unload(Cancel As Integer) DestroyMyCursor End Sub |
|