设置鼠标的位置,在画布上引发单击事件

时间:2013-11-29 20:47:08

标签: wpf vb.net canvas

我现在正在使用的这个功能有点棘手,所以我需要一些帮助才能找到解决方案。 问题是我必须从代​​码中在画布上引发MouseDown事件,并在画布中传递鼠标的位置以及其余参数。 是VB.Net中的一个项目,我知道我不需要'Call'关键字,但我保留它只是为了澄清代码:) 这是我的代码,但仍然不知道如何通过该调用的鼠标位置。

    Call picture_MouseDown(picture, New MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left))

假设我在这两个变量上有X和Y位置:

    CInt(XC), CInt(YC)

提前感谢您,我真的很感激任何帮助:)

1 个答案:

答案 0 :(得分:0)

您不应直接调用事件处理程序。相反,将单击图片框时出现的功能分离为单独的方法并从两个位置调用该方法:

Class MainWindow 
    Private Sub Canvas1_MouseDown(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs) Handles Canvas1.MouseDown
        DoSomethingWithCanvas(sender, e.GetPosition(sender), e.ChangedButton)
    End Sub

    Private Sub SomeOtherSub()
        Dim XC As Double = 100.0
        Dim YC As Double = 100.0

        'Do the code that would normally happen if the picturebox was actually clicked.
        DoSomethingWithCanvas(Canvas1, New Point(CInt(XC), CInt(YC)), MouseButton.Left)
    End Sub

    Private Sub DoSomethingWithCanvas(picBox As Canvas, loc As Point, button As MouseButton)
        'Some Code here that manipulates the Canvas
        If button = MouseButton.Left Then
            'Do something for left button click
        End If
    End Sub
End Class