I am trying to open the MainWindow from the system tray if the application is already running and the user tried to run a new instance of the program (single instance)
I was following this tutorial : https://www.codeproject.com/Articles/32908/C-Single-Instance-App-With-the-Ability-To-Restore
It worked perfectly if the MainWindow is minimized , However, when I close the window which I am just hiding, and re-open it from the newly created instance by passing a message (using the main window handler), I get the window opened but it's a black screen.
Closing MainWindow:
private void main_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
// If the window is visible, then hide it
if (IsVisible)
Visibility = Visibility.Hidden;
e.Cancel = true;
}
Showing MainWindow using MainWinow handler
public const int SW_SHOWNORMAL = 1;
[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static void ShowToFront(IntPtr window) {
ShowWindow(window, SW_SHOWNORMAL);
SetForegroundWindow(window);
}
How I call it in my MainWindow.xaml.cs
public void ShowWindow() {
WinApi.ShowToFront(new WindowInteropHelper(this).Handle);
}
I was able to have the normal window by replacing
public void ShowWindow() {
WinApi.ShowToFront(new WindowInteropHelper(this).Handle);
}
with
public void ShowWindow() {
Visibility = Visibility.Visible;
WindowState = WindowState.Normal;
}
Howerver, I am still wondering why the Handler opened my MainWindow with a black screen ?