即使在重新添加处理程序

时间:2015-05-19 12:17:07

标签: wpf vb.net mouseevent

在我的程序Cyrus-Beck算法中,我必须剪切点。剪切窗口内的点必须为红色。外面仍然是黑色的。

我将程序中的点表示为rectangle,并给出了删除该点的事件处理程序。当我想删除点时,窗口外的点正在工作,但是多边形内部的点不起作用,事件是右键单击事件没有调用。

这是我的Point类。

忽略Component类,因为它只是在主窗口中处理画布

        Public Class Points
            Inherits Component

            Public items As List(Of Point) = New List(Of Point)()
            Public rects As List(Of Rectangle) = New List(Of Rectangle)


        'add element
            Public Overrides Sub Canvas1_MouseMove(sender As Object, e As MouseEventArgs)
                canvas.Cursor = Cursors.Pen
            End Sub

            Public Overrides Sub Canvas1_MouseDown(sender As Object, e As MouseButtonEventArgs)
                canvas.Cursor = Cursors.Pen
                If e.LeftButton = MouseButtonState.Pressed Then
                    items.Add(e.GetPosition(canvas))

                    Dim p As Rectangle = New Rectangle()
                    p.Width = 3
                    p.Height = 3
                    canvas.SetLeft(p, e.GetPosition(canvas).X)
                    canvas.SetTop(p, e.GetPosition(canvas).Y)
                    p.Stroke = Brushes.Black
                    p.Fill = Brushes.Black
                    rects.Add(p)
                End If
            End Sub



    'deleting element
'this method not calling when the element has updated
            Sub point_rightClick(sender As Object, e As MouseButtonEventArgs)
                Dim temp As Rectangle = New Rectangle()
                temp = DirectCast(sender, Rectangle)
                Dim pt As Point = New Point(Controls.Canvas.GetLeft(temp), Controls.Canvas.GetTop(temp))
                canvas.Children.Remove(temp)
                items.Remove(pt)
                rects.Remove(temp)
            End Sub

            Public Shared Function samePoint(one As Rectangle, two As Rectangle) As Boolean
                Return ((Controls.Canvas.GetLeft(one) = Controls.Canvas.GetLeft(two)) And (Controls.Canvas.GetTop(one) = Controls.Canvas.GetTop(two)))
            End Function

            Public Shared Function samePoint(one As Point, two As Rectangle) As Boolean
                Return (one.X = Controls.Canvas.GetLeft(two)) And (one.Y = Controls.Canvas.GetTop(two))
            End Function
        End Class 

为了能够删除,我deleting表示状态,如果deletingtruecurrentState = MouseState.Pointer。代码将为所有points(矩形)添加处理程序。

Private Sub chge_pointer_Click(sender As Object, e As RoutedEventArgs) Handles chge_pointer.Click
    currentState = MouseState.Pointer
    deleting = True

    chooseHandler()

End Sub


Private Sub chooseHandler()
        If deleting Then
            For Each r As Rectangle In points.rects
                AddHandler r.MouseRightButtonDown, AddressOf points.point_rightClick
            Next
        Else
            For Each r As Rectangle In points.rects
                RemoveHandler r.MouseRightButtonDown, AddressOf points.point_rightClick
            Next
        End If

    End Sub

在我将点修改为红色之前,我必须有一个循环来检查位置是否相同,如果位置相同则编辑点

    Dim z As CyrusBeck = New CyrusBeck(clippingWindow)
                z.Clip(points.items)
'CyrusBeck has a properties 'points'
'CyrusBeck.points saves the all the point that inside the clipping window
                For i As Integer = 0 To points.rects.Count - 1
                    For Each p As Point In z.points
                        If CGAProject.Points.samePoint(p, points.rects.Item(i)) Then
                            Dim temp_r As Rectangle = New Rectangle()
                            temp_r = points.rects.Item(i)

                            temp_r.Stroke = Brushes.Red
                            temp_r.Fill = Brushes.Red
                            points.rects.Add(temp_r)
                            points.rects.RemoveAt(i)
                        End If
                    Next
                Next

当程序达到此目的时,currentState永远不会是MouseState.Pointer。因此,如果用户想要删除一个点,则必须按下按钮chge_pointer,这样我的chge_pointer_Click才会被调用,我的所有处理程序都将被重新设置。

Rectangle.MouseRightButtonDown更新后,为什么不调用rectangle? 没有更新的矩形调用Rectangle.MouseRightButtonDown事件,所以我重新设置了处理程序。

是什么?

有没有其他方法可以让用户删除窗口内外的矩形?

注意:

  • 在我将Rectangle分配回列表后,我已经重置了处理程序,但它仍然无法正常工作。
  • 在我将AddHandler temp_r.MouseRightButtonDown, AddressOf points.point_rightClick分配回列表后,我也创建了Rectangle,但它也没有。

1 个答案:

答案 0 :(得分:0)

在您点击的活动结束时,例如:

Sub point_rightClick(sender As Object, e As MouseButtonEventArgs)

我认为你应该有一个"句柄"。

E.g。

Sub point_rightClick(sender As Object, e As MouseButtonEventArgs) Handles point.MouseClick

如果没有这个,sub将永远不会被调用(因为它似乎正在发生)。

此方法也没有"句柄"

Public Overrides Sub Canvas1_MouseDown(sender As Object, e As MouseButtonEventArgs)

可能是:

Public Overrides Sub Canvas1_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Canvas1.MouseDown