UWP中的GetWindowRect

UWP中的GetWindowRect,uwp,dllimport,Uwp,Dllimport,由于添加了AppDiagnostics权限,我认为应该可以获得当前前台窗口的大小。以下是我已经尝试过的: async private void DoThing() { Rect angle = GetRectValues(); reportString += angle.Y + " "; reportString += angle.X + " "; reportString += angle.Height + "

由于添加了AppDiagnostics权限,我认为应该可以获得当前前台窗口的大小。以下是我已经尝试过的:

    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);
它运行并返回值。问题是,这些值非常奇怪

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

这些是在我的屏幕中间运行的浏览器窗口的值。当我移动窗口时,它们会改变,但我不知道如何解释它们。此外,如果窗口是全屏的,它们将变为NaN。除非我将它们转换为整数,否则在这种情况下,值将转换为

-2147483648-2147483648 0
无论出于何种原因


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

在UWP中,我们可以使用
ApplicationView.GetForCurrentView().VisibleBounds
在主窗口中获取VisibleBounds

我们可以将许多DLL称为Win32应用程序


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

您对
Rect
的结构定义必须不正确。从字符串表示中,我可以看出它的成员当前是双精度的。如果您查看MSDN

定义如下:

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

我认为OP想要得到系统当前前景窗口的大小,这是一个他们不拥有的窗口。它可以是经典窗口或UWP窗口。调用VisibleBounds在这里不会起作用。@StefanWickMSFT是的,没错。也就是说,修复Rect定义就成功了!仅供参考,Windows应用商店目前不允许使用此API,因此即使您开始工作,也将仅限于侧面加载或企业安装。还要注意,大多数重要的
用户
API都会失败。