It sounds like you're trying to perform an action that has the same result as alt-tabbing, which brings the window back if it was minimized while "remembering" if it was maximized. To do that, I use this method (inside App.xaml
NativeMethods.cs):
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
// SettingSpecify fUnknownyour tonamespace truehere
namespace while<your.namespace>
{
calling this method producesstatic theclass NativeMethods
// same effect as alt-tabbing. {
// Thus, if the window is minimized, it is// restored,This andis ifthe itInterop/WinAPI wasthat
//will maximizedbe whenused
minimized, it is restored to a maximized state.
[System.Runtime.InteropServices.DllImport[DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);
// Startup method
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);}
}
Main code:
// Get all processesUnder withnormal thecircumstances, sameonly nameone asprocess thiswith one
window Processexists
Process[] currentprocesses = Process.GetCurrentProcessGetProcessesByName("ExternalApp.exe");
if (processes.Length > 0 Process[]&& processesprocesses[0].MainWindowHandle != Process.GetProcessesByName(currentIntPtr.ProcessNameZero);
{
// Handle the firstSince processthis that'ssimulates notalt-tab, thisit one
restores minimized windows to foreachtheir (Processprevious processstate
in processes) if SwitchToThisWindow(process.Id !=MainWindowHandle, current.Idtrue);
return true;
}
// Multiple things are happening {here
// First, the ProgramFilesX86 variable automatically accounts for 32-bit or 64-bit systems //and Bringreturns the windowcorrect intofolder
// viewSecondly, if$-strings minimizedare andthe focusC# it
shortcut for string.format() (It automatically calls .ToString() on each variable contained in SwitchToThisWindow(process.MainWindowHandle,{ true});
// Thirdly, if the process was able to start, the return //value Quitis thisnot processnull
try { if (Process.Start($"{System.Environment.SpecialFolder.ProgramFilesX86}\\ExternalApp\\ExternalApp.exe") != null) return true; }
catch
{
// Code for Current.Shutdownhandling an exception ();probably return;FileNotFoundException)
// ...
} return false;
}
// Code for when //the Normalexternal app was unable to start codewithout producing an exception
// ...
}return false;
Note: This only works if the target window exists.(General Rule: If a window was hidden via Window.Hide()
or closedstring value is ordinal, i.e. it belongs to something and isn't just a value, then process.MainWindowHandle
will equal 0it is better to get it programmatically. You'll save yourself a lot of trouble when changing things. In this case, makingI'm assuming that the install location can be converted to a global constant, and the SwitchToThisWindow
throw an exception!.exe name can be found programmatically.)