none
PointToScreen获取的坐标不对(ToggleButton.PointToScreen(Point) help) RRS feed

  • 问题

  • wpf窗口。

    在我的窗口里有一个按钮,点击按钮后,在按钮右侧弹出一个提示框。

    在台机显示器(22寸,1920*1080)上,通过PointToScreen获取的位置是正确的。

    但是在surface(win8 平板)上不正确,位置出现偏差。win8平板的屏幕参数是:10.6寸,1920*1080分辨率的。

    求帮助。

    ------------------

    wpf window.

    there is a button in my window, It will show a prompt dialog box when click the button.

    In my desktop computer,  It is correct to get the position of the button by PointToScreen. my display's parameters are 22inch, 1920*1080 resolution.

    but,ti is incorrect to get the position of the button by PointToScreen in surface, it has offset, my surface's parameters are 10.6inch, 1920*1080 resolution.

    please help me! thanks.

    my English is poor,Also please forgive me 

    2013年10月21日 1:58

答案

  • 谢谢你的回复。

    我的问题已经解决,用方法PointFromScreen即可,是我自己没有想到,也没有仔细看这2个方法的区别,浪费你时间了,对不起。

    对于你的回复的代码我没有测试。

    ---------------

    thanks for your reply.

    my question has solved.i am sorry to waste your time. thanks!

    • 已标记为答案 oiooooio 2013年10月23日 1:25
    2013年10月23日 1:24
  • 您好,

    因为本人硬件条件有限,无法帮您测试PointToScreen方法在Surface上的问题,您可以尝试使用以下的方法获取相对屏幕的位置:

    static Point TransformToScreen(Point point, Visual relativeTo)
    {
    
    HwndSource hwndSource = PresentationSource.FromVisual(relativeTo) as HwndSource;
    
    Visual root = hwndSource.RootVisual;
    
    // Translate the point from the visual to the root.
    
    GeneralTransform transformToRoot = relativeTo.TransformToAncestor(root);
    
    Point pointRoot = transformToRoot.Transform(point);
    
    // Transform the point from the root to client coordinates.
    
    Matrix m = Matrix.Identity;
    
    Transform transform = VisualTreeHelper.GetTransform(root);
    
    
    if (transform != null)
    
    {
    
    m = Matrix.Multiply(m, transform.Value);
    
    }
    
    
    Vector offset = VisualTreeHelper.GetOffset(root);
    
    m.Translate(offset.X, offset.Y);
    
    
    Point pointClient = m.Transform(pointRoot);
    
    // Convert from “device-independent pixels” into pixels.
    
    pointClient = hwndSource.CompositionTarget.TransformToDevice.Transform(pointClient);
    
    
    POINT pointClientPixels = new POINT();
    
    pointClientPixels.x = (0 < pointClient.X) ? (int)(pointClient.X + 0.5) : (int)(pointClient.X - 0.5);
    
    pointClientPixels.y = (0 < pointClient.Y) ? (int)(pointClient.Y + 0.5) : (int)(pointClient.Y - 0.5);
    
    
    // Transform the point into screen coordinates.
    
    POINT pointScreenPixels = pointClientPixels;
    
    ClientToScreen(hwndSource.Handle, pointScreenPixels);
    
    
    return new Point(pointScreenPixels.x, pointScreenPixels.y);
    
    } 
    
    
    [StructLayout(LayoutKind.Sequential)]
    
    public class POINT
    
    {
    
    public int x = 0;
    
    public int y = 0;
    
    }
    
    [DllImport("User32", EntryPoint = "ClientToScreen", SetLastError = true, 
    ExactSpelling = true, CharSet = CharSet.Auto)]
    private static extern int ClientToScreen(IntPtr hWnd, [In, Out] POINT pt);
    

    以下为一些引用:

    #Code for getting screen relative Position in WPF
    http://blogs.msdn.com/b/llobo/archive/2006/05/02/code-for-getting-screen-relative-position-in-wpf.aspx

    #Visual.PointToScreen Method
    http://msdn.microsoft.com/en-us/library/system.windows.media.visual.pointtoscreen.aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 oiooooio 2013年10月23日 1:25
    2013年10月22日 7:10
    版主

全部回复

  • 您好,

    因为本人硬件条件有限,无法帮您测试PointToScreen方法在Surface上的问题,您可以尝试使用以下的方法获取相对屏幕的位置:

    static Point TransformToScreen(Point point, Visual relativeTo)
    {
    
    HwndSource hwndSource = PresentationSource.FromVisual(relativeTo) as HwndSource;
    
    Visual root = hwndSource.RootVisual;
    
    // Translate the point from the visual to the root.
    
    GeneralTransform transformToRoot = relativeTo.TransformToAncestor(root);
    
    Point pointRoot = transformToRoot.Transform(point);
    
    // Transform the point from the root to client coordinates.
    
    Matrix m = Matrix.Identity;
    
    Transform transform = VisualTreeHelper.GetTransform(root);
    
    
    if (transform != null)
    
    {
    
    m = Matrix.Multiply(m, transform.Value);
    
    }
    
    
    Vector offset = VisualTreeHelper.GetOffset(root);
    
    m.Translate(offset.X, offset.Y);
    
    
    Point pointClient = m.Transform(pointRoot);
    
    // Convert from “device-independent pixels” into pixels.
    
    pointClient = hwndSource.CompositionTarget.TransformToDevice.Transform(pointClient);
    
    
    POINT pointClientPixels = new POINT();
    
    pointClientPixels.x = (0 < pointClient.X) ? (int)(pointClient.X + 0.5) : (int)(pointClient.X - 0.5);
    
    pointClientPixels.y = (0 < pointClient.Y) ? (int)(pointClient.Y + 0.5) : (int)(pointClient.Y - 0.5);
    
    
    // Transform the point into screen coordinates.
    
    POINT pointScreenPixels = pointClientPixels;
    
    ClientToScreen(hwndSource.Handle, pointScreenPixels);
    
    
    return new Point(pointScreenPixels.x, pointScreenPixels.y);
    
    } 
    
    
    [StructLayout(LayoutKind.Sequential)]
    
    public class POINT
    
    {
    
    public int x = 0;
    
    public int y = 0;
    
    }
    
    [DllImport("User32", EntryPoint = "ClientToScreen", SetLastError = true, 
    ExactSpelling = true, CharSet = CharSet.Auto)]
    private static extern int ClientToScreen(IntPtr hWnd, [In, Out] POINT pt);
    

    以下为一些引用:

    #Code for getting screen relative Position in WPF
    http://blogs.msdn.com/b/llobo/archive/2006/05/02/code-for-getting-screen-relative-position-in-wpf.aspx

    #Visual.PointToScreen Method
    http://msdn.microsoft.com/en-us/library/system.windows.media.visual.pointtoscreen.aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 oiooooio 2013年10月23日 1:25
    2013年10月22日 7:10
    版主
  • 谢谢你的回复。

    我的问题已经解决,用方法PointFromScreen即可,是我自己没有想到,也没有仔细看这2个方法的区别,浪费你时间了,对不起。

    对于你的回复的代码我没有测试。

    ---------------

    thanks for your reply.

    my question has solved.i am sorry to waste your time. thanks!

    • 已标记为答案 oiooooio 2013年10月23日 1:25
    2013年10月23日 1:24