UWP 中的 GetWindowRect

GetWindowRect in UWP

既然添加了AppDiagnostics权限,我想应该可以获取当前在前台的window的大小。这是我已经尝试过的:

    async private void DoThing()
    {
        Rect angle = GetRectValues();

        reportString += angle.Y + " ";
        reportString += angle.X + " ";
        reportString += angle.Height + " ";
        reportString += angle.Width;
    }

    private unsafe Rect GetRectValues()
    {

        IntPtr hWID = GetForegroundWindow();

        Rect angle;

        Rect* pAngle = ∠

        GetWindowRect(GetForegroundWindow(), pAngle);

        return angle;
    }

    [DllImport("user32.dll", CharSet = CharSet.Ansi)]
    private static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", CharSet = CharSet.Ansi)]
    private unsafe static extern Boolean GetWindowRect(IntPtr intPtr, Rect* lpRect);

它运行,还有 returns 值。问题是,这些值很奇怪。

3.47522019152555E-43 1.50919844607783E-42 1.60448674165192E-42 3.50885135466934E-42 

这些是我屏幕中间的浏览器 window 运行 的值。当我移动 window 时它们会改变,但我不知道如何解释它们。此外,如果 window 全屏,它们将更改为 NaN。除非我将它们转换为整数,在这种情况下,无论出于何种原因,这些值都会变成 -2147483648 -2147483648 0 0

如何正确使用此方法,如何解释这些值?

在 UWP 中,我们可以使用 ApplicationView.GetForCurrentView().VisibleBounds 来获取主要 windows 中的 VisibleBounds。

我们可以调用很多dll作为Win32应用程序。

我们可以使用Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds获取应用程序的边框window。获取当前 windows 的另一种方法是使用 Window.Current.Bounds 获取边界。

您对 Rect 的结构定义一定不正确。从字符串表示中,我可以看出它的成员目前是双打。如果您查看 MSDN,RECT structure only has integers in it.

定义如下:

struct Rect
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}