Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCapture Lib "user32" () As Long
'From VB2TheMax.Com
'MouseEnter And MouseExit Code
'NOTE: To use this code to change the background color of a command
'button, the command button style MUST be set to graphical
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (X < 0) Or (Y < 0) Or (X > Command1.Width) Or (Y > Command1.Height) Then ' the MOUSELEAVE pseudo-event
ReleaseCapture ' in this example revert the caption to normal
Command1.Font.Bold = False
ElseIf GetCapture() <> Command1.hwnd Then ' the MOUSEENTER pseudo-event
SetCapture Command1.hwnd ' in this example, make the caption bold
Command1.Font.Bold = True
End If
End Sub
'You can apply this tip to all controls that have the hWnd property, such as PictureBox,
'ListBox, etc, for example to easily implement hot-tracking effects. Here is another example
'that selects the text of a textbox (Text1) when the cursor is over.
Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, _
Y As Single)
If (X Or Y) < 0 Or (X > Text1.Width) Or (Y > Text1.Height) Then
ReleaseCapture
Text1.SelLength = 0
ElseIf GetCapture() <> Text1.hWnd Then
SetCapture Text1.hWnd
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
Text1.SetFocus
End If
End Sub
'To change the backcolor of a command button
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (X < 0) Or (Y < 0) Or (X > cmdClose.Width) Or (Y > cmdClose.Height) Then ' the MOUSELEAVE pseudo-event
ReleaseCapture ' the mouse is no longer over the button, change backcolor to gray
cmdClose.BackColor = &HC0C0C0
ElseIf GetCapture() <> cmdClose.hwnd Then ' the MOUSEENTER pseudo-event
SetCapture cmdClose.hwnd ' the mouse is over the button, change backcolor to cyan
cmdClose.BackColor = vbCyan
End If
End Sub