Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取WPF中窗口的实际位置_C#_Wpf - Fatal编程技术网

C# 获取WPF中窗口的实际位置

C# 获取WPF中窗口的实际位置,c#,wpf,C#,Wpf,如何获取WPF窗口的真实位置(以像素为单位) this.PointFromScreen(新点(0,0))不会给出屏幕上的实际位置。我想这与缩放有关,但在我的例子中,我需要屏幕上的“真实”位置 我想你想要PointFromScreen将屏幕坐标转换为Visual的坐标系 var window = new Window(); Point screenCoordinates = window.PointToScreen(new Point(0,0)); 我有这段代码,但老实说,我不记得是否至少使用

如何获取WPF窗口的真实位置(以像素为单位)

this.PointFromScreen(新点(0,0))不会给出屏幕上的实际位置。我想这与缩放有关,但在我的例子中,我需要屏幕上的“真实”位置

我想你想要
PointFromScreen
将屏幕坐标转换为
Visual
的坐标系

var window = new Window();
Point screenCoordinates = window.PointToScreen(new Point(0,0)); 

我有这段代码,但老实说,我不记得是否至少使用过一次。我相信它应该“体面地”工作

顺便说一句,我想知道你为什么需要类似的信息

    [StructLayout(LayoutKind.Sequential)]
    private struct POINT
    {
        public int X;
        public int Y;
    }


    /// <summary>
    /// The ClientToScreen function converts the client-area coordinates of a specified point to screen coordinates.
    /// </summary>
    /// <param name="hWnd">Handle to the window whose client area is used for the conversion.</param>
    /// <param name="pt">Pointer to a POINT structure that contains the client coordinates to be converted. The new screen coordinates are copied into this structure if the function succeeds.</param>
    /// <returns>If the function succeeds, the return value is nonzero.</returns>
    [DllImport("User32", EntryPoint = "ClientToScreen", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool ClientToScreen(
        IntPtr hWnd,
        ref POINT pt);


    /// <summary>
    /// Code for getting screen relative Position in WPF
    /// </summary>
    /// <param name="point">The source point to be trasnformed</param>
    /// <param name="relativeTo">The Visual object used as reference</param>
    /// <returns>The screen-relative point obtained by the trasformation</returns>
    /// <remarks>
    /// http://blogs.msdn.com/llobo/archive/2006/05/02/Code-for-getting-screen-relative-Position-in-WPF.aspx
    /// One of the common customer queries that we see on the forums 
    /// is to get the screen relative position of  a point.
    /// Currently we do not provide an API which allows this functionality.
    /// However, Nick Kramer came up with this code on the forum 
    /// and it works great for LTR (left to right) systems. 
    /// Following is the code for getting the screen relative position.
    /// </remarks>
    [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
    public static Point? TransformToScreen(
        Point point,
        Visual relativeTo)
    {
        // Translate the point from the visual to the root.
        var hwndSource = PresentationSource.FromVisual(relativeTo) as HwndSource;
        if (hwndSource == null)
            return null;
        var root = hwndSource.RootVisual;

        // Transform the point from the root to client coordinates.
        var transformToRoot = relativeTo.TransformToAncestor(root);
        var pointRoot = transformToRoot.Transform(point);
        var m = Matrix.Identity;
        var transform = VisualTreeHelper.GetTransform(root);
        if (transform != null)
        {
            m = Matrix.Multiply(m, transform.Value);
        }

        // Convert from “device-independent pixels” into pixels.
        var offset = VisualTreeHelper.GetOffset(root);
        m.Translate(offset.X, offset.Y);
        var pointClient = m.Transform(pointRoot);
        pointClient = hwndSource.CompositionTarget.TransformToDevice.Transform(pointClient);

        var 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.
        var pointScreenPixels = pointClientPixels;

        if (ClientToScreen(
            hwndSource.Handle,
            ref pointScreenPixels))
        {
            return new Point(
                pointScreenPixels.X,
                pointScreenPixels.Y);
        }
        else
        {
            return new Point();
        }
    }
[StructLayout(LayoutKind.Sequential)]
私有结构点
{
公共int X;
公共智力;
}
/// 
///ClientToScreen函数将指定点的客户端区域坐标转换为屏幕坐标。
/// 
///其客户端区域用于转换的窗口的句柄。
///指向包含要转换的客户端坐标的点结构的指针。如果函数成功,新的屏幕坐标将复制到此结构中。
///如果函数成功,则返回值为非零。
[DllImport(“User32”,EntryPoint=“ClientToScreen”,SetLastError=true,ExactSpelling=true,CharSet=CharSet.Auto)]
[返回:Marshallas(UnmanagedType.Bool)]
专用静态外部布尔客户端到屏幕(
IntPtr hWnd,
参考点(pt);
/// 
///获取WPF中屏幕相对位置的代码
/// 
///要传输的源点
///用作参考的可视对象
///通过转换获得的屏幕相对点
/// 
/// http://blogs.msdn.com/llobo/archive/2006/05/02/Code-for-getting-screen-relative-Position-in-WPF.aspx
///我们在论坛上看到的常见客户查询之一
///是获取屏幕上某一点的相对位置。
///目前,我们没有提供允许此功能的API。
///然而,Nick Kramer在论坛上提出了这个代码
///它对LTR(从左到右)系统非常有效。
///下面是获取屏幕相对位置的代码。
/// 
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand,Unrestricted=true)]
公共静态点?转换屏幕(
点,点,
视觉相对论
{
//将点从视觉坐标系转换为根坐标系。
var hwndSource=PresentationSource.FromVisual(relativeTo)作为hwndSource;
if(hwndSource==null)
返回null;
var root=hwndSource.RootVisual;
//将点从根坐标转换为客户坐标。
var transformToRoot=相对于变压器电容器(根);
var pointRoot=transformToRoot.Transform(点);
var m=矩阵恒等式;
var transform=VisualTreeHelper.GetTransform(根);
如果(转换!=null)
{
m=矩阵乘法(m,变换值);
}
//将“设备独立像素”转换为像素。
var offset=visualtreeheloper.GetOffset(根);
m、 平移(偏移量X,偏移量Y);
var pointClient=m.Transform(pointRoot);
pointClient=hwndSource.CompositionTarget.TransformToDevice.Transform(pointClient);
var pointClientPixels=新点();
pointClientPixels.X=(0
另外,如果您打算全部使用“我更改了DPI”,我会问您的应用程序是什么?您的“DPI感知”是什么意思?默认情况下WPF DPI不知道吗?