0

I have imported SetWindowPos and GetForegroundWindow:

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


[DllImport("user32.dll", CharSet = CharSet.Ansi)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, UInt32 uFlags);

I have tried to use them, and while GetForegroundWindow does return a valid value, SetWindowPos does not do anything:

IntPtr hWID = GetForegroundWindow();
SetWindowPos(hWID, IntPtr.Zero, 50, 500, 800, 800, 0x0004);

Is there some capability I need to set? Or have I forgotten a permission?

It seems like the same block of code, inserted into a .NET project yields the wanted results. Is there a way to achieve this in UWP, or do I have to do this in .NET?

JonnyLee
  • 85
  • 7

1 Answers1

1

Most of the User APIs won't work for a UWP application, for various reasons. They are also not supported by the Store, so you would fail ingestion if you wanted to submit your app.

UWP has limited windowing support at the moment, although we're always looking to improve things (like enabling XAML controls inside of WPF apps).

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • Makes sense. Is there an easy way to check if an API is supported by UWP/The store other than trying it out? Some information on the documentation page would be great. Also, it seems like the right thing to do would be to either redo this app in .NET WPF, or use 'runFullTrust'. I assume that it has no difference for the store if I tried to zse runFullTrust vs use the Desktop bridge? And as I don't care if the main project is UWP or not, it would be simpler to do it in WPF and use Desktop bridge? – JonnyLee Nov 21 '18 at 06:43
  • @JonnyLee You can accomplish this task by adding a desktop extension to your UWP app package and making the SetWindowPos call from there. See quick tutorial here on the general: https://stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-1/ - Btw, better windowing support in UWP will not address this, since you are trying to operate on other app's windows (which may or may not be UWP windows). – Stefan Wick MSFT Nov 21 '18 at 16:30
  • The documentation should tell you if an API works for Desktop and / UWP if you look at the bottom of the page. – Peter Torr - MSFT Nov 21 '18 at 16:33