Click to See Complete Forum and Search --> : MouseMove ColorChange
Steve Scott
October 21st, 2001, 06:42 PM
I have the mousemove event of my control set to change the color of a button residing inside it, so I know when the mouse is over it(the button is resized to fit the control in the Resize Event btw).
But im not sure howto have the color of the button change back when the mouse isnt over it. As I understand it when the mouse leaves the control the form starts using it's mousemove event (not the control). And I want it to be independant.
Please Help,
Steve
tdeltax
October 22nd, 2001, 04:24 AM
i guess u cannot use any event but u can try and fool the user using the following code
i hope this suffices ur need
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.Caption = "Xpos = " & X & " Ypos = " & Y
Command1.BackColor = vbGreen
If X > Command1.Width - 50 Or Y > Command1.Width - 50 Or X < 50 Or Y < 50 Then
Command1.BackColor = vbBlue
End If
End Sub
tdeltax
October 22nd, 2001, 04:24 AM
i guess u cannot use any event but u can try and fool the user using the following code
i hope this suffices ur need
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.Caption = "Xpos = " & X & " Ypos = " & Y
Command1.BackColor = vbGreen
If X > Command1.Width - 50 Or Y > Command1.Width - 50 Or X < 50 Or Y < 50 Then
Command1.BackColor = vbBlue
End If
End Sub
Iouri
October 22nd, 2001, 08:24 AM
When the mouse moves over the button, use the GetCapture function to see
what window has the mouse captured. If it is not the button, use
SetCapture to capture mouse events.
When the MouseMove event occurs and the cursor has left the button's area,
use ReleaseCapture to release the capture.
'on the form array of buttons and array of Chekboxes
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 Check1_Click(Index As Integer)
'stop check control being indented
Check1(Index).Value = 0
End Sub
Private Sub Check1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If (X < 0) Or (Y < 0) Or (X > Check1(Index).Width) Or (Y > Check1(Index).Height) Then
' ' the MOUSELEAVE pseudo-event
ReleaseCapture
' ' in this example revert the caption to normal
Check1(Index).Font.Bold = False
Check1(Index).ForeColor = vbBlack
'
ElseIf GetCapture() <> Check1(Index).hwnd Then
' ' the MOUSEENTER pseudo-event
SetCapture Check1(Index).hwnd
Check1(Index).Font.Bold = True
Check1(Index).ForeColor = vbRed
End If
End Sub
Private Sub Command1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If (X < 0) Or (Y < 0) Or (X > Command1(Index).Width) Or (Y > Command1(Index).Height) Then
' ' the MOUSELEAVE pseudo-event
ReleaseCapture
' ' in this example revert the caption to normal
Command1(Index).Font.Bold = False
'
ElseIf GetCapture() <> Command1(Index).hwnd Then
' ' the MOUSEENTER pseudo-event
SetCapture Command1(Index).hwnd
Command1(Index).Font.Bold = True
End If
End Sub
Iouri Boutchkine
iouri@hotsheet.com
Iouri
October 22nd, 2001, 08:27 AM
Another way of doing it to check if mouse over the control or over the form
Control array of 3 textboxes 0-3
When mouse is over Text1(index) - the text is red
When mouse leaves text box the color is black
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim i As Integer
For i = 0 To 2
Text1(i).ForeColor = vbBlack
Next i
End Sub
Private Sub Text1_Click(Index As Integer)
MsgBox Text1(Index)
End Sub
Private Sub Text1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1(Index).ForeColor = vbRed
End Sub
Iouri Boutchkine
iouri@hotsheet.com
John G Duffy
October 22nd, 2001, 10:48 AM
Here is a simple example of MouseOver not Over
Start a new project. Add a command button to the form. Paste this code into the general declarations section of the form. Run it. Move the mouse over then not over the command button. This code can actually be used with any control that has a MouseMove event
option Explicit
private Declare Function SetCapture Lib "user32" (byval hwnd as Long) as Long
private Declare Function ReleaseCapture Lib "user32" () as Long
'Code:
'Put this code in MouseMove event. In this example, I put a
'CommandButton on a
'form with the name Command1
private Sub Command1_MouseMove(Button as Integer, Shift as Integer, X _
as Single, Y as Single)
static CtrMov as Boolean
static Counter as Long
Counter = Counter + 1
With Command1 'Change this 'Command1' to your control name
If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) then
ReleaseCapture
CtrMov = false
Command1.BackColor = &HFF&
Command1.Caption = " Not Over " & Counter
Command1.Refresh
'Put here your code to LostMouseFocus
'for example:
me.print "LostMouseFocus"
else
SetCapture .hwnd
If CtrMov = false then ' do this only once per "over"
CtrMov = true
Command1.BackColor = &HFFFFFF
Command1.Caption = " Over " & Counter
Command1.Refresh
'Put here your code to GetMouseFocus
'for example:
me.print "GetMouseFocus"
End If
End If
End With
End Sub
John G
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.