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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Rollover-Image Menu

Images, Etc.

Creating a Rollover-Image Menu

by  MikeBronner  Posted    (Edited  )
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:

Module:
=======
Code:
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

Form:
=====
Code:
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
        Else
            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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top