You need to call native code for that.
Add a reference to PInvoke.User32
package:
<PackageReference Include="PInvoke.User32" Version="0.7.104" Condition="$([MSBuild]::IsOSPlatform('windows'))"/>
Minimize
As an example upon a button click we minimize the window:
void MinimizeWindow(object sender, EventArgs e)
{
#if WINDOWS
var mauiWindow = App.Current.Windows.First();
var nativeWindow = mauiWindow.Handler.PlatformView;
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
PInvoke.User32.ShowWindow(windowHandle, PInvoke.User32.WindowShowStyle.SW_MINIMIZE);
#endif
}
Move
void MoveWindow(object sender, EventArgs e)
{
#if WINDOWS
var mauiWindow = App.Current.Windows.First();
var nativeWindow = mauiWindow.Handler.PlatformView;
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
PInvoke.RECT rect;
PInvoke.User32.GetWindowRect(windowHandle, out rect);
(var width, var height) = GetWinSize(rect);
PInvoke.User32.MoveWindow(windowHandle, 50, 0, width, height, true);
#endif
}
(int width, int height) GetWinSize(PInvoke.RECT rect) =>
(rect.right - rect.left, rect.bottom - rect.top);
It's possible to resize the window with MoveWindow()
, but since the aim in the question is to move the window only, then we supply the values of the current window's width and height as parameters.
Ps: Unfortunately I didn't find a simpler solution to get the window dimensions.
EDIT
A better alternative is to use AppWindow.Move(PointInt32):
#if WINDOWS
WindowId WindowId = Win32Interop.GetWindowIdFromWindow(windowHandle);
AppWindow appWindow = AppWindow.GetFromWindowId(WindowId);
appWindow.Move(new Windows.Graphics.PointInt32(x,y))
#endif
x
and y
are the coordinate for the desired new position. The origin (0,0) is the left upper corner of the screen.
For information about the screen dimensions: