Creating an ImageMapAs you browse the Web, you've probably noticed that many Web site feature an image map to help navigate the site. An image map is a single image, but it's sensitive to the location at which you click it. This tip demonstrates how to create an image map. You can download an example file to try it out for yourself. To create an image map, you must be familiar with the MouseOver event. This event is triggered whenever the mouse pointer moves over a particular control. In this case, the control is an Image control. If you're not familiar with the MouseOver event, consult the online help for details. Basically, this event lets you determine the horizontal and vertical coordinates of the mouse pointer, relative to the control. The image I use in the example is shown below.
Setting up the image map simply involves monitoring the mouse position, and taking the appropriate action if the control is clicked. The VBA code is shown below. Every mouse movement over the Image control triggers the MouseMove event and executes the Image1_MouseMove subroutine. The mouse coordinates are passed to the subroutine via the X and Y arguments. A series of Select Case statements determines which flag is under the mouse pointer, and stores the country name in the Country variable. I also display the country in the status bar. Obviously, it will take some work to figure out the coordinates, and you'll probably want to use an image in which the "subimages" are rectangular. When the image is clicked, the Image1_Click subroutine is executed. This subroutine simply activates a worksheet, the name of which is stored in the Country variable. The worksheets are named for the countries represented by the flags. VBA CodeIn this example, the Image control is placed on a worksheet named Menu. Therefore, this code resides in the code module for the Menu sheet. Public Country As String
Private Sub Image1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Select Case Y 'Vertical coordinate
Case Is < 2 'Off the map
Country = ""
Case Is < 47
Select Case X 'Horizontal coordinate
Case Is < 2: Country = ""
Case Is < 78: Country = "United States"
Case Is < 152: Country = "Canada"
Case Else: Country = "" 'Off the map
End Select
Case Is < 87
Select Case X 'Horizontal coordinate
Case Is < 2: Country = ""
Case Is < 40: Country = ""
Case Is < 112: Country = "Cuba"
Case Else: Country = "" 'Off the map
End Select
Case Is < 133
Select Case X 'Horizontal coordinate
Case Is < 2: Country = ""
Case Is < 78: Country = "Mexico"
Case Is < 152: Country = "Puerto Rico"
Case Else: Country = "" 'Off the map
End Select
Case Else 'Off the map
Country = ""
End Select
' The statement below was used while figuring out the coordinates
' Application.StatusBar = Country & " " & X & " " & Y
Application.StatusBar = Country
End Sub
Private Sub Image1_Click()
If Country <> "" Then Sheets(Country).Activate
' Reset the status bar
Application.StatusBar = False
End Sub
NOTE: Another way to create an image map is to insert transparent objects over the image, and create event-handler subroutines for the objects. |