我正在寻找一个属性,它给我一个Control的位置相对于Form的位置,而不是Form的ClientRectangle's"0,0".
当然我可以将所有内容转换为屏幕坐标,但我想知道是否有更直接的方法来做到这一点.
Jim*_*hel 10
您需要转换为屏幕坐标,然后进行一些数学运算.
Point controlLoc = form.PointToScreen(myControl.Location);
Run Code Online (Sandbox Code Playgroud)
表单的位置已经在屏幕坐标中.
现在:
Point relativeLoc = new Point(controlLoc.X - form.Location.X, controlLoc.Y - form.Location.Y);
Run Code Online (Sandbox Code Playgroud)
这将为您提供相对于表单左上角的位置,而不是相对于表单的客户区域.
我认为这将回答您的问题。注意,“ this”是表格。
Rectangle screenCoordinates = control.Parent.ClientToScreen(control.ClientRectangle);
Rectangle formCoordinates = this.ScreenToClient(screenCoordinates);
Run Code Online (Sandbox Code Playgroud)