Results 1 to 18 of 18

Thread: Changing Buttons on Mouseover

  1. #1

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214

    Changing Buttons on Mouseover

    Hi All,

    I am trying to figure out how to change the look of buttons when a mouse pointer hovers over them.

    I have managed to change the button on the mousemove event but then how do I change it back when the mouse leaves the buttons area?

    Thanks,
    Gavin

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    You have to change it back in the Form's mousemove event.

  3. #3

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    ....

    Thanks.
    Easy enough.

  4. #4

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    Ooops

    This doesn't work if you have 2 buttons close to each other, because if you move the mouse quick enough you can highlight both the buttons at the same time.

    Is there not a better way to do this?

    And no I'm not moving the mouse extremely quickly just to see if I can break it either ...

    Cheers,
    Gavin

  5. #5
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Thanks.
    Easy enough.
    Yep, although you will get problems if the button is too close to the edge of the form. If the user moves the mouse quicky off your form, the forms mousemove won't fire.

  6. #6

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    Originally posted by chrisjk
    Yep, although you will get problems if the button is too close to the edge of the form. If the user moves the mouse quicky off your form, the forms mousemove won't fire.
    We seem to have posted at the same time with the same problem.

    Is there another way around this?

  7. #7
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Originally posted by Gavin_Mannion
    Ooops

    This doesn't work if you have 2 buttons close to each other, because if you move the mouse quick enough you can highlight both the buttons at the same time.

    Is there not a better way to do this?

    And no I'm not moving the mouse extremely quickly just to see if I can break it either ...

    Cheers,
    Gavin
    You can do something about it, but it requires the use of API...this example posts a message when the mouse moves outside the form. You should be able to modify it to fire when the mouse moves outside the button
    VB Code:
    1. 'In a module
    2. Public Const TME_CANCEL = &H80000000
    3. Public Const TME_HOVER = &H1&
    4. Public Const TME_LEAVE = &H2&
    5. Public Const TME_NONCLIENT = &H10&
    6. Public Const TME_QUERY = &H40000000
    7. Public Const WM_MOUSELEAVE = &H2A3&
    8. Public Type TRACKMOUSEEVENTTYPE
    9.     cbSize As Long
    10.     dwFlags As Long
    11.     hwndTrack As Long
    12.     dwHoverTime As Long
    13. End Type
    14.  
    15. Public Declare Function TrackMouseEvent Lib "user32" (lpEventTrack As TRACKMOUSEEVENTTYPE) As Long
    16. Public Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
    17. 'Subclassing is explained in our subclassing tutorial at [url]http://www.allapi.net/[/url]
    18. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    19. Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    20. Public Const GWL_WNDPROC = (-4)
    21. Public PrevProc As Long
    22.  
    23. Public Sub HookForm(F As Form)
    24.     PrevProc = SetWindowLong(F.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    25. End Sub
    26. Public Sub UnHookForm(F As Form)
    27.     SetWindowLong F.hWnd, GWL_WNDPROC, PrevProc
    28. End Sub
    29. Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    30.     If uMsg = WM_MOUSELEAVE Then
    31.         'if we receive a WM_MOUSELEAVE message, show it
    32.         Form1.Print "The mouse left the form!"
    33.     End If
    34.     WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
    35. End Function
    VB Code:
    1. 'In a form (Form1)
    2. Private Sub Form_Click()
    3.     Dim ET As TRACKMOUSEEVENTTYPE
    4.     'initialize structure
    5.     ET.cbSize = Len(ET)
    6.     ET.hwndTrack = Me.hWnd
    7.     ET.dwFlags = TME_LEAVE
    8.     'start the tracking
    9.     TrackMouseEvent ET
    10.     'show a message to the user
    11.     Me.Print "Move the mouse cursor outside the form" + vbCrLf + "to generate a WM_MOUSELEAVE event"
    12. End Sub
    13. Private Sub Form_Load()
    14.     'KPD-Team 2001
    15.     'URL: [email][email protected][/email]
    16.     'E-Mail: [email][email protected][/email]
    17.     'show a warning message
    18.     MsgBox "WARNING: This sample uses subclassing." + vbCrLf + "To end this program, always use the X button of the form." + vbCrLf + "Do not use VB's Stop button and do not use the 'End' keyword in your VB code." + vbCrLf + vbCrLf + "For more information about subclassing, check out" + vbCrLf + "our subclassing tutorial at [url]http://www.allapi.net/[/url]", vbExclamation
    19.     'set the graphics mode to persistent
    20.     Me.AutoRedraw = True
    21.     Me.Print "Click the form to begin"
    22.     'start subclassing this form
    23.     HookForm Me
    24. End Sub
    25. Private Sub Form_Unload(Cancel As Integer)
    26.     'stop subclassing this form
    27.     UnHookForm Me
    28. End Sub

  8. #8

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    HAH..

    There is not a chance I am going into all that muck at 16:32 my time.

    I have only started VB last week, wow that looks confusing.

    I will give it a shot in the morning.

    Thanks,
    Gavin

    PS: It's so much easier to do that in ASP.
    A:Hover ... lovely

  9. #9
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Okay, I modified it for you

    Into a module
    VB Code:
    1. Public Const TME_CANCEL = &H80000000
    2. Public Const TME_HOVER = &H1&
    3. Public Const TME_LEAVE = &H2&
    4. Public Const TME_NONCLIENT = &H10&
    5. Public Const TME_QUERY = &H40000000
    6. Public Const WM_MOUSELEAVE = &H2A3&
    7.  
    8. Public Type TRACKMOUSEEVENTTYPE
    9.     cbSize As Long
    10.     dwFlags As Long
    11.     hwndTrack As Long
    12.     dwHoverTime As Long
    13. End Type
    14.  
    15. Public Declare Function TrackMouseEvent Lib "user32" (lpEventTrack As TRACKMOUSEEVENTTYPE) As Long
    16. Public Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
    17.  
    18. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    19. Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    20.  
    21. Public Const GWL_WNDPROC = (-4)
    22. Public PrevProc As Long
    23.  
    24. Public Sub HookForm(F As Control)
    25.  
    26. PrevProc = SetWindowLong(F.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    27.  
    28. End Sub
    29. Public Sub UnHookForm(F As Control)
    30.  
    31. SetWindowLong F.hWnd, GWL_WNDPROC, PrevProc
    32.  
    33. End Sub
    34. Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    35.  
    36. If uMsg = WM_MOUSELEAVE Then
    37.     'if we receive a WM_MOUSELEAVE message, show it
    38.     Form1.Print "The mouse left the button!"
    39. End If
    40.  
    41. WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
    42.  
    43. End Function
    into a form
    VB Code:
    1. Private Sub command1_Click()
    2.  
    3. Dim ET As TRACKMOUSEEVENTTYPE
    4. 'initialize structure
    5. ET.cbSize = Len(ET)
    6. ET.hwndTrack = Command1.hWnd
    7. ET.dwFlags = TME_LEAVE
    8. 'start the tracking
    9. TrackMouseEvent ET
    10. 'show a message to the user
    11. Me.Print "Move the mouse cursor outside the button" + vbCrLf + "to generate a WM_MOUSELEAVE event"
    12.  
    13. End Sub
    14. Private Sub Form_Load()
    15.  
    16. MsgBox "WARNING: This sample uses subclassing." + vbCrLf + "To end this program, always use the X button of the form." + vbCrLf + "Do not use VB's Stop button and do not use the 'End' keyword in your VB code." + vbCrLf + vbCrLf + "For more information about subclassing, check out" + vbCrLf + "our subclassing tutorial at [url]http://www.allapi.net/[/url]", vbExclamation
    17. 'set the graphics mode to persistent
    18. Me.AutoRedraw = True
    19. Me.Print "Click the button to begin"
    20. 'start subclassing this form
    21. HookForm Command1
    22.    
    23. End Sub
    24. Private Sub Form_Unload(Cancel As Integer)
    25.  
    26. 'stop subclassing this control
    27. UnHookForm Command1
    28.  
    29. End Sub

  10. #10
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    This example uses 4 command buttons with there style property set to graphical
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    4.   ChangeColor Command1
    5. End Sub
    6.  
    7. Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    8.   ChangeColor Command2
    9. End Sub
    10.  
    11. Private Sub Command3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    12.   ChangeColor Command3
    13. End Sub
    14.  
    15. Private Sub Command4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    16.    ChangeColor Command4
    17. End Sub
    18.  
    19. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    20.    DefaultColor
    21. End Sub
    22.  
    23. Private Sub ChangeColor(cmdButton As CommandButton)
    24.    DefaultColor
    25.    cmdButton.BackColor = vbRed
    26. End Sub
    27.  
    28. Private Sub DefaultColor()
    29.    Dim ctl As Control
    30.    For Each ctl In Controls
    31.       If TypeOf ctl Is CommandButton Then
    32.          ctl.BackColor = Me.BackColor
    33.       End If
    34.    Next ctl
    35. End Sub

  11. #11
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Mark, no offense by maybe you should read the whole thread

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    This seemed to be the question
    Hi All,

    I am trying to figure out how to change the look of buttons when a mouse pointer hovers over them.

    I have managed to change the button on the mousemove event but then how do I change it back when the mouse leaves the buttons area?

    Thanks,
    Gavin
    What did I miss?

  13. #13
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Originally posted by MarkT
    What did I miss?
    You missed this...
    Ooops

    This doesn't work if you have 2 buttons close to each other, because if you move the mouse quick enough you can highlight both the buttons at the same time.

    Is there not a better way to do this?

    And no I'm not moving the mouse extremely quickly just to see if I can break it either

  14. #14
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Try my code and I bet you can never highlight 2 buttons at once. You could even have them overlapping and only the one the the pointer is on will be highlighted.
    You may be able to keep one highlighted if you move off the form real quickly or the button is against the edge of the form, but as soon as you return to the form it will change back.

  15. #15

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    Thanks for all the help so far and I nearly have this working now.

    Just one small problem.

    How do I pass the command button to a module..

    By that I mean I have a public function in a module called ChangeButton()

    What I would like to be able to do is from the form call the function as in something like ChangeButton(form1.command1) and the module to recognise the correct button to change.

    I have tried a few different things but nothing seems to be working.

    Thanks for the help,
    Gavin

  16. #16
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Try in your module
    VB Code:
    1. Public Sub ChangeButton(frm as Form)
    2.    Debug.Print frm.ActiveControl.Name
    3. End sub
    and in your form
    VB Code:
    1. Private Sub Command1_Click()
    2.    ChangeButton Me
    3. End Sub

  17. #17

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    Cool ..

    Thanks,
    Gavin

  18. #18
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The following code will change the backcolor of a command button when the mouse is hovering the button.
    (The command button style property must be set to graphical)
    VB Code:
    1. Private Declare Function SetCapture _
    2.  Lib "user32" ( _
    3.  ByVal hwnd As Long) As Long
    4.  
    5. Private Declare Function ReleaseCapture _
    6.  Lib "user32" () As Long
    7.  
    8. Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    9.     Static blnHasCapture As Boolean
    10.    
    11.     If blnHasCapture = False Then
    12.         SetCapture Command1.hwnd
    13.         blnHasCapture = True
    14.     End If
    15.     If (X < 0 Or X > Command1.Width) Or (Y < 0 Or Y > Command1.Height) Then
    16.         Command1.BackColor = vbButtonFace
    17.         ReleaseCapture
    18.         blnHasCapture = False
    19.     Else
    20.         Command1.BackColor = vbRed
    21.     End If
    22. End Sub
    23.  
    24. Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    25.     SetCapture Command1.hwnd
    26. End Sub
    Best regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width