HELP: how to detect mouseclick in polygon? 
Author Message
 HELP: how to detect mouseclick in polygon?
Hi

I have a vb.net windows forms app in which a polygon is drawn. The polygon
contains four corners at random x,y coordinates.

I want to be able to check if the x,y coordinates of a mousemove event fall
inside a polygon, thus detecting if a user moves the mouse inside the
polygon.

Does anyone have any ideas on how to check this? I can retrieve the Point
objects of the polygon at any time, so I always have the x,y coordinates of
the polygon corners. The problem is that it can't be assumed that the
polygon is a perfect rectangle or square. How can I calculate if the given
x,y coordinates fall inside the polygon?

Or is it maybe possible to attach an event handler to a polygon for when it
is clicked?

Thanx for any help!



Thu, 22 Sep 2005 02:22:28 GMT  
 HELP: how to detect mouseclick in polygon?
Hello,


Quote:
> I have a vb.net windows forms app in which a
> polygon is drawn. The polygon contains four corners
> at random x,y coordinates.

> I want to be able to check if the x,y coordinates
> of a mousemove event fall inside a polygon, thus
> detecting if a user moves the mouse inside the
> polygon.

You can use regions to do this.

Simple example using lines:

\\\

' Liste unserer Objekte.
Private m_alElements As New ArrayList()

Public Class Element
  Public StartPoint As Point
  Public EndPoint As Point
  Public Region As Region
  Public Pen As Pen

  Public Sub UpdateRegion()
    Dim path As New Drawing.Drawing2D.GraphicsPath()
    path.AddLine(StartPoint, EndPoint)
    path.Widen(Pen)
    Region = New Region(path)
  End Sub
End Class

Private Sub Form1_Load( _
  ByVal sender As System.Object, _
  ByVal e As System.EventArgs _
) Handles MyBase.Load
  Dim el As New Element()
  With el
    .StartPoint = New Point(10, 10)
    .EndPoint = New Point(100, 100)
    .Pen = New Pen(Color.Red, 4)
    .UpdateRegion()
  End With
  m_alElements.Add(el)
  el = New Element()
  With el
    .StartPoint = New Point(30, 10)
    .EndPoint = New Point(50, 100)
    .Pen = New Pen(Color.Blue, 9)
    .UpdateRegion()
  End With
  m_alElements.Add(el)
End Sub

Private Sub Form1_Paint( _
  ByVal sender As Object, _
  ByVal e As System.Windows.Forms.PaintEventArgs _
) Handles MyBase.Paint
  Dim el As Element
  For Each el In m_alElements
    e.Graphics.DrawLine(el.Pen, el.StartPoint, el.EndPoint)
  Next el
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
  Dim el As Element
  For Each el In m_alElements
    If el.Region.IsVisible(New Point(e.X, e.Y)) Then
      el.Pen = New Pen(el.Pen.Color, el.Pen.Width * 2)
      el.UpdateRegion()
    End If
  Next el
  Me.Invalidate()
End Sub
///

Regards,
Herfried K. Wagner



Thu, 22 Sep 2005 03:30:40 GMT  
 HELP: how to detect mouseclick in polygon?
Hey, that works perfectly !!

Thanx for the quick response !



Quote:
> Hello,


> > I have a vb.net windows forms app in which a
> > polygon is drawn. The polygon contains four corners
> > at random x,y coordinates.

> > I want to be able to check if the x,y coordinates
> > of a mousemove event fall inside a polygon, thus
> > detecting if a user moves the mouse inside the
> > polygon.

> You can use regions to do this.

> Simple example using lines:

> \\\

> ' Liste unserer Objekte.
> Private m_alElements As New ArrayList()

> Public Class Element
>   Public StartPoint As Point
>   Public EndPoint As Point
>   Public Region As Region
>   Public Pen As Pen

>   Public Sub UpdateRegion()
>     Dim path As New Drawing.Drawing2D.GraphicsPath()
>     path.AddLine(StartPoint, EndPoint)
>     path.Widen(Pen)
>     Region = New Region(path)
>   End Sub
> End Class

> Private Sub Form1_Load( _
>   ByVal sender As System.Object, _
>   ByVal e As System.EventArgs _
> ) Handles MyBase.Load
>   Dim el As New Element()
>   With el
>     .StartPoint = New Point(10, 10)
>     .EndPoint = New Point(100, 100)
>     .Pen = New Pen(Color.Red, 4)
>     .UpdateRegion()
>   End With
>   m_alElements.Add(el)
>   el = New Element()
>   With el
>     .StartPoint = New Point(30, 10)
>     .EndPoint = New Point(50, 100)
>     .Pen = New Pen(Color.Blue, 9)
>     .UpdateRegion()
>   End With
>   m_alElements.Add(el)
> End Sub

> Private Sub Form1_Paint( _
>   ByVal sender As Object, _
>   ByVal e As System.Windows.Forms.PaintEventArgs _
> ) Handles MyBase.Paint
>   Dim el As Element
>   For Each el In m_alElements
>     e.Graphics.DrawLine(el.Pen, el.StartPoint, el.EndPoint)
>   Next el
> End Sub

> Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As
> System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
>   Dim el As Element
>   For Each el In m_alElements
>     If el.Region.IsVisible(New Point(e.X, e.Y)) Then
>       el.Pen = New Pen(el.Pen.Color, el.Pen.Width * 2)
>       el.UpdateRegion()
>     End If
>   Next el
>   Me.Invalidate()
> End Sub
> ///

> Regards,
> Herfried K. Wagner



Thu, 22 Sep 2005 06:08:08 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Detecting mouseclicks ?

2. Function to detect if a co-ordinate is within a 2d polygon

3. polygon - polygon intersection

4. Printing filled polygons - Help help...

5. Printing a filled polygon...help, help....

6. Need help on auto mouseclick!

7. help:Areas+polygon

8. Help -- need advice on drawing color filled polygon in VB

9. help...create polygon region API

10. Filling Polygons - Help

11. HELP: API Polygon Functions

12. Someone please help with Polygon API??

 

 
Powered by phpBB® Forum Software