Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ

Images, Etc.

Creating a Rollover-Image Menu by MikeBronner
Posted: 21 Jun 01

This menu will act as the webpage rollover menus work: the user moves the mouse over a menu-item, which then changes its appearance, or even gives audio feedback.

Although not as simple as with HTML, it can be done in VB using a few simple tricks (once you know them, they're all simple!).

When designing my menu system, I noticed that VB has a MouseMove event, which recognizes when the mouse moves over the object in question.
This solved the problem of initiating the image change.

The real problem was resetting the image to its normal state, once the mouse left the object(s) in question. I scoured the net in search of various solutions (there are some out there, but none that adequately work in an efficient manner), but didn't come up with much.
I figured I need to reset the image based on the pixel-location of the mouse.

Now I had a new problem: which event would look at the mouse movement other than the mousemove event? I searched the forum archives and saw that another user had experienced exactly my same problem. His solution was to use the timer object. This turned out to be the perfect solution.

It is necessary to create a timer object on the form you wish to create your menu on. Also, the menu is based on a control array of picture boxes (all same dimensions and alignment) aligned vertically.

I also added a windows sound event, whenever the button is moved over or clicked on (see my Sound FAQ for more details). The following is the code I used:

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As Single, ByVal hmodule As Long, ByVal dwflags As Long) As Long

Public MousePoint As POINTAPI
Public Const SND_ASYNC = &H1

Public Type POINTAPI
    x As Long
    y As Long
End Type

Dim nMove as Integer
Dim bMove as Boolean
Dim nTile as integer

Private Sub Form_Load()
    nMove = picBox.Count * 2 'initialize to a value out of range of menu items
    nTile = picBox.Count * 2
End Sub

Private Sub picBox_Click(Index As Integer)
    PlaySound "Open", 0, SND_ASYNC Or SND_ALIAS
End Sub

Private Sub Timer1_Timer()
    Dim i as Integer
    Dim xCord as Single
    Dim yCord as Single

    GetCursorPosition MousePoint
    xCord = Mousepoint.x - (Form1.Left / Screen.TwipsPerPixelX + 4) 'add 4 pixels for the border
    yCord = MousePoint.y - (Form1.Top / ScreenTwipsPerPixelY + 4) ' add 4 pixels for the border
    bMove = false
    For i = 0 To picBox.Count * 2
        If ((xCord >= picBox(i).Left/ Screen.TwipsPerPixelX) And (xCord <= picBox(i).Left/ Screen.TwipsPerPixelX + picBox(i).Width/ Screen.TwipsPerPixelX) And (yCord >= picBox(i).Top/ Screen.TwipsPerPixelY) And (yCord <= picBox(i).Top/ Screen.TwipsPerPixelY + picBox(i).Height/ Screen.TwipsPerPixelY)) Then
            nTile = i
            Exit For
            nTile = picBox.Count * 2
        End If
    Next i
    If ((nTile <> nMove) And (nMove <> picBox.Count * 2)) Then
        picBox(nMove).Picture = LoadPicture("[i][Path to the standard picture here][/i]")
        nMove = picBox.Count * 2
    End If
    If ((nTile <> nMove) And (nTile <> picBox.Count * 2)) Then
        picBox(nTile).Picture = LoadPicture("[i][Path to the highlighted picture here][/i]")
        nMove = nTile
    End If
End Sub

This code is sensitive to each button, regardless where the button is placed (the code I had on here earlier was not).
Please let me know if you have any suggestions, comments, or corrections.

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close