Results 1 to 12 of 12

Thread: MouseOver event code

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Post

    I finally came up with a solution to a problem that someone posted, but I can't find them now. He wanted to have a control (an image, I think) change whenever the mouse was over it, then change back when the mouse left it. Here is code that creates a "MouseOver" event for any control on your form.

    Paste the following into a standard module:
    Code:
    Option Explicit
    
    Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    
    Private Function GetControlRect(ControlIn As Control, FormIn As Form) As RECT
        GetControlRect.Left = (ControlIn.Left + FormIn.Left + 50) / _
            Screen.TwipsPerPixelX
        GetControlRect.Right = (ControlIn.Left + FormIn.Left + _
            ControlIn.Width + 50) / Screen.TwipsPerPixelX
        GetControlRect.Top = (ControlIn.Top + FormIn.Top + 325) / _
            Screen.TwipsPerPixelY
        GetControlRect.Bottom = (ControlIn.Top + FormIn.Top + _
            325 + ControlIn.Height) / Screen.TwipsPerPixelY
    End Function
    
    Public Function MouseOver(ControlIn As Control, FormIn As Form) As Boolean
        Dim CursorPos As POINTAPI
        Dim ControlRect As RECT
    
        GetCursorPos CursorPos
        ControlRect = GetControlRect(ControlIn, FormIn)
        
        If CursorPos.X > ControlRect.Left And _
            CursorPos.X < ControlRect.Right And _
            CursorPos.Y > ControlRect.Top And _
            CursorPos.Y < ControlRect.Bottom Then
                MouseOver = True
        Else
            MouseOver = False
        End If
    End Function
    Usage (Probably best used in a timer):

    If MouseOver(ControlName, FormName) then
    'Highlight control here
    Else
    'Unhighlight control here
    End If

    Example:
    Place a timer, a picturebox, and two labels on a form (make sure the timer's Interval property is set to 10 or less for a quick response time). Then place the following code in the form's code window:
    Code:
    Option Explicit
    
    Private Sub Timer1_Timer()
        If MouseOver(Label1, Me) Then
            Label2 = "You're over the label"
        ElseIf MouseOver(Picture1, Me) Then
            Label2 = "You're over the picture"
        Else
            Label2 = "You aren't over any controls"
        End If
    End Sub
    Hope that helps!

    ~seaweed

    Edited by seaweed on 02-23-2000 at 04:56 PM

  2. #2
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by seaweed
    I finally came up with a solution to a problem that someone posted, but I can't find them now. He wanted to have a control (an image, I think) change whenever the mouse was over it, then change back when the mouse left it. Here is code that creates a "MouseOver" event for any control on your form.

    Paste the following into a standard module:
    Code:
    Option Explicit
    
    Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    
    Private Function GetControlRect(ControlIn As Control, FormIn As Form) As RECT
        GetControlRect.Left = (ControlIn.Left + FormIn.Left + 50) / _
            Screen.TwipsPerPixelX
        GetControlRect.Right = (ControlIn.Left + FormIn.Left + _
            ControlIn.Width + 50) / Screen.TwipsPerPixelX
        GetControlRect.Top = (ControlIn.Top + FormIn.Top + 325) / _
            Screen.TwipsPerPixelY
        GetControlRect.Bottom = (ControlIn.Top + FormIn.Top + _
            325 + ControlIn.Height) / Screen.TwipsPerPixelY
    End Function
    
    Public Function MouseOver(ControlIn As Control, FormIn As Form) As Boolean
        Dim CursorPos As POINTAPI
        Dim ControlRect As RECT
    
        GetCursorPos CursorPos
        ControlRect = GetControlRect(ControlIn, FormIn)
        
        If CursorPos.X > ControlRect.Left And _
            CursorPos.X < ControlRect.Right And _
            CursorPos.Y > ControlRect.Top And _
            CursorPos.Y < ControlRect.Bottom Then
                MouseOver = True
        Else
            MouseOver = False
        End If
    End Function
    Usage (Probably best used in a timer):

    If MouseOver(ControlName, FormName) then
    'Highlight control here
    Else
    'Unhighlight control here
    End If

    Example:
    Place a timer, a picturebox, and two labels on a form (make sure the timer's Interval property is set to 10 or less for a quick response time). Then place the following code in the form's code window:
    Code:
    Option Explicit
    
    Private Sub Timer1_Timer()
        If MouseOver(Label1, Me) Then
            Label2 = "You're over the label"
        ElseIf MouseOver(Picture1, Me) Then
            Label2 = "You're over the picture"
        Else
            Label2 = "You aren't over any controls"
        End If
    End Sub
    Hope that helps!

    ~seaweed

    Edited by seaweed on 02-23-2000 at 04:56 PM
    I tried to use your code for a lable control but all it did was always return a false return for the MouseOver function..

  3. #3
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    That's not the way to do it.

    http://forums.vb-world.net/showthrea...ght=setcapture

    This is the best way I know to create a "rollover image" effect. The "how" you load the new image is up to you, there are several different way to do it: Loadpicture, a picture array, an ImageList, whatever. But the essence of the effect is shown in this code:

    VB Code:
    1. Private Declare Function ReleaseCapture Lib "user32" () As Long
    2. Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
    3.  
    4. Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    5.     With Picture1
    6.         If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) Then
    7.             ReleaseCapture
    8.             .Picture = RollOver(0).Picture
    9.         Else
    10.             SetCapture .hwnd
    11.             .Picture = RollOver(1).Picture
    12.         End If
    13.     End With
    14.    
    15. End Sub


    The PictureBox RollOver is an array, but you can use whateve method you like to load one or other image.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  4. #4
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    thank you very much. Can u tell me how to change the mouse's icon when I do this effect?
    thanks

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    VB Code:
    1. Screen.MouseIcon = vbCustom
    2.     Screen.MousePointer = Pictture1.Picture
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    also try the WindowFromPointer API, it returns the handle of the control that the mouse pointer is over it. You can make a timer and check to see if the mouse is over the picturebox (if the function returns the same hWnd as the picturebox's). I'm not sure if Picture_mousemove works always

  7. #7
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    if I use this in my code
    Code:
    Label3.ForeColor = vbBlue
        Me.MouseIcon = LoadPicture("C:\Program Files\Microsoft Visual Studio\Common\Graphics\Cursors\H_POINT.CUR")
        Me.MousePointer = vbCustom
    when I distribute how will the APP know where to find the path to the icon or is there a better way?

  8. #8
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by jesus4u
    if I use this in my code
    Code:
    Label3.ForeColor = vbBlue
        Me.MouseIcon = LoadPicture("C:\Program Files\Microsoft Visual Studio\Common\Graphics\Cursors\H_POINT.CUR")
        Me.MousePointer = vbCustom
    when I distribute how will the APP know where to find the path to the icon or is there a better way?
    It would look in the path you're saying in the code. So, if the user doens't get the path, or the cursor in that folder, you're app will raise an error.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  9. #9
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by Mc Brain


    It would look in the path you're saying in the code. So, if the user doens't get the path, or the cursor in that folder, you're app will raise an error.

    Would it be better to place the icon in the same folder as the app then?

  10. #10
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Sure, but you'll have to package it and installed it on the user's folders.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  11. #11
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    is there any simpler way than that?

  12. #12
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    The simpler way is this, Just load the Picture in the MouseIcon property in design time, and forget about it.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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