sorry, ignore the CreateGraphics part.
Main Topics
Browse All TopicsHi,
Assume i have a form with a picturebox on it,
the image in the picturebox is a dartboard. (Darts as in England --> image of dartboard here: http://www.amisane.org/ima
What i would like to accomplish: if the user clicks somewhere on the image i want to know where on the board the user clicked so i can allow points.
I know how to make polygons on the picturebox, so i thought i could use that.
First of all, there are alot (very very alot lol) polygons to be created,
and second, how do i know on what polygon the user clicked... (can a polygon be named? or would i just use the X,Y coordinates to find out in what polygon the user clicked? --> seems that would slow down the code very much)
I don't know if i'm following the correct way to do this, let alone i would be aware of another solution...
Btw, if this can be done in VB6, that would also be good.
.Net would be really great!
cheers
Ricky
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Use Polar coordinates. Then you can "define" the areas of the board as regions between a start and stop angle and a start and stop distance from the center of the dartboard:
http://en.wikipedia.org/wi
Hi Idle_mind,
looks very interesting,
but i don't have a clue on how to start coding that...
Do you have an example?
About polygons
Can polygons be given a name when created?
'This example draws a polygon in the Triple 20 area
Private Sub cmdDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDraw.Click
Dim g As System.Drawing.Graphics
Dim pen1 As New System.Drawing.Pen(Color.B
Dim p(7) As System.Drawing.Point
p(0).X = 365
p(0).Y = 195
'Draw again, or there will be a line from the left upper corner of the picturebox to the polygon
p(1).X = 365
p(1).Y = 195
p(2).X = 393
p(2).Y = 194
p(3).X = 419
p(3).Y = 195
p(4).X = 416
p(4).Y = 208
p(5).X = 394
p(5).Y = 207
p(6).X = 368
p(6).Y = 207
'Do the first point again, to close the polygon
p(7).X = 365
p(7).Y = 195
g = PictureBox1.CreateGraphics
g.DrawPolygon(pen1, p)
End Sub
Is there anyway to know if the user clicked the polygon?
I was thinking in the lines of like a clickable map in HTML.
The part the mouse hovers over "lights up", like a link. When clicked the link would "send" a value to a subroutine or something..... ???
cheers
Ricky
Come to think,
i can create a HTML-page, with the image on it,
create all the area shapes (all the polygons) and put links to it
The question is then, how do i put this page into a .net form?
And, if that can be done, how do i get the link-value into my .net form (code) after i click a polygon in the image in the html-page (which should be inside my form)?
Anyone?
cheers
Ricky
If you want to define the darboard as multiple polygons then here is an approach that may work for you:
Imports System.Drawing.Drawing2D
Public Class Form1
Private polygons As New ArrayList
Private polygonNames As New Hashtable
Private lastPolygon As GraphicsPath = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim gp As New GraphicsPath
Dim p(7) As System.Drawing.Point
p(0).X = 365
p(0).Y = 195
p(1).X = 365
p(1).Y = 195
p(2).X = 393
p(2).Y = 194
p(3).X = 419
p(3).Y = 195
p(4).X = 416
p(4).Y = 208
p(5).X = 394
p(5).Y = 207
p(6).X = 368
p(6).Y = 207
p(7).X = 365
p(7).Y = 195
gp = New GraphicsPath
gp.AddPolygon(p)
polygons.Add(gp)
polygonNames.Add(gp, "Triple 20 Area")
' create more polygons just like above...
' ...and add them to "polygons" and "polygonNames"
End Sub
Private Sub PictureBox1_MouseMove(ByVa
Dim curPolygon As GraphicsPath = Nothing
For Each polygon As GraphicsPath In polygons
If polygon.IsVisible(e.X, e.Y) Then
curPolygon = polygon
Exit For
End If
Next
If Not (curPolygon Is Nothing) Then
If Not (lastpolygon Is curPolygon) Then
lastPolygon = curPolygon
PictureBox1.Refresh()
Label1.Text = polygonNames.Item(curPolyg
End If
ElseIf Not (lastPolygon Is Nothing) Then
lastPolygon = Nothing
Label1.Text = ""
PictureBox1.Refresh()
End If
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.Paint
If Not (lastPolygon Is Nothing) Then
Dim pen1 As New System.Drawing.Pen(Color.B
e.Graphics.DrawPath(pen1, lastPolygon)
pen1.Dispose()
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVa
Dim curPolygon As GraphicsPath = Nothing
For Each polygon As GraphicsPath In polygons
If polygon.IsVisible(e.X, e.Y) Then
curPolygon = polygon
Exit For
End If
Next
If Not (curPolygon Is Nothing) Then
MessageBox.Show(polygonNam
End If
End Sub
End Class
"Btw, if this can be done in VB6, that would also be good.
.Net would be really great!"
This can also be done in VB6 but you would have to use lower level GDI APIs to create the polygons, do the "hit testing", and paint them into your PictureBox.
If you have VB.Net 2005 then you can use the Generic List instead of an ArrayList.
If you want to really go with the concept then write a custom class to encapsulate the GraphicsPath. Then you can store a name AND a value for each path. This will make the code cleaner since you wouldn't seperate structures to hold the paths, names and values...
Business Accounts
Answer for Membership
by: craskinPosted on 2007-10-03 at 12:13:19ID: 20009181
You could do something like this:
EventArgs) Handles _
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
As
System.Windows.Forms.Mouse
MyBase.MouseDown
' Clear previous results.
Dim gr As Graphics = Me.CreateGraphics()
DrawGraph(gr)
' Convert the X coordinate into world coordinates.
Dim m As Matrix = gr.Transform()
m.Invert()
Dim pts() As PointF = {New PointF(e.X, e.Y)}
m.TransformPoints(pts)
Dim x0 As Single = pts(0).X
Dim y0 As Single = pts(0).Y
'do something with X,Y
End Sub