The following should illustrate the point. You need two forms, the first should have 4 command buttons.
[tt]
Option Explicit
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Sub Command1_Click()
' Appears to work as expected, but in fact this is because
' when you move the mouse pointer out of the form it get's switched
' by whatever application the mouse is now over, rather than by VB
' As far as VB is concerned it never (apart from the actual point you
' make the Mousepointer call) gets the opportunity
' to switch pointer as mouse moves in and out of form as the
' tight loop stops any mouse messages getting to it
' You can see this effect when you move the cursor over the overlapped Form2.
' You'd expect the pointer to change back to the default, but it doesn't.
Dim StartTime As Date
ButtonToggle
StartTime = Now
Form1.MousePointer = vbHourglass
Do Until DateDiff("s", StartTime, Now) > 10
Loop
Form1.MousePointer = vbDefault
ButtonToggle
End Sub
Private Sub Command2_Click()
' Now we defer to the OS in the loop, so VB gets a chance to
' switch the pointer. It looks exactly the same as above, but in fact
' VB is doing the pointer switching. Moving the pointer over the overlapped
' Form2. Unlike the previous method above, this time the cursor DOES change
Dim StartTime As Date
ButtonToggle
StartTime = Now
Form1.MousePointer = vbHourglass
Do Until DateDiff("s", StartTime, Now) > 10
DoEvents
Loop
Form1.MousePointer = vbDefault
ButtonToggle
End Sub
Private Sub Command3_Click()
' The SetCursorPos effectively does what having a common dialog
' or a MsgBox might do - i.e to move the cursor OFF the form for
' which we are setting the pointer
' VB only tries to switch the pointers when it gets a message from the mouse
' saying 'Hello, I'm over the form', and we prevent that ever happening
' by having a tight loop
Dim StartTime As Date
ButtonToggle
StartTime = Now
SetCursorPos 0, 0
Form1.MousePointer = vbHourglass
Do Until DateDiff("s", StartTime, Now) > 10
Loop
Form1.MousePointer = vbDefault
ButtonToggle
End Sub
Private Sub Command4_Click()
' Same as above, only this time we defer to the OS during the loop,
' so VB gets the necessary mouse messages, and we get the behaviour we want
Dim StartTime As Date
ButtonToggle
StartTime = Now
SetCursorPos 0, 0
Form1.MousePointer = vbHourglass
Do Until DateDiff("s", StartTime, Now) > 10
DoEvents
Loop
Form1.MousePointer = vbDefault
ButtonToggle
End Sub
Private Sub Form_Load()
' Just make sure form isn't placed at 0,0
Form1.Move 100, 100
' Offset Form2
Form2.Move Form1.Left + 300, Form1.Top + 300
Form2.Show
End Sub
Private Sub ButtonToggle()
Dim tmpButton As Control
For Each tmpButton In Form1.Controls
tmpButton.Enabled = Not tmpButton.Enabled
Next
End Sub