Rizki Pratama
Rizki Pratama

Reputation: 571

C# WPF window not positioned on the center when it's being shown from hidden state on the application startup

I've built a C# WPF application where:

My window is only one, that is, MainWindow, where on its XAML I set

WindowStartupLocation="CenterScreen"

to make the window positioned at the center of the screen.

All is great. At normal condition without the argument, the application is centered.

...However, not for this one: when I run the app with argument --minimize-to-tray and double-click the sys tray icon to reveal the window, it is not centered.


Here is my code to accept arguments and its associated actions:

...

/// <summary>
/// MainWindow constructor
/// <summary>
public MainWindow()
{
    InitializeComponent();

    InitializeAppArguments();

    ...
}

/// <summary>
/// Initialize actions that are associated with application's arguments.
/// </summary>
public void InitializeAppArguments()
{
    string[] args = Environment.GetCommandLineArgs();

    // Minimize window on app startup to tray 
    // if user put first argument "--minimize-to-tray" on the app
    //
    if (args.Length >= 2)
    {
        if (args[1] == "--minimize-to-tray")
        {
            this.WindowState = WindowState.Minimized;
            this.Hide();

            this.StateChanged += MainWindow_StateChanged;
        }
    }
}

...

/// <summary>
/// Hide app's window from taskbar whenever user minimize the app window.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void MainWindow_StateChanged(object sender, EventArgs e)
{
    // When app's window is minimized, hide it from taskbar
    //
    if (this.WindowState == WindowState.Minimized)
    {
        this.Hide();
    }
}


And event handler on system tray icon's double click or click on "Open" context menu of the icon:

/// <summary>
/// Open/show application when user click on "Open" context menu on application's system tray icon.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void MenuOpen_Click__SysTrayIcon_DoubleClick(object sender, EventArgs e)
{
    this.Show();
    this.Focus();

    this.WindowState = WindowState.Normal;
}


So, why is the window not centered when it's being shown from hidden on the application startup? Am I doing something wrong here?

Any help would be appreciated.

Upvotes: 3

Views: 1558

Answers (1)

Rizki Pratama
Rizki Pratama

Reputation: 571

According to this solution:

It is necessary to set WindowState to Normal before setting window location.


Apparently, it also works to prevent the window appears uncentered. My mistake is that before I hide the window, I set the WindowState to Minimized, which makes the window uncentered.

So, to solve that, I change the line this.WindowState = WindowState.Minimized; to this.WindowState = WindowState.Normal;.

public void InitializeAppArguments()
{
    string[] args = Environment.GetCommandLineArgs();

    // Minimize window on app startup to tray 
    // if user put first argument "--minimize-to-tray" on the app
    //
    if (args.Length >= 2)
    {
        if (args[1] == "--minimize-to-tray")
        {
            this.WindowState = WindowState.Normal; // Fixed the problem
            this.Hide();

            this.StateChanged += MainWindow_StateChanged;
        }
    }
}

Upvotes: 4

Related Questions