38

I want that my window is completely hidden on the startup. No window, no entry in the taskbar. The user doesn't see, the application is started.

How can I realize that?

Thank you!

rakete
  • 2,953
  • 11
  • 54
  • 108
  • 1
    Will he be able to open the window later? – Daniel Hilgarth Jul 14 '11 at 09:08
  • 1
    Out of interest, what problem does this actually solve? You may need to think about using os mutexes to prevent multiple instances of your app starting. A user might think that they mis-clicked if nothing opens... – LukeN Jul 14 '11 at 09:39
  • 2
    @LukeN: One valid use case is using your GUI application as a command-line program (in which case you want it to execute the command in the background without popping up, then exit). Sometimes it's not viable to make two versions of an application just to have rarely used command-line support. – Cameron Feb 13 '15 at 04:25
  • @LukeN Silent printing implementation would be another use case. – Eternal21 Sep 11 '15 at 13:54

5 Answers5

50

An alternative to H.B.'s method is just to set the Visibility to hidden and set ShowInTaskbar to false. This still creates the window and lets it do its thing.

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" ShowInTaskbar="False" Visibility="Hidden">
    <Grid>

    </Grid>
</Window>
LukeN
  • 1,698
  • 15
  • 29
  • 3
    Are you sure of your statement ? I'm currently trying to use your trick and it seems that the `Load` event of `MainWindow` is never raised if Visibility is `Hidden`. – Emrys Myrooin Jul 06 '16 at 07:39
  • 3
    @EmrysMyrooin Load event is fired when the window is first seen, since it is never seen it is never fired. Try using the initialized event instead if you need to execute some stuff after the window is constructed. – Wobbles Aug 16 '16 at 11:36
  • This didn't work for me. Was easier to use @H.B. 's answer. IE don't create and show a window if I don't want one. – Chuck Savage Nov 28 '17 at 23:22
  • 3
    In my case i could not switch the visibility from `Hidden` to `Visible`, so i use `ShowInTaskbar="False"` + `WindowState="Minimized"`. then i could change them in code easily – Muhammad Sulaiman Sep 17 '20 at 00:34
45

Don't show the window. By default there is a StartupUri defined in the App.xaml, remove it and override the OnStartup method in the code-behind to create a window, just Show and Hide it as you wish.

H.B.
  • 166,899
  • 29
  • 327
  • 400
21

Simply don't create a window, just delete the StartupUri from App.xaml.

It might be helpful to set the Application to ShutDownMode="OnExplicitShutdown" this will prevent that your application shuts down if your last window was closed.

Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66
  • 3
    I don't think that you are actually required to set ShutDownMode to OnExplicitShutdown. I think this is only needed if he doesn't want the app to shutdown when he finally does show a window, and then closes it – LukeN Jul 14 '11 at 09:32
  • 3
    Depending on the application he wants to create it might be useful, otherwise the app will close as soon as the last window is closed as you said. This will give him explicit control over the Application life cycle. But I'll edit my answer a bit to reflect that it is not strictly necessary. – Leonard Brünings Jul 14 '11 at 09:36
0

My requirement: Start a process to show a window, and embed it into a wpf control. The window must be loaded normally, trigger initialized/loaded events, then run as child window in control.

My solution: Set the window width and height to 1, after loaded, resize it to normal size. The window will be shown in short time, almost 1 second. User will not notice it.

Jerry Jian
  • 26
  • 3
0

If a window should be created but not displayed, just don't show it. Set '-s' command line argument to run in silent mode.

    protected override void OnStartup(StartupEventArgs e)
    {
        // Get the command line arguments
        string[] args = Environment.GetCommandLineArgs();
        var silentMode = args.Count() > 1 && args[1].ToLower().Equals("-s");

        // If silent mode is enabled, the window is not displayed
        var mainWindow = new MainWindow();
        if (!silentMode) mainWindow.Show();
    }
Alex Soloh
  • 61
  • 3