windows-store-appscommand-line-argumentswindows-app-sdk

Start a packaged Windows app on system start with minimized or hidden window


I have developed a packaged desktop Windows App SDK app that has the option run on system startup using StartupTask. This works well, but now I also want the option to start with the window in the minimized or hidden state. In the unpackaged version of the same app, I simply create a shortcut in the Startup folder with commandline arguments, which I can handle and set the window state. How do I do the same for the packaged version? Is there a way to pass in arguments at startup?

I have tried creating shortcuts to start the packaged version of the app like in this question but this doesn't seem to be the proper way to achieve this.


Solution

  • You could do this in your app via the OverlappedPresenter Class. When you detected the app is launched via StartUp task, then use OverlappedPresenter to minimize the app.

    You could try to use the code here:

         protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
     {
    
         m_window = new MainWindow();
         m_window.Activate();
    
    
         AppActivationArguments appActivationArguments = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
    
         if (appActivationArguments.Kind is ExtendedActivationKind.StartupTask )
         {
             // Retrieve the window handle (HWND) of the current (XAML) WinUI 3 window.
             IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);
    
             // Retrieve the WindowId that corresponds to hWnd.
             WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
    
             // Lastly, retrieve the AppWindow for the current (XAML) WinUI 3 window.
             AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
    
             if (appWindow.Presenter is OverlappedPresenter presenter)
             {
                 presenter.Minimize();
             }
         }
     }