Using a graphics editor, the Microsoft Windows Software Development
Kit (SDK), and the Microsoft C compiler, you can create a dynamic-link
library (DLL) containing mouse cursors that can be used in a Microsoft
Visual Basic application. By making calls to the Windows API functions
LoadLibrary, LoadCursor, SetClassWord, and GetFocus, you can display a
custom cursor from within a Visual Basic application. Below are the steps
necessary to a create a custom cursor and a Visual Basic application to use
this custom cursor.
Setting a custom cursor in a Visual Basic application requires a call to
the Windows API function LoadLibrary to load the custom DLL containing the
cursor resource(s). A call to LoadCursor is then required to load a single
cursor contained in the DLL. The return value of the LoadCursor function is
a handle to the custom cursor. This handle can be passed as an argument to
the API function SetClassWord with the constant GCW_HCURSOR. SetClassWord
also requires a window handle (hWnd) to the object (form or control) for
which the cursor is to be set. The hWnd of a form is available via the hWnd
run-time method. For example, the statement FWnd = Form1.hWnd will return
the hWnd of Form1 to the variable FWnd. The hWnd of a control can be
obtained by first using the SetFocus method on the control to give it
the input focus and then calling the API function GetFocus. GetFocus
returns the hWnd of the object with the current input focus.
A custom cursor always takes the place of the system cursor. The
MousePointer property of a form or control to receive the custom
cursor must be set to zero (system). Any other value for this property
will result in the selected cursor being displayed, not the custom
Because the cursor is defined as part of a window class, any change to
the window class will be reflected across any control or form that
uses that class. For example, if the MousePointer property for two
command buttons is zero (system) and a custom cursor is set for one of
the command buttons, both of the command buttons will receive the
custom cursor. To guarantee a custom cursor for each control requires
that the cursor be set by calling SetClassWord in the MouseMove event
procedure of the control.
Some controls, such as command buttons, do not contain a MouseMove
event procedure. A custom mouse pointer for these types of controls
can be set by initiating a timer event. Within the timer event, calls
to the API functions GetCursorPos and WindowFromPoint can be made to
determine if the mouse is over the control or not. If the
WindowFromPoint API call returns the hWnd of the control, then the
mouse is over the control. A call to SetClassWord can then be made to
set the custom cursor for the control.
Below is an example of using the Windows SDK and C Compiler to create
a DLL containing cursor resources. Further below are the steps
necessary to create a Visual Basic for Windows program to use the cursor
If you do not have the Windows SDK but have a pre-compiled DLL
containing cursor resources, skip to the steps below outlining how to
create a Visual Basic application to use the custom cursor resources.
- 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.
- 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:
int _far _pascal LibMain (HANDLE hInstance,
int _far _pascal WEP (int nParameter)
- Save the file created in step 2 above as CURSORS.C.
- Using any text editor, create a definition file (.DEF) for the
DLL. Enter the following as the body of the .DEF file:
DESCRIPTION 'DLL containing cursor resources'
CODE MOVEABLE DISCARDABLE
DATA MOVEABLE SINGLE
WEP @1 RESIDENTNAME
- Save the file created in step 4 above as CURSORS.DEF.
- 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
- Save the file created in step 6 above as CURSORS.RC.
- Compile CURSORS.C from the MS-DOS command line as follows:
CL /W2 /ALw /c /Od /GD /W2 CURSORS.C
- 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.
LINK /NOE /NOD cursors.obj +
- 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:
- Rename CURSORS.EXE to CURSORS.DLL from the MS-DOS command line as
REN CURSORS.EXE CURSORS.DLL
Below are the steps necessary to create a Visual Basic for Windows
application that uses the cursor resources created in the steps above.
When running the Visual Basic for Windows program created by following
the steps below, it is important to terminate the application from the
system menu, NOT the Run End option from the file menu. When Run End is
chosen from the file menu, the unload event procedure is not executed.
Therefore, the system cursor is not restored and the custom cursor
will remain present at design time. Using Visual Basic version 1.0 for
Windows, avoid terminating the program from the Program Manager
(PROGMAN.EXE) task list. The unload event procedure is also not called
when a program is terminated from the task list in Visual Basic
version 1.0 for Windows.
- 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.
- Place a picture control (Picture1), command button (Command1), and timer
control (Timer1) on Form1.
- Enter the following code in the Global Module:
x As Integer
y As Integer
- Enter the following code in the General Declaration section of Form1:
' 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 p As PointType
- Enter the following code in the Form_Load event procedure of Form1:
Sub Form_Load ()
DLLInstance = LoadLibrary("CURSORS.DLL")
Curs1Handle = LoadCursor(DLLInstance, "Cursor1")
Curs2Handle = LoadCursor(DLLInstance, "Cursor2")
' Get the current control with the input focus.
CurrHwnd = GetFocus()
' Get the Window handle of Picture1.
Pic1hWnd = GetFocus()
' Get the Window handle of Command1.
Command1hWnd = GetFocus()
' Restore the focus to the control with the input focus.
r = PutFocus(CurrHwnd)
timer1.interval = 1 ' One millisecond.
timer1.enabled = -1
- 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)
- Enter the following code in the Timer1_Timer event procedure of Timer1:
Sub Timer1_Timer ()
' Get the current (absolute) cursor position.
' 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)
LastCursor=SetClassWord(Form1.hWnd, GCW_HCURSOR, Curs2Handle)
LastCursor=SetClassWord(Command1hWnd, GCW_HCURSOR, Curs2Handle)
LastCursor=SetClassWord(Pic1hWnd, GCW_HCURSOR, Curs2Handle)
LastCursor=SetClassWord(Form1.hWnd, GCW_HCURSOR, Curs1Handle)
LastCursor=SetClassWord(Command1hWnd, GCW_HCURSOR, Curs1Handle)
LastCursor = SetClassWord(Form1.hWnd, GCW_HCURSOR, Curs1Handle)
LastCursor = SetClassWord(Pic1hWnd%, GCW_HCURSOR, Curs1Handle)
- 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.
Article ID: 76666 - Last Review: October 26, 2013 - 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|