patriciaxxx
Programmer
I have written the following cursor function, which works as commented.
I am only a vba beginner and did this by looking at other functions anfd tring to work out how to do it properly.
My question is can some one with experience who knows the correct way to write functions tell me if my code is how they would expect it to be, or post an example the way it should be written.
This will help me a great deal in learning the right way to code things.
Option Compare Database
Option Explicit
'Access allows us to display specific mouse pointers by controlling the MousePointer
'property of the Application object. However, it does not have the hand pointer (for
'example) pre-defined.
'Given a local ICO (icon) file, we can assign it to the Mouse pointer by using the
'LoadCursor and SetCursor API functions.
'Declare Windows API Constants for Windows System cursors.
Public Enum SystemCursorID
IDC_APPSTARTING = 32650& 'Standard arrow and small hourglass.
IDC_ARROW = 32512& 'Standard arrow.
IDC_CROSS = 32515 'Crosshair.
IDC_HAND = 32649 'Hand.
IDC_HELP = 32651 'Arrow and question mark.
IDC_IBEAM = 32513& 'Text I-beam.
IDC_ICON = 32641& 'Windows NT only: Empty icon.
IDC_NO = 32648& 'Slashed circle.
IDC_SIZE = 32640& 'Windows NT only: Four-pointed arrow.
IDC_SIZEALL = 32646& 'Four-pointed arrow pointing north, south, east, and west.
IDC_SIZENESW = 32643& 'Double-pointed arrow pointing northeast and southwest.
IDC_SIZENS = 32645& 'Double-pointed arrow pointing north and south.
IDC_SIZENWSE = 32642& 'Double-pointed arrow pointing northwest and southeast.
IDC_SIZEWE = 32644& 'Double-pointed arrow pointing west and east.
IDC_UPARROW = 32516& 'Vertical arrow.
IDC_WAIT = 32514& 'Hourglass.
End Enum
'Declarations for API Functions.
Private Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long
Private Declare Function LoadCursorFromFile Lib "user32" Alias "LoadCursorFromFileA" (ByVal lpFileName As String) As Long
Private Declare Function SetCursor Lib "user32" (ByVal hCursor As Long) As Long
Private Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'Declare handles for cursor.
Private Const GCL_HCURSOR = (-12)
Private hOldCursor As Long
Private hNewCursor As Long
'Change cursor to a hand.
'Browser default cursor.
Public Function UseHand()
modCursor.UseCursor IDC_HAND
End Function
'When you unload the cursor use hwnd
Public Function UnloadCursor(ByVal CurrentWindow As Long)
'Unload cursor.
hOldCursor = modCursor.SetClassLong(CurrentWindow, GCL_HCURSOR, hOldCursor)
End Function
'The UseCursor function will load and set a system cursor or a cursor from file to either a
'controls event property or to a forms event property.
'For controls set the argument Controls to True (default value).
'The argument CursorID is not optional.
'For system cursors select value from list.
'For cursors from file set the argument CursorPath to full path and enter 0 as the value for
'CursorID
'For forms set the argument Controls to False
'For correct use the forms Pop Up and Modal properties should be set to Yes
'Use's the hWnd property to determine the handle (a unique Long Integer value) assigned
'by Microsoft Windows to the current window.
'When you load the cursor use Me.hwnd
'When you unload the cursor use hwnd
Public Function UseCursor(CursorID As SystemCursorID, _
Optional CursorPath As String = "", _
Optional ByVal CurrentWindow As Long = 0, _
Optional Controls As Boolean = True)
'The arglist argument has too many values.
If CursorID > 0 And CursorPath <> "" Then
MsgBox "Can't assign both values." & vbCrLf & vbCrLf _
& "CursorID: " & CLng(CursorID) & vbCrLf _
& "CursorPath: " & CursorPath & vbCrLf, vbOKOnly, "UseCursor"
Exit Function
End If
'System cursor.
If Controls And CursorID > 0 Then
'Load new cursor and, if successful, set.
hNewCursor = LoadCursor(ByVal 0&, CLng(CursorID))
If (hNewCursor > 0) Then
hNewCursor = SetCursor(hNewCursor)
End If
End If
'Custom cursor from file.
'Custom animated cursor from file.
If Controls And CursorPath <> "" Then
'Load new cursor and, if successful, set.
hNewCursor = LoadCursorFromFile(CursorPath)
If (hNewCursor > 0) Then
hNewCursor = SetCursor(hNewCursor)
End If
End If
'System cursor.
If Controls = False And CursorID > 0 Then
'Load new cursor and, if successful, set.
hNewCursor = LoadCursor(ByVal 0&, CLng(CursorID))
If (hNewCursor > 0) Then
hOldCursor = SetClassLong(CurrentWindow, GCL_HCURSOR, hNewCursor)
End If
End If
'Custom cursor from file.
'Custom animated cursor from file.
If Controls = False And CursorPath <> "" Then
'Load new cursor and, if successful, set.
hNewCursor = LoadCursorFromFile(CursorPath)
If (hNewCursor > 0) Then
hOldCursor = SetClassLong(CurrentWindow, GCL_HCURSOR, hNewCursor)
End If
End If
End Function
I am only a vba beginner and did this by looking at other functions anfd tring to work out how to do it properly.
My question is can some one with experience who knows the correct way to write functions tell me if my code is how they would expect it to be, or post an example the way it should be written.
This will help me a great deal in learning the right way to code things.
Option Compare Database
Option Explicit
'Access allows us to display specific mouse pointers by controlling the MousePointer
'property of the Application object. However, it does not have the hand pointer (for
'example) pre-defined.
'Given a local ICO (icon) file, we can assign it to the Mouse pointer by using the
'LoadCursor and SetCursor API functions.
'Declare Windows API Constants for Windows System cursors.
Public Enum SystemCursorID
IDC_APPSTARTING = 32650& 'Standard arrow and small hourglass.
IDC_ARROW = 32512& 'Standard arrow.
IDC_CROSS = 32515 'Crosshair.
IDC_HAND = 32649 'Hand.
IDC_HELP = 32651 'Arrow and question mark.
IDC_IBEAM = 32513& 'Text I-beam.
IDC_ICON = 32641& 'Windows NT only: Empty icon.
IDC_NO = 32648& 'Slashed circle.
IDC_SIZE = 32640& 'Windows NT only: Four-pointed arrow.
IDC_SIZEALL = 32646& 'Four-pointed arrow pointing north, south, east, and west.
IDC_SIZENESW = 32643& 'Double-pointed arrow pointing northeast and southwest.
IDC_SIZENS = 32645& 'Double-pointed arrow pointing north and south.
IDC_SIZENWSE = 32642& 'Double-pointed arrow pointing northwest and southeast.
IDC_SIZEWE = 32644& 'Double-pointed arrow pointing west and east.
IDC_UPARROW = 32516& 'Vertical arrow.
IDC_WAIT = 32514& 'Hourglass.
End Enum
'Declarations for API Functions.
Private Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long
Private Declare Function LoadCursorFromFile Lib "user32" Alias "LoadCursorFromFileA" (ByVal lpFileName As String) As Long
Private Declare Function SetCursor Lib "user32" (ByVal hCursor As Long) As Long
Private Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'Declare handles for cursor.
Private Const GCL_HCURSOR = (-12)
Private hOldCursor As Long
Private hNewCursor As Long
'Change cursor to a hand.
'Browser default cursor.
Public Function UseHand()
modCursor.UseCursor IDC_HAND
End Function
'When you unload the cursor use hwnd
Public Function UnloadCursor(ByVal CurrentWindow As Long)
'Unload cursor.
hOldCursor = modCursor.SetClassLong(CurrentWindow, GCL_HCURSOR, hOldCursor)
End Function
'The UseCursor function will load and set a system cursor or a cursor from file to either a
'controls event property or to a forms event property.
'For controls set the argument Controls to True (default value).
'The argument CursorID is not optional.
'For system cursors select value from list.
'For cursors from file set the argument CursorPath to full path and enter 0 as the value for
'CursorID
'For forms set the argument Controls to False
'For correct use the forms Pop Up and Modal properties should be set to Yes
'Use's the hWnd property to determine the handle (a unique Long Integer value) assigned
'by Microsoft Windows to the current window.
'When you load the cursor use Me.hwnd
'When you unload the cursor use hwnd
Public Function UseCursor(CursorID As SystemCursorID, _
Optional CursorPath As String = "", _
Optional ByVal CurrentWindow As Long = 0, _
Optional Controls As Boolean = True)
'The arglist argument has too many values.
If CursorID > 0 And CursorPath <> "" Then
MsgBox "Can't assign both values." & vbCrLf & vbCrLf _
& "CursorID: " & CLng(CursorID) & vbCrLf _
& "CursorPath: " & CursorPath & vbCrLf, vbOKOnly, "UseCursor"
Exit Function
End If
'System cursor.
If Controls And CursorID > 0 Then
'Load new cursor and, if successful, set.
hNewCursor = LoadCursor(ByVal 0&, CLng(CursorID))
If (hNewCursor > 0) Then
hNewCursor = SetCursor(hNewCursor)
End If
End If
'Custom cursor from file.
'Custom animated cursor from file.
If Controls And CursorPath <> "" Then
'Load new cursor and, if successful, set.
hNewCursor = LoadCursorFromFile(CursorPath)
If (hNewCursor > 0) Then
hNewCursor = SetCursor(hNewCursor)
End If
End If
'System cursor.
If Controls = False And CursorID > 0 Then
'Load new cursor and, if successful, set.
hNewCursor = LoadCursor(ByVal 0&, CLng(CursorID))
If (hNewCursor > 0) Then
hOldCursor = SetClassLong(CurrentWindow, GCL_HCURSOR, hNewCursor)
End If
End If
'Custom cursor from file.
'Custom animated cursor from file.
If Controls = False And CursorPath <> "" Then
'Load new cursor and, if successful, set.
hNewCursor = LoadCursorFromFile(CursorPath)
If (hNewCursor > 0) Then
hOldCursor = SetClassLong(CurrentWindow, GCL_HCURSOR, hNewCursor)
End If
End If
End Function