UWP中的GetWindowRect

时间:2018-11-19 21:30:39

标签: uwp dllimport

由于已添加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 0

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

2 个答案:

答案 0 :(得分:1)

您对Rect的结构定义必须不正确。从字符串表示形式,我可以看出其成员当前是双精度型。如果您使用的是MSDN,RECT structure only has integers in it

这样定义它:

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

答案 1 :(得分:0)

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

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

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