|
|
|
Visual Basic raises the MouseMove event when the mouse cursor is within a control, so it's easy to know when the cursor "enters" a control. However, determining when the mouse leaves the control is more difficult.
You can solve this problem by monitoring the first time the control raises the MouseMove event, which corresponds to the MouseEnter event; in this occasion you capture the mouse input with SetCapture API function, so that your control will receive MouseMove messages even if the cursor is outside the control's window.
Once you have captured the mouse input you can check the coordinates of the cursor and verify if it's still in the control area. When mouse leaves the control's window, it's time for the MouseExit event, and you must release the mouse capture with ReleaseCapture(). Here's the code that does the trick for the Command1 control, but that can be easily modified for any control: |
Click here to copy the following block | 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
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 ReleaseCapture Command1.Font.Bold = False ElseIf GetCapture() <> Command1.hwnd Then SetCapture Command1.hwnd 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. |
UPDATE: The original example for the Command1 button didn't work well, and the mouseover functionality was disabled after you click on the button. The new version shown above uses the GetCapture API to solve this problem, and gets rid of the bCaptured Static variable, so it's also simpler than the original code. Big thanks to Sergio Perciballi from UK for letting us know about the mistake.
|
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|