How to Create and Use a Custom Cursor in Visual Basic; Win SDK

This article was previously published under Q76666
This article has been archived. It is offered "as is" and will no longer be updated.
Using a graphics editor, the Microsoft Windows Software DevelopmentKit (SDK), and the Microsoft C compiler, you can create a dynamic-linklibrary (DLL) containing mouse cursors that can be used in a MicrosoftVisual Basic application. By making calls to the Windows API functionsLoadLibrary, LoadCursor, SetClassWord, and GetFocus, you can display acustom cursor from within a Visual Basic application. Below are the stepsnecessary to a create a custom cursor and a Visual Basic application to usethis custom cursor.
More information
Setting a custom cursor in a Visual Basic application requires a call tothe Windows API function LoadLibrary to load the custom DLL containing thecursor resource(s). A call to LoadCursor is then required to load a singlecursor contained in the DLL. The return value of the LoadCursor function isa handle to the custom cursor. This handle can be passed as an argument tothe API function SetClassWord with the constant GCW_HCURSOR. SetClassWordalso requires a window handle (hWnd) to the object (form or control) forwhich the cursor is to be set. The hWnd of a form is available via the hWndrun-time method. For example, the statement FWnd = Form1.hWnd will returnthe hWnd of Form1 to the variable FWnd. The hWnd of a control can beobtained by first using the SetFocus method on the control to give itthe input focus and then calling the API function GetFocus. GetFocusreturns the hWnd of the object with the current input focus.

A custom cursor always takes the place of the system cursor. TheMousePointer property of a form or control to receive the customcursor must be set to zero (system). Any other value for this propertywill result in the selected cursor being displayed, not the customcursor.

Because the cursor is defined as part of a window class, any change tothe window class will be reflected across any control or form thatuses that class. For example, if the MousePointer property for twocommand buttons is zero (system) and a custom cursor is set for one ofthe command buttons, both of the command buttons will receive thecustom cursor. To guarantee a custom cursor for each control requiresthat the cursor be set by calling SetClassWord in the MouseMove eventprocedure of the control.

Some controls, such as command buttons, do not contain a MouseMoveevent procedure. A custom mouse pointer for these types of controlscan be set by initiating a timer event. Within the timer event, callsto the API functions GetCursorPos and WindowFromPoint can be made todetermine if the mouse is over the control or not. If theWindowFromPoint API call returns the hWnd of the control, then themouse is over the control. A call to SetClassWord can then be made toset the custom cursor for the control.

Below is an example of using the Windows SDK and C Compiler to createa DLL containing cursor resources. Further below are the stepsnecessary to create a Visual Basic for Windows program to use the cursorresources.

If you do not have the Windows SDK but have a pre-compiled DLLcontaining cursor resources, skip to the steps below outlining how tocreate a Visual Basic application to use the custom cursor resources.
  1. Using a graphics editor such as Microsoft Windows SDK Paint program, create two cursor images. Save the images separately as CURS1.CUR and CURS2.CUR, respectively.
  2. Using any text editor, create a C source file containing the minimum amount of code for a Windows DLL. The source code must contain the functions LibEntry and WEP (Windows exit procedure). Below is an example of the C source file:
       #include <windows.h>   int _far _pascal LibMain (HANDLE hInstance,                             WORD wDataSeg,                             WORD cbHeapSize,                             LPSTR lpszCmdLine)   {      return(1);   }   int _far _pascal WEP (int nParameter)   {      return(1);   }
  3. Save the file created in step 2 above as CURSORS.C.
  4. Using any text editor, create a definition file (.DEF) for the DLL. Enter the following as the body of the .DEF file:
  5. Save the file created in step 4 above as CURSORS.DEF.
  6. Using a text editor, create a resource file for the cursors created in step 1 above. Enter the following as the body of the .RC file:
       Cursor1 CURSOR CURS1.CUR   Cursor2 CURSOR CURS2.CUR
  7. Save the file created in step 6 above as CURSORS.RC.
  8. Compile CURSORS.C from the MS-DOS command line as follows:

    CL /W2 /ALw /c /Od /GD /W2 CURSORS.C

  9. Link the program from the MS-DOS command line as follows (enter the following two lines on a single line):
    This will create the file CURSORS.EXE.
  10. Add the cursor resources created in step 1 above to the .EXE file created in step 9 above by invoking the Microsoft Resource Compiler (RC.EXE) from the MS-DOS command line as follows:
  11. Rename CURSORS.EXE to CURSORS.DLL from the MS-DOS command line as follows:
Below are the steps necessary to create a Visual Basic for Windowsapplication that uses the cursor resources created in the steps above.


