
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