zoukankan      html  css  js  c++  java
  • PointFromScreen和PointFromScreen的用法和区别

    如下面的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,该窗体最后运行出来相对于屏幕的

    位置如下图所示:

    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)。

  • 相关阅读:
    所谓瓶颈
    世界那么Big,组件那么Small
    PHPUnit单元测试中类protected/private方法测试
    WCF:如何将net.tcp协议寄宿到IIS
    Windows服务安装异常:System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security
    Windows 服务入门指南
    Visual studio 能否定位打开文件在项目中的位置
    电商框架调研
    可伸缩性/可扩展性(Scalable/scalability)
    LoadRunner基础入门教程
  • 原文地址:https://www.cnblogs.com/bear831204/p/1261019.html
Copyright © 2011-2022 走看看