When running the Visual Basic for Windows program created by followingthe steps below, it is important to terminate the application from thesystem menu, NOT the Run End option from the file menu. When Run End ischosen from the file menu, the unload event procedure is not executed.Therefore, the system cursor is not restored and the custom cursorwill remain present at design time. Using Visual Basic version 1.0 forWindows, avoid terminating the program from the Program Manager(PROGMAN.EXE) task list. The unload event procedure is also not calledwhen a program is terminated from the task list in Visual Basicversion 1.0 for Windows.
  1. Start Visual Basic for Windows, or from the File menu, choose New Project (press ALT, F, N) if Visual Basic for Windows is already running. Form1 will be created by default.
  2. Place a picture control (Picture1), command button (Command1), and timer control (Timer1) on Form1.
  3. Enter the following code in the Global Module:
       Type PointType      x As Integer      y As Integer   End Type
  4. Enter the following code in the General Declaration section of Form1:
       DefInt A-Z   ' Enter each of the following Declare statements as one, single line:   Declare Function LoadLibrary Lib "kernel" (ByVal LibName$)   Declare Function LoadCursor Lib "user"      (ByVal hInstance, ByVal CursorName$)   Declare Function SetClassWord Lib "user"      (ByVal hWnd, ByVal nIndex, ByVal NewVal)   Declare Function GetFocus Lib "user" ()   Declare Function PutFocus Lib "user" Alias "SetFocus" (ByVal hWnd)   Declare Sub GetCursorPos Lib "user" (p As PointType)   Declare Function WindowFromPoint Lib "user" (ByVal y, ByVal x)   Declare Sub FreeLibrary LIB "Kernel" (ByVal hLIbMod as Integer)   Const GCW_HCURSOR = (-12)   Dim DLLInstance as Long   Dim SysCursHandle   Dim Curs1Handle   Dim Curs2Handle   Dim Pic1hWnd   Dim Command1hWnd   Dim p As PointType
  5. Enter the following code in the Form_Load event procedure of Form1:
       Sub Form_Load ()      Form1.Show      DLLInstance = LoadLibrary("CURSORS.DLL")      Curs1Handle = LoadCursor(DLLInstance, "Cursor1")      Curs2Handle = LoadCursor(DLLInstance, "Cursor2")      SysCursHandle=SetClassWord(Form1.hWnd,GCW_HCURSOR,Curs2Handle)      ' Get the current control with the input focus.      CurrHwnd = GetFocus()      ' Get the Window handle of Picture1.      Picture1.SetFocus      Pic1hWnd = GetFocus()      ' Get the Window handle of Command1.      Command1.SetFocus      Command1hWnd = GetFocus()      ' Restore the focus to the control with the input focus.      r = PutFocus(CurrHwnd)      timer1.interval = 1   ' One millisecond.      timer1.enabled = -1   End Sub
  6. Enter the following code in the Form_Unload event procedure of Form1:
       Sub Form_Unload (Cancel As Integer)      ' Restore the custom cursors to the system cursor:      LastCursor =SetClassWord(Form1.hWnd, GCW_HCURSOR, SysCursHandle)      LastCursor = SetClassWord(Pic1hWnd, GCW_HCURSOR, SysCursHandle)      LastCursor=SetClassWord(Command1hWnd, GCW_HCURSOR,SysCursHandle)      FreeLibrary(DLLInstance)   End Sub
  7. Enter the following code in the Timer1_Timer event procedure of Timer1:
       Sub Timer1_Timer ()      ' Get the current (absolute) cursor position.      Call GetCursorPos(p)      ' Find out which control the midpoint of the cursor is over.      ' The cursor is 32 x 32 pixels square. Change the class word      ' of the control to the appropriate cursor.      Select Case WindowFromPoint(p.y + 16, p.x + 16)         Case Form1.hWnd            LastCursor=SetClassWord(Form1.hWnd, GCW_HCURSOR, Curs2Handle)            LastCursor=SetClassWord(Command1hWnd, GCW_HCURSOR, Curs2Handle)            LastCursor=SetClassWord(Pic1hWnd, GCW_HCURSOR, Curs2Handle)         Case Command1hWnd            LastCursor=SetClassWord(Form1.hWnd, GCW_HCURSOR, Curs1Handle)            LastCursor=SetClassWord(Command1hWnd, GCW_HCURSOR, Curs1Handle)         Case Pic1hWnd            LastCursor = SetClassWord(Form1.hWnd, GCW_HCURSOR, Curs1Handle)            LastCursor = SetClassWord(Pic1hWnd%, GCW_HCURSOR, Curs1Handle)      End Select   End Sub
  8. Run the program. The form should receive the "Cursor2" cursor and the controls Command1 and Picture1 should receive the "Cursor1" cursor as the mouse cursor is moved about the form.
2.00 3.00

Article ID: 76666 - Last Review: 10/26/2013 11:43:00 - Revision: 3.0

Microsoft Visual Basic 2.0 Standard Edition, Microsoft Visual Basic 3.0 Professional Edition, Microsoft Visual Basic 2.0 Professional Edition, Microsoft Visual Basic 1.0 Standard Edition

  • kbnosurvey kbarchive KB76666