How to Create and Use a Custom Cursor in Visual Basic; Win SDKID: Q76666
|
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
cursor.
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
resources.
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.
#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);
}
LIBRARY CURSORS
DESCRIPTION 'DLL containing cursor resources'
EXETYPE WINDOWS
STUB 'WINSTUB.EXE'
CODE MOVEABLE DISCARDABLE
DATA MOVEABLE SINGLE
HEAPSIZE 0
EXPORTS
WEP @1 RESIDENTNAME
Cursor1 CURSOR CURS1.CUR
Cursor2 CURSOR CURS2.CUR
CL /W2 /ALw /c /Od /GD /W2 CURSORS.C
LINK /NOE /NOD cursors.obj +
LIBENTRY.OBJ,,,MDLLCEW.LIB+LIBW.LIB,CURSORS.DEF;
This will create the file CURSORS.EXE.
RC CURSORS.RC
REN CURSORS.EXE CURSORS.DLL
Type PointType
x As Integer
y As Integer
End Type
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
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
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
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
Additional query words: 2.00 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 4, 1999