[.NET] 坐标变换

坐标转换函数

假设在窗体上的Panel1里面放置一个BUTTON1控件

关于坐标的获取

1.

this.button1.location  //获取的是窗体相对于父容器的坐标,此处获取的坐标就是Button1相对于Panel的坐标

2.

获取相对于屏幕的坐标,此坐标一屏幕左上角为起点

point p1= this.button1.PointToScreen(this.button1.Location);

3.

转化为相对于父容器的坐标,此处得到结果和1一样

  Point p2 = this.button1.PointToClient(p1);

4.

获取BUTTON1相对于窗体的坐标,此处通过转换相对于屏幕的坐标来得到相对于窗体的坐标

把改点坐标转换为相对于窗体坐标

 Point p = this.PointToClient(p1);

5.ClientRectangle

获取表示控件的工作区的矩形。

控件的工作区是控件的边界减去非工作区元素(如滚动条、边框、标题栏和菜单)所以会有一定的偏差
类型:System.Drawing..::.Rectangle

一个 Rectangle,它表示控件的工作区。

6.RectangleToScreen 方法

计算指定工作区矩形的大小和位置(以屏幕坐标表示)。

参数

r
类型:System.Drawing..::.Rectangle

要转换的屏幕坐标 Rectangle

返回值

类型:System.Drawing..::.Rectangle

一个 Rectangle,它表示转换后的 Rectanglep(以屏幕坐标表示)。

7. DragOffset

拖放设置

8.本地坐标转换为屏幕坐标

  Point pos = this.Parent.PointToScreen(this.Location);
                pos.Offset(0, this.Height);

                pos = this.ParentForm.PointToClient(pos);
                
                this.PnlTree.Location = pos;

原文链接:http://hi.baidu.com/loun/blog/item/1b43bdafc9309ada7dd92a1d.html 
 

原文地址:https://www.cnblogs.com/reynold/p/2317219.html