用鼠标点击控件,通过Location属性得到的坐标是相对坐标,即相对父控件的位置。有时候,我们希望得到相对于屏幕的绝对位置,对于控制新弹出的对话框的位置,是非常友好的。
代码如下,需要的朋友可以参考。此段代码未考虑父控件边框的width,如SplitContainer的Splitterwidth,但影响不大。
Private Sub Button1_MouseClick(sender As Object, e As MouseEventArgs) Handles Button1.MouseClick
Dim sender001 As Object = sender
Dim point001 As Point = New Point(e.Location)
Do While sender001.parent IsNot Nothing
point001 = New Point(point001.X + sender001.parent.location.x, point001.Y + sender001.parent.location.y)
sender001 = sender001.parent
Loop
MsgBox(point001.X & " " & point001.Y)
End Sub