Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSee which part of a picture the mouse is over
Keywordspicture, map, mouse over
CategoriesGraphics
 
Make a mask image with the different areas colored. Use Point to find the color of the pixel corresponding to the mouse position. Use the color to identify the area.
 
' The user is moving the mouse over the picture.
' Display the name of the cat the mouse is over.
Private Sub picCats_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
Dim clr As Long

    ' Find the color of the corresponding pixel
    ' in the mask.
    clr = picMask.Point(X, Y)

    ' See which cat this is.
    On Error Resume Next
    CurrentCat = CatNames(Str$(clr))

    ' See if we had an error looking for the name.
    If Err.Number <> 0 Then
        ' There was an error. This color is not
        ' in the collection.
        CurrentCat = ""
    End If

    ' Display the cat's name.
    If lblName.Caption <> CurrentCat _
        Then lblName.Caption = CurrentCat
End Sub
 
Note that this technique doesn't work well if you have very many regions in the picture and a low color depth. For example, if you are running 8-bit color and 50 regions, there aren't enough colors to distinguish every region.

One solution is to put only outlines in the mask and record the coordinates of an anchor point for each region. To see which region is below the mouse, flood the are under the mouse with a color and see which region's anchor point changed color. Don't forget to change the color back.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated