Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Create your own cursor at runtime
[ All Languages » VB »  Windows]

Total Hit ( 3320)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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  ' 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 retval As Long  ' counter and return value

Private Sub CreateMyCursor()
  '//Now for 32x32 Black-White Cursor we need 32 Bits x 32 Bits (4 Bytes for 32 lines => 128)

  'AND mask |XOR mask | Display
  '====================================
  '0    | 0    | Black
  '0    | 1    | White
  '1    | 0    | Screen
  '1    | 1    | Reverse screen

  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

  ' Finally, create this cursor! The hotspot is at (19,2) on the cursor.
  hNewCursor = CreateCursor(App.hInstance, 16, 16, 32, 32, andbits(0), xorbits(0))

End Sub

Sub DestroyMyCursor()
  If hNewCursor <> 0 Then
    '//change cursor back
    retval = SetCursor(hOldCursor)

    '//Destroy the new cursor.
    retval = DestroyCursor(hNewCursor)
  End If
End Sub

Private Sub Command1_Click()
  '//Destroy if any previous cursour
  DestroyMyCursor

  '//Now create our own cursor
  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

'//VB Automatically set default cursor for form and any other control on mouse move event
'//So we have to set custom cursor everytime mouse moves
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  hOldCursor = SetCursor(hNewCursor)  ' change cursor
End Sub

Private Sub Form_Unload(Cancel As Integer)
  DestroyMyCursor
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.