Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mouse hover over a label

Status
Not open for further replies.

SurvivorTiger

Programmer
Jul 9, 2002
265
0
0
US
Hi everyone,

I want the ForeColor property of my label to change as you hover the mouse over it. But I also want the label to go back to its normal color if the mouse is not over the label anymore. (Kind of like what they do in most websites, where the color of hyperlinks change when u hover over them)

Thanks in advance!



AIM: survivertiger
 
Normally I do this I don't know if this is the properway to do
Code:
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Label1
.FontBold = True
.ForeColor = vbBlue
.BackColor = vbWhite
End With
End Sub

Code:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Label1
.FontBold = False
.ForeColor = vbWhite
.BackColor = vbBlue
End With
End Sub
regards

Zameer Abdulla
Visit Me
Where to pay your [purple]Tsunami[/purple] relief donations?
 
this may be of interest: thread222-416091

see strongm's post (the one with 3 stars ;-))


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Strongm's method would be perfect if i wasn't using labels. But unfortunately, his method doesn't work with labels. ZmrAbdulla's method works, but i don't really like how it works. I don't think it's a very efficient method since instructions are being executed every milisecond as the mouse is moving on the form. So, is there anyone who could give me some alternate solutions?

Thanks

AIM: survivertiger
 
How about setting a timer to reset the font/color?

Private sub Timer1()
label1.fontbold = false
timer1.enabled = false

End Sub

Private sub label1_mousemove()
label1.fontbold = true
timer1.enabled = true
end sub

Play with the timer time until you fine the desired delay. It will not keep the changes long but at least the program will only exacute commands when the mouse moves over the label.

Just an idea.....
 
Oh... i see... the Label has no hWnd...

Ok... using:
a Timer with interval set to 10
a Form with scalemode set to 3 - pixels
and a few labels...

I came up with this work-around...
Code:
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 Sub Timer1_Timer()
  Dim myPoint As POINTAPI
  Dim OverWindow As Boolean
  Dim x As Long, x1 As Long, x2 As Long
  Dim y As Long, y1 As Long, y2 As Long
  Dim wxm As Long, wl As Long, ww As Long
  Dim wym As Long, wt As Long, wh As Long
  Dim lbl As Control
  For Each lbl In Me.Controls
    If TypeOf lbl Is Label Then
    wl = ScaleX(Me.Left, vbTwips, vbPixels)
    wt = ScaleY(Me.Top, vbTwips, vbPixels)
    ww = ScaleX(Me.Width, vbTwips, vbPixels)
    wh = ScaleY(Me.Height, vbTwips, vbPixels)
    wxm = (ww - ScaleWidth) \ 2
    wym = (wh - ScaleHeight) - wxm
    
    GetCursorPos myPoint
    x = myPoint.x - wl - wxm
    y = myPoint.y - wt - wym
    
    With lbl
      x1 = .Left
      x2 = .Left + .Width
      y1 = .Top
      y2 = .Top + .Height
      If (x > x1 And x < x2 And y > y1 And y < y2) Then
          lbl.ForeColor = &HFF
      Else
          lbl.ForeColor = &H0
      End If
    End With
    End If
  Next
End Sub

You can get creative if you waant, and use the label's Tag property to set the color, set it as a link, check it, etc...

Such as, if you were to place the color in tag property, if the label was not a link, the tag would be blank, and the color would remain black...

here is the modified code for that:
Code:
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 Sub Timer1_Timer()
  Dim myPoint As POINTAPI
  Dim OverWindow As Boolean
  Dim x As Long, x1 As Long, x2 As Long
  Dim y As Long, y1 As Long, y2 As Long
  Dim wxm As Long, wl As Long, ww As Long
  Dim wym As Long, wt As Long, wh As Long
  Dim lbl As Control
  For Each lbl In Me.Controls
    If TypeOf lbl Is Label Then
    wl = ScaleX(Me.Left, vbTwips, vbPixels)
    wt = ScaleY(Me.Top, vbTwips, vbPixels)
    ww = ScaleX(Me.Width, vbTwips, vbPixels)
    wh = ScaleY(Me.Height, vbTwips, vbPixels)
    wxm = (ww - ScaleWidth) \ 2
    wym = (wh - ScaleHeight) - wxm
    
    GetCursorPos myPoint
    x = myPoint.x - wl - wxm
    y = myPoint.y - wt - wym
    
    With lbl
      x1 = .Left
      x2 = .Left + .Width
      y1 = .Top
      y2 = .Top + .Height
      If (x > x1 And x < x2 And y > y1 And y < y2) Then
          lbl.ForeColor = [b]Val(lbl.Tag)[/b]
      Else
          lbl.ForeColor = &H0
      End If
    End With
    End If
  Next
End Sub

Just set the Tag Property like...

Label1.Tag = "&hFF0000" 'Blue
Label2.Tag = "&h00FF00" 'Green
Label3.Tag = "&h0000FF" 'Red
Label4.Tag = "&hFFFFFF" 'White
etc..

Hope this helps...
Good Luck!
-Josh


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Just looked at the link that ZmrAbdulla provided for planet source code...

They use the same basic functionality as the code i posted above...

I tried to do basically the same thing at first, but since you can't get the hwnd, you can't use the SetCapture or ReleaseCapture functions, which are the key to using the function with MouseMove... so, note how they are using a timer too...

What are you planning to use this for...?

You might just want to throw a WebBrowser Object on your form and use it instead ;-)


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Also... If you want the label's on and off colors to be controled through the tag property, you can set it up like this:
Code:
      If (x > x1 And x < x2 And y > y1 And y < y2) Then
          lbl.ForeColor = Val(split(lbl.Tag & "|", "|")(0))
      Else
          lbl.ForeColor = Val(split(lbl.Tag & "|", "|")(1))
      End If

where you would set the labels up like:
Label1.Tag = "&h0000FF|&hFF0000" 'Red / Blue
Label2.Tag = "&h00FF00|&hFFFFFF" 'Green / White
Label3.Tag = "&h0000FF" ' Red / (default to black)


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
My suggestion would be to place another control that DOES have the options you want (picturebox, maybe?) on top of the label and set all it's attributes so that it is completely transparent with no borders, etc. Then that control can handle the events you need and set the underlying label's attributes accordingly.


Meddle not in the affairs of dragons,
for you are crunchy, and good with mustard.
 
>>transparent with no borders

What do you mean transparent with no borders???

How do you make a picture box transparent with no borders, and still be able to raise events???

If you set the Visibility to False, it can not be clicked, or accessed in anyway, by the mouse...

If you find a way to set the Alpha of the PictureBox to 100% transparent, it has the same effect as Visibility = False...

Have you tried this method?


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Actually, I hadn't tried it, which is why I said "maybe?". I was simply suggesting a possible avenue of investigation (sometimes you learn more by checking things out for yourself). I was assuming that you could set the background transparent, just like you can do with labels. Unfortunately, that seems not to be the case.

What I was basing my suggestion on is a similar situation where I wanted to be able to double-click on a label to turn on or off a special feature. Unfortunately when the label wasn't displayed, you couldn't double-click on it. I solved the problem by putting another label with no text and a transparent background on top of it, and using that label to catch the double-click. I was hoping the same type of technique would apply.

After spending some time in the MSDN docs it appears as if the suggestions above using onMouseMove are the only viable alternatives. As far as I can tell there is no such thing as onMouseOver/onMouseOut in VB, just onMouseMove. However according to MSDN the label should respond to the onMouseMove event, so that should solve the problem.


Meddle not in the affairs of dragons,
for you are crunchy, and good with mustard.
 
Yeah... that's one thing I hate about the VB controls...

Labels don't have an hwnd...
and
You can't set the Background of a Picture box to transparent...

-----

The problem is not with the MouseMove...

It is not being able to know when the mouse is off of the button...

You can tell with a picture box by using: SetCapture or ReleaseCapture

But... that requires an HWND... (which a picture box has, and a label does not)

The only solution I see is to use the Timer...


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top