Link to home
Start Free TrialLog in
Avatar of skyzipper
skyzipperFlag for Afghanistan

asked on

How to get image coordinates on a windows form and make sections clickable

Hello experts,
I am making a visual studio 2008 windows form.  Within the form i have a picture box, with an image set to fill the entire control.  I want to make certain portions of the image clickable.
First i need to get the coordinates of the mouse location, than define my rectangles on the image.  Has anyone done this.  I know in html you can use code like this:
<img src ="planets.gif" width="145" height="126" alt="Planets" usemap ="#planetmap" />

<map id ="planetmap" name="planetmap">
<area shape ="rect" coords ="0,0,82,126" href ="sun.htm" alt="Sun" />
<area shape ="circle" coords ="90,58,3" href ="mercur.htm" alt="Mercury" />
<area shape ="circle" coords ="124,58,8" href ="venus.htm" alt="Venus" />
</map>

Here is my window form code to get the mouse location of the click
'This code keeps the form from being moved, which will change the location of the mouse click
 Protected Overloads Overrides Sub WndProc(ByRef m As Message)
        Const WM_NCLBUTTONDOWN As Integer = 161
        Const WM_SYSCOMMAND As Integer = 274
        Const HTCAPTION As Integer = 2
        Const SC_MOVE As Integer = 61456
        If (m.Msg = WM_SYSCOMMAND) AndAlso (m.WParam.ToInt32() = SC_MOVE) Then
            Exit Sub
        End If

        If (m.Msg = WM_NCLBUTTONDOWN) AndAlso (m.WParam.ToInt32() = HTCAPTION) Then
            Exit Sub
        End If
        MyBase.WndProc(m)
    End Sub


    Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
        x = System.Windows.Forms.Control.MousePosition.X
        y = System.Windows.Forms.Control.MousePosition.Y
        MessageBox.Show("You clicked x cor: " & x & "  You Clicked y cord: " & y)
    End Sub
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Is the image static?  Can you draw an invisible panel over the image?  Then on mouse enter of the panel you can set the cursor.  When exiting set it back to default.

Then each region has it's own event, which makes your code a lot easier to manage.


What's the "end game"?  (EG: what are you doign with the regions?  What happens on the clicks, etc)
You can create a GraphicsPath() instance that represents each "hot spot" on your image:
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath(VS.71).aspx

Next, create a Region() from those GraphicsPath() instances:
http://msdn.microsoft.com/en-us/library/system.drawing.region.aspx

Finally, you can use the Region.IsVisible() function to determine if the cursor is contained by one of the hot spots:
http://msdn.microsoft.com/en-us/library/dz11htdf.aspx
Avatar of skyzipper

ASKER

ged225, i tried using panels, but when i set the background to transparent it still covers the image and if i set its z axis to behind the images, none of the panel's events fire.  I will have to try idle_minds.
Not sure the regions will work either, how to i mouse event hander?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial