PointFromScreen和PointFromScreen的用法和区别
2013-05-19 00:00
375 查看
如下面的xaml代码:
<Window x:Class="WindowsApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowsApplication5" Height="300" Width="300" WindowStartupLocation="CenterScreen"
>
<Grid>
<Canvas Name="canvas">
<Button Name="btnClick" Width="30" Height="20" Canvas.Left="50" Canvas.Top="50" VerticalAlignment="Center"HorizontalAlignment="Center">click me</Button>
</Canvas>
</Grid>
</Window>
在Canvas上面有一个Button,btnClick相对于canvas的位置是50和50,该窗体最后运行出来相对于屏幕的
位置如下图所示:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/06/b9d7c7d7bf4fe56d9184b551f14b9fce.jpg)
UIElement.PointFromScreen(Point):参数是屏幕Screen中的一个坐标点,这个函数的作用是把Screen中的一个点的坐标转换成该窗体Window中的坐标,参照点是UIElement的位置(当作(0,0)),所以:
btnClick.PointFromScreen(new Point(0, 0)); 值为
(-416,-292) -- screen的(0,0)相对于btnClick的topleft(当作(0,0))的坐标。
btnClick.PointFromScreen(new Point(50, 50));
值为 (-366,-242) -- screen的(50,50)相对于btnClick的topleft(当作(0,0))的坐标。
canvas.PointFromScreen(new Point(0, 0));
值为 (-366,-242) -- screen的(0,0)相对于canvas的topleft(当作(0,0))的坐标。
Canvas.GetLeft(btnClick); 值为50
Canvas.GetTop(btnClick); 值为50
UIElement.PointToScreen(Point):参数是窗体Window中的一个坐标,参照点是相对于UIElement(位置当作(0,0)),偏移量为Point的一个点,返回该点在Screen中的坐标:
btnClick.PointToScreen(new Point(50,50)); // (466,342)
canvas.PointToScreen(new Point(50,50)); // (416,292)
注意屏幕的左上角的坐标是(0,0)。
另外学习
e.GetPosition(UIElement)的用法,也可以用Mouse.GetPosition(UIElement)达到相同的效果。
通常e是一个鼠标单击事件的EventArgs,例如:
btnClick.PreviewMouseLeftButtonDown += new MouseButtonEventHandler
(btnClick_PreviewMouseLeftButtonDown);
void btnClick_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point p1 = e.GetPosition(sender as Button);
Point p2 = e.GetPosition(canvas);
}
如果我单击btnClick上很靠近左上角的一个地方,假设该点相对于btnClick左上角的坐标是(2,2),
那么p1的值是(2,2),p2(52,52)。
<Window x:Class="WindowsApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowsApplication5" Height="300" Width="300" WindowStartupLocation="CenterScreen"
>
<Grid>
<Canvas Name="canvas">
<Button Name="btnClick" Width="30" Height="20" Canvas.Left="50" Canvas.Top="50" VerticalAlignment="Center"HorizontalAlignment="Center">click me</Button>
</Canvas>
</Grid>
</Window>
在Canvas上面有一个Button,btnClick相对于canvas的位置是50和50,该窗体最后运行出来相对于屏幕的
位置如下图所示:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/06/b9d7c7d7bf4fe56d9184b551f14b9fce.jpg)
UIElement.PointFromScreen(Point):参数是屏幕Screen中的一个坐标点,这个函数的作用是把Screen中的一个点的坐标转换成该窗体Window中的坐标,参照点是UIElement的位置(当作(0,0)),所以:
btnClick.PointFromScreen(new Point(0, 0)); 值为
(-416,-292) -- screen的(0,0)相对于btnClick的topleft(当作(0,0))的坐标。
btnClick.PointFromScreen(new Point(50, 50));
值为 (-366,-242) -- screen的(50,50)相对于btnClick的topleft(当作(0,0))的坐标。
canvas.PointFromScreen(new Point(0, 0));
值为 (-366,-242) -- screen的(0,0)相对于canvas的topleft(当作(0,0))的坐标。
Canvas.GetLeft(btnClick); 值为50
Canvas.GetTop(btnClick); 值为50
UIElement.PointToScreen(Point):参数是窗体Window中的一个坐标,参照点是相对于UIElement(位置当作(0,0)),偏移量为Point的一个点,返回该点在Screen中的坐标:
btnClick.PointToScreen(new Point(50,50)); // (466,342)
canvas.PointToScreen(new Point(50,50)); // (416,292)
注意屏幕的左上角的坐标是(0,0)。
另外学习
e.GetPosition(UIElement)的用法,也可以用Mouse.GetPosition(UIElement)达到相同的效果。
通常e是一个鼠标单击事件的EventArgs,例如:
btnClick.PreviewMouseLeftButtonDown += new MouseButtonEventHandler
(btnClick_PreviewMouseLeftButtonDown);
void btnClick_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point p1 = e.GetPosition(sender as Button);
Point p2 = e.GetPosition(canvas);
}
如果我单击btnClick上很靠近左上角的一个地方,假设该点相对于btnClick左上角的坐标是(2,2),
那么p1的值是(2,2),p2(52,52)。
相关文章推荐
- PointFromScreen和PointFromScreen的用法和区别
- select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的用法和区别
- Python之import与from...import的区别与用法
- insert into select 和select into from的用法和区别
- select into from 和 insert into select 的用法和区别
- made of、made by、made from、 made in的区别及其用法
- select into from 和 insert into select 的区别和用法及 SQL SELECT INTO 中Undeclared variable错误解决办法
- Python import用法以及与from...import的区别
- select into from 和 insert into select 的用法和区别
- select into from 提示 Undeclared variable.....错误的解决办法 && select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的用法和区别
- SELECT INTO FROM 和 INSERT INTO SELECT 的用法和区别
- RxJava 1.x from()和just()用法、区别和举例
- elementFromPoint的用法(详解)
- insert into select 和select into from的用法和区别
- insert into select 和select into from的用法和区别(转)
- mysql中删除表记录delete from和truncate table的用法区别