Solved

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

Posted on 2012-03-13
5
530 Views
Last Modified: 2012-03-30
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
0
Comment
Question by:skyzipper
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37715296
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)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37715587
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
0
 

Author Comment

by:skyzipper
ID: 37716152
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.
0
 

Author Comment

by:skyzipper
ID: 37716293
Not sure the regions will work either, how to i mouse event hander?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 320 total points
ID: 37716778
Here's a simple example that highlights the GraphicsPath when the mouse is within:
Imports System.Drawing.Drawing2D
Public Class Form1

    Private CurPath As Integer = -1
    Private Regions As New List(Of Region)
    Private Paths As New List(Of GraphicsPath)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim gp As New GraphicsPath
        gp.AddEllipse(New Rectangle(50, 50, 125, 75))
        Paths.Add(gp)
        Regions.Add(New Region(gp))

        gp = New GraphicsPath
        gp.AddPolygon(New Point() {New Point(250, 250), New Point(325, 300), New Point(175, 275)})
        Paths.Add(gp)
        Regions.Add(New Region(gp))

        gp = New GraphicsPath
        gp.AddClosedCurve(New Point() {New Point(400, 125), New Point(425, 175), New Point(500, 225), New Point(375, 200)})
        Paths.Add(gp)
        Regions.Add(New Region(gp))
    End Sub

    Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        For i As Integer = 0 To Paths.Count - 1
            e.Graphics.DrawPath(IIf(CurPath = i, Pens.Red, Pens.Black), Paths(i))
        Next
    End Sub

    Private Sub PictureBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        Dim Hit As Boolean = False
        For i As Integer = 0 To Regions.Count - 1
            If Regions(i).IsVisible(e.X, e.Y) Then
                Hit = True
                If CurPath <> i Then
                    CurPath = i
                    PictureBox1.Refresh()
                    Exit For
                End If
            End If
        Next
        If Not Hit Then
            CurPath = -1
            PictureBox1.Refresh()
        End If
    End Sub

End Class

Open in new window


The mouse is over the squiggle shape thing...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now