I found out creating a mouseover in VB6 is not as simple as VBnet. If anyone can help me with me out with how to do it in VB6 I would be oh so greatful.
Mousemove has the draw back of not responding when the cursor has left the event area. There are a few different approachs in dealing with this, most using the api. It really depends on what you are working on. One approach is to monitor the mouse position on the screen, which means that you need to know where your event area is in relasionship to the screen. The best way is in the first move event put the x/y event area and the x/y screen area in a variable then compute a new x1/y1 x2/y2 area relative to the screen, in this way you know exactly when your mouse leaves the area. Also you need a timer for this.
The following code will retrieve the screen position of X/Y, The functions also have an optional input value which you do not need to use.
Private Declare Function GetCursorPos& Lib "user32" (lpPoint As POINTAPI)
Private Type POINTAPI
X As Long
Y As Long
End Type
Function PosX(Optional ByVal hwnd As Long) As Long
Dim lpPoint As POINTAPI
GetCursorPos lpPoint
PosX = lpPoint.X
End Function
Function PosY(Optional ByVal hwnd As Long) As Long
Dim lpPoint As POINTAPI
GetCursorPos lpPoint
PosY = lpPoint.Y
End Function
The next step is to build a mouse_over function, it is important that you had a boolean to block repeated calls during the same move event.
On your form ;
Dim MyMoveIsActive As Boolean
Dim RealX, RealY, vX1, vX2, vY1, vY2 As Long
Function MyMouseOver(ByVal X1 As Long, X2 As Long, Y1 As Long, Y2 As Long)As Boolean
Dim qX, qY As Boolean
If(MyMoveIsActive = True)Then
GoTo QuestionMouse
End If
In the previous mentioned there are two places where Do Whatever is written, In the mousemove turn your image on and in the timer turn it off. The if statement in mousemove will only be true on entering the event object, the timer event will remain true until the mouse exits the area and so in the else statment mouseover will be turned off.
And if you are willing to consider an API solution, here's a variant. Note that the WindowFromPoint call basically does all the work that merlinx's mymouseover function provides: [tt]
Option Explicit
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Function RollOver(ctlControl As Control) As RollType
Dim myPoint As POINTAPI
Dim OverWindow As Boolean
GetCursorPos myPoint
On Error Resume Next ' Bit of a cheat to handle case where control does not have an hWnd
OverWindow = (ctlControl.hWnd = WindowFromPoint(myPoint.X, myPoint.Y))
On Error GoTo 0
If OverWindow Then
SetCapture ctlControl.hWnd
RollOver = RollIn
Else
ReleaseCapture
RollOver = RollOut
End If
End Function [/tt]
And here's an example of how it might be used. In this case I just change the Backcolor of the button, which requires the button's style to be set to Graphical. [tt]
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If RollOver(Command1) = RollIn Then
'Do rollin event, eg:
Command1.BackColor = &H80000010 ' Assumes button style is Graphical
Else ' Ok, we've got a rollout
' Do rollout event, eg:
Command1.BackColor = &H8000000F ' Assumes button style is Graphical
End If
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If RollOver(Command1) = RollIn Then
'Do rollin event, eg:
Command1.Picture=LoadPicture("image1.jpg" ' Assumes button style is Graphical
Else ' Ok, we've got a rollout
' Do rollout event, eg:
Command1.Picture=LoadPicture("image2.jpg" ' Assumes button style is Graphical
End If
End Sub
strongm, I was trying to use your code, but I didn't know how to seperate it(just a begginer), meaning, where to put the:
Option Explicit
Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
and the :
Private Function RollOver(ctlControl As Control) As RollType
Dim myPoint As POINTAPI
Dim OverWindow As Boolean
GetCursorPos myPoint
On Error Resume Next ' Bit of a cheat to handle case where control does not have an hWnd
OverWindow = (ctlControl.hWnd = WindowFromPoint(myPoint.X, myPoint.Y))
On Error GoTo 0
If OverWindow Then
SetCapture ctlControl.hWnd
RollOver = RollIn
Else
ReleaseCapture
RollOver = RollOut
End If
End Function
as for the sub, I know where to use it.
Do I have to put them in different places, because it didn't work when I just paste it and do the changes I need..
strongm, as I mentioned before, the code u wrote work fine, except for one thing that I need to ask about, its not only with your code but with most mousemove functions that I tired... The problem is that the buttons usually don't run from the first click, what happens is that I see the focus on the button after the first click, and then the second click make it run (sometimes even the third, and sometimes the first)... Is there any reason for that ? And do u have the same problem ?
Strongm, if I use one command box and one image box, it works. However when I try to use one image box and one label it does not work properly, when I move the mouse into the image, the label's caption changes(what I want) but when I move the mouse out the image, nothing happens(label's caption should be change to another text), Please help me. Thanks.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.