获取WPF按钮的角坐标

时间:2019-01-07 14:16:47

标签: wpf vb.net xaml button

这似乎很简单,但是确实有问题!我有一个按钮,当我单击它时,我想要一个角的x,y坐标,因此我可以从该按钮的某个位置弹出一个窗口。我知道按钮的高度和宽度,我可以获得用鼠标单击的位置的坐标,但确实很难获取角的坐标。这是我到目前为止的内容:

Private Sub HandleClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim clickLocationPos = Mouse.GetPosition(Window.GetWindow(Me))

        Dim xPos = clickLocationPos.X
        Dim yPos = clickLocationPos.Y
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

这应该给出相对于父窗口而言,单击的Button的左上角坐标:

Private Sub HandleClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim button = CType(sender, Button)
    Dim topLeftCorner = button.TransformToAncestor(Me).Transform(New Point(0, 0))
    Dim xPos = topLeftCorner.X
    Dim yPos = topLeftCorner.Y
End Sub