'How do I restore a minimized(hided) wpf app to foreground [duplicate]
I have a wpf application on .net 4.8. If i close the app i use MainWindow.Hide() to bring it in Systemtray and to minimize the app. But if a user starts a new instance of the app it should be bring to foreground the minimized app. For this I use this pice of code:
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
var runningProcesses = Process.GetProcesses().FirstOrDefault(p => p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id);
if (runningProcesses != null)
{
var window = runningProcesses.MainWindowHandle;
ShowWindowAsync(window, 1); //SW_SHOWNORMAL
SetForegroundWindow(window);
this.Close();
}
If the app is only minimized it works perfekt. If it is hided (Systemtray) the MainWindowHandle is always 0. I have noticed that if I hide the app it goes from apps section to the background process section in the taskmanager. Is there a way to avoid this action or is there a way to bring it in foreground? Thanks
Solution 1:[1]
you can use FindWindow api to find MainWindow handle. then you can bring it to foreground.
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
var mainWindowHandle = FindWindow("","MainWindow"); //Suppose your window name is MainWindow
ShowWindowAsync(mainWindowHandle , 1); //SW_SHOWNORMAL
SetForegroundWindow(mainWindowHandle );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
