GetWindowRect in UWP












0















Since AppDiagnostics permissions have been added, I think it should be possible to get the size of the window currently in the foreground. Here is what I have already tried:



    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);


It runs, and also returns values. The problem is, these values are pretty weird.



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


These are the values for an explorer window running in the middle of my screen. They change when I move the window, but I don't know how to interprete them. Also, they change to NaN if the window is fullscreen. Unless I cast them to integers, in which case the values turn into -2147483648 -2147483648 0 0
for whatever reason.



How do I use this method correctly, and how do I interprete these values?










share|improve this question























  • Just FYI, the Windows Store doesn't currently allow this API so even if you get if go to work, you will be limited to side loading or enterprise installs. Also note that most of the important User APIs will fail.

    – Peter Torr - MSFT
    Nov 21 '18 at 4:34
















0















Since AppDiagnostics permissions have been added, I think it should be possible to get the size of the window currently in the foreground. Here is what I have already tried:



    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);


It runs, and also returns values. The problem is, these values are pretty weird.



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


These are the values for an explorer window running in the middle of my screen. They change when I move the window, but I don't know how to interprete them. Also, they change to NaN if the window is fullscreen. Unless I cast them to integers, in which case the values turn into -2147483648 -2147483648 0 0
for whatever reason.



How do I use this method correctly, and how do I interprete these values?










share|improve this question























  • Just FYI, the Windows Store doesn't currently allow this API so even if you get if go to work, you will be limited to side loading or enterprise installs. Also note that most of the important User APIs will fail.

    – Peter Torr - MSFT
    Nov 21 '18 at 4:34














0












0








0








Since AppDiagnostics permissions have been added, I think it should be possible to get the size of the window currently in the foreground. Here is what I have already tried:



    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);


It runs, and also returns values. The problem is, these values are pretty weird.



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


These are the values for an explorer window running in the middle of my screen. They change when I move the window, but I don't know how to interprete them. Also, they change to NaN if the window is fullscreen. Unless I cast them to integers, in which case the values turn into -2147483648 -2147483648 0 0
for whatever reason.



How do I use this method correctly, and how do I interprete these values?










share|improve this question














Since AppDiagnostics permissions have been added, I think it should be possible to get the size of the window currently in the foreground. Here is what I have already tried:



    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);


It runs, and also returns values. The problem is, these values are pretty weird.



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


These are the values for an explorer window running in the middle of my screen. They change when I move the window, but I don't know how to interprete them. Also, they change to NaN if the window is fullscreen. Unless I cast them to integers, in which case the values turn into -2147483648 -2147483648 0 0
for whatever reason.



How do I use this method correctly, and how do I interprete these values?







uwp dllimport






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 21:30









JonnyLeeJonnyLee

306




306













  • Just FYI, the Windows Store doesn't currently allow this API so even if you get if go to work, you will be limited to side loading or enterprise installs. Also note that most of the important User APIs will fail.

    – Peter Torr - MSFT
    Nov 21 '18 at 4:34



















  • Just FYI, the Windows Store doesn't currently allow this API so even if you get if go to work, you will be limited to side loading or enterprise installs. Also note that most of the important User APIs will fail.

    – Peter Torr - MSFT
    Nov 21 '18 at 4:34

















Just FYI, the Windows Store doesn't currently allow this API so even if you get if go to work, you will be limited to side loading or enterprise installs. Also note that most of the important User APIs will fail.

– Peter Torr - MSFT
Nov 21 '18 at 4:34





Just FYI, the Windows Store doesn't currently allow this API so even if you get if go to work, you will be limited to side loading or enterprise installs. Also note that most of the important User APIs will fail.

– Peter Torr - MSFT
Nov 21 '18 at 4:34












2 Answers
2






active

oldest

votes


















1














Your struct definition for Rect must be incorrect. From the string representation, I can tell its members currently are doubles. If you look on MSDN, RECT structure only has integers in it.



Define it like this:



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





share|improve this answer































    0














    In UWP we can use ApplicationView.GetForCurrentView().VisibleBounds to get the VisibleBounds in the main windows.



    We can call many dlls as the Win32 application.



    We can use Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds to get the bounding rectangle of the app window. The other way to get current windows is use Window.Current.Bounds to get the bounds.






    share|improve this answer
























    • I think the OP wants to get the size of the current foreground window of the system, which is a window they don't own. It could be a classic window or a UWP window. Calling VisibleBounds won't do the trick here.

      – Stefan Wick MSFT
      Nov 20 '18 at 4:39











    • @StefanWickMSFT Yes, exactly. That said, fixing the Rect definition did the trick!

      – JonnyLee
      Nov 20 '18 at 20:24











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382923%2fgetwindowrect-in-uwp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Your struct definition for Rect must be incorrect. From the string representation, I can tell its members currently are doubles. If you look on MSDN, RECT structure only has integers in it.



    Define it like this:



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





    share|improve this answer




























      1














      Your struct definition for Rect must be incorrect. From the string representation, I can tell its members currently are doubles. If you look on MSDN, RECT structure only has integers in it.



      Define it like this:



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





      share|improve this answer


























        1












        1








        1







        Your struct definition for Rect must be incorrect. From the string representation, I can tell its members currently are doubles. If you look on MSDN, RECT structure only has integers in it.



        Define it like this:



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





        share|improve this answer













        Your struct definition for Rect must be incorrect. From the string representation, I can tell its members currently are doubles. If you look on MSDN, RECT structure only has integers in it.



        Define it like this:



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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 7:07









        SuniusSunius

        1,161820




        1,161820

























            0














            In UWP we can use ApplicationView.GetForCurrentView().VisibleBounds to get the VisibleBounds in the main windows.



            We can call many dlls as the Win32 application.



            We can use Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds to get the bounding rectangle of the app window. The other way to get current windows is use Window.Current.Bounds to get the bounds.






            share|improve this answer
























            • I think the OP wants to get the size of the current foreground window of the system, which is a window they don't own. It could be a classic window or a UWP window. Calling VisibleBounds won't do the trick here.

              – Stefan Wick MSFT
              Nov 20 '18 at 4:39











            • @StefanWickMSFT Yes, exactly. That said, fixing the Rect definition did the trick!

              – JonnyLee
              Nov 20 '18 at 20:24
















            0














            In UWP we can use ApplicationView.GetForCurrentView().VisibleBounds to get the VisibleBounds in the main windows.



            We can call many dlls as the Win32 application.



            We can use Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds to get the bounding rectangle of the app window. The other way to get current windows is use Window.Current.Bounds to get the bounds.






            share|improve this answer
























            • I think the OP wants to get the size of the current foreground window of the system, which is a window they don't own. It could be a classic window or a UWP window. Calling VisibleBounds won't do the trick here.

              – Stefan Wick MSFT
              Nov 20 '18 at 4:39











            • @StefanWickMSFT Yes, exactly. That said, fixing the Rect definition did the trick!

              – JonnyLee
              Nov 20 '18 at 20:24














            0












            0








            0







            In UWP we can use ApplicationView.GetForCurrentView().VisibleBounds to get the VisibleBounds in the main windows.



            We can call many dlls as the Win32 application.



            We can use Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds to get the bounding rectangle of the app window. The other way to get current windows is use Window.Current.Bounds to get the bounds.






            share|improve this answer













            In UWP we can use ApplicationView.GetForCurrentView().VisibleBounds to get the VisibleBounds in the main windows.



            We can call many dlls as the Win32 application.



            We can use Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds to get the bounding rectangle of the app window. The other way to get current windows is use Window.Current.Bounds to get the bounds.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 1:25









            lindexilindexi

            2,2121731




            2,2121731













            • I think the OP wants to get the size of the current foreground window of the system, which is a window they don't own. It could be a classic window or a UWP window. Calling VisibleBounds won't do the trick here.

              – Stefan Wick MSFT
              Nov 20 '18 at 4:39











            • @StefanWickMSFT Yes, exactly. That said, fixing the Rect definition did the trick!

              – JonnyLee
              Nov 20 '18 at 20:24



















            • I think the OP wants to get the size of the current foreground window of the system, which is a window they don't own. It could be a classic window or a UWP window. Calling VisibleBounds won't do the trick here.

              – Stefan Wick MSFT
              Nov 20 '18 at 4:39











            • @StefanWickMSFT Yes, exactly. That said, fixing the Rect definition did the trick!

              – JonnyLee
              Nov 20 '18 at 20:24

















            I think the OP wants to get the size of the current foreground window of the system, which is a window they don't own. It could be a classic window or a UWP window. Calling VisibleBounds won't do the trick here.

            – Stefan Wick MSFT
            Nov 20 '18 at 4:39





            I think the OP wants to get the size of the current foreground window of the system, which is a window they don't own. It could be a classic window or a UWP window. Calling VisibleBounds won't do the trick here.

            – Stefan Wick MSFT
            Nov 20 '18 at 4:39













            @StefanWickMSFT Yes, exactly. That said, fixing the Rect definition did the trick!

            – JonnyLee
            Nov 20 '18 at 20:24





            @StefanWickMSFT Yes, exactly. That said, fixing the Rect definition did the trick!

            – JonnyLee
            Nov 20 '18 at 20:24


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382923%2fgetwindowrect-in-uwp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter