Skip to main content
Better implementation
Source Link

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.)

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.cs):

using System.Diagnostics;

// Setting fUnknown to true while calling this method produces the 
// same effect as alt-tabbing. 
// Thus, if the window is minimized, it is restored, and if it was 
// maximized when minimized, it is restored to a maximized state.
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);

// Startup method
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // Get all processes with the same name as this one
    Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName(current.ProcessName);

    // Handle the first process that's not this one
    foreach (Process process in processes) if (process.Id != current.Id)
        {
            // Bring the window into view if minimized and focus it
            SwitchToThisWindow(process.MainWindowHandle, true);
            // Quit this process
            Current.Shutdown(); return;
        }

    // Normal start code...
}

Note: This only works if the target window exists. If a window was hidden via Window.Hide() or closed, then process.MainWindowHandle will equal 0, making SwitchToThisWindow throw an exception!

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.

NativeMethods.cs:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

// Specify your namespace here
namespace <your.namespace>
{
    static class NativeMethods
    {
        // This is the Interop/WinAPI that will be used
        [DllImport("user32.dll")]
        static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);
    }
}

Main code:

// Under normal circumstances, only one process with one window exists
Process[] processes = Process.GetProcessesByName("ExternalApp.exe");
if (processes.Length > 0 && processes[0].MainWindowHandle != IntPtr.Zero)
{
    // Since this simulates alt-tab, it restores minimized windows to their previous state
    SwitchToThisWindow(process.MainWindowHandle, true);
    return true;
}
// Multiple things are happening here
// First, the ProgramFilesX86 variable automatically accounts for 32-bit or 64-bit systems and returns the correct folder
// Secondly, $-strings are the C# shortcut for string.format() (It automatically calls .ToString() on each variable contained in { })
// Thirdly, if the process was able to start, the return value is not null
try { if (Process.Start($"{System.Environment.SpecialFolder.ProgramFilesX86}\\ExternalApp\\ExternalApp.exe") != null) return true; }
catch
{
    // Code for handling an exception (probably FileNotFoundException)
    // ...
    return false;
}
// Code for when the external app was unable to start without producing an exception
// ...
return false;

(General Rule: If a string value is ordinal, i.e. it belongs to something and isn't just a value, then it is better to get it programmatically. You'll save yourself a lot of trouble when changing things. In this case, I'm assuming that the install location can be converted to a global constant, and the .exe name can be found programmatically.)

deleted 12 characters in body
Source Link

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.cs):

using System.Diagnostics;

// IfSetting fUnknown isto true, while calling thethis method produces the 
// same effect as alt-tabbing. 
// Thus, if the window is minimized, it is restored
// If it was minimized, whileand maximized,if it is restoredwas maximized
// If it is behindmaximized otherwhen windowsminimized, it is sentrestored to thea frontmaximized state.
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);

// Startup method
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e); //part of the override

    // Get all processes with the same name as this one
    Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName(current.ProcessName);

    // SkipHandle handlingthe forfirst process that's not this processone
    foreach (Process process in processes) if (process.Id != current.Id)
        {
            // Bring the window into view if minimized and focus it
            SwitchToThisWindow(process.MainWindowHandle, true);
            // Quit thethis applicationprocess
            Current.Shutdown(); return;
        }

    // Normal start code...
}

I hope this provides a much simpler solution.

Note: This only works if the target window exists. If a window was hidden via Window.Hide() or closed, then process.MainWindowHandle will equal 0, making SwitchToThisWindow throw an exception!

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.cs):

using System.Diagnostics;

// If fUnknown is true, calling the method produces the same effect as alt-tabbing
// Thus, if the window is minimized, it is restored
// If it was minimized while maximized, it is restored maximized
// If it is behind other windows, it is sent to the front
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);

// Startup method
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e); //part of the override

    // Get all processes with the same name as this one
    Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName(current.ProcessName);

    // Skip handling for this process
    foreach (Process process in processes) if (process.Id != current.Id)
        {
            // Bring the window into view if minimized and focus it
            SwitchToThisWindow(process.MainWindowHandle, true);
            // Quit the application
            Current.Shutdown(); return;
        }

    // Normal start code...
}

I hope this provides a much simpler solution.

Note: This only works if the target window exists. If a window was hidden via Window.Hide() or closed, then process.MainWindowHandle will equal 0, making SwitchToThisWindow throw an exception!

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.cs):

using System.Diagnostics;

// Setting fUnknown to true while calling this method produces the 
// same effect as alt-tabbing. 
// Thus, if the window is minimized, it is restored, and if it was 
// maximized when minimized, it is restored to a maximized state.
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);

// Startup method
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // Get all processes with the same name as this one
    Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName(current.ProcessName);

    // Handle the first process that's not this one
    foreach (Process process in processes) if (process.Id != current.Id)
        {
            // Bring the window into view if minimized and focus it
            SwitchToThisWindow(process.MainWindowHandle, true);
            // Quit this process
            Current.Shutdown(); return;
        }

    // Normal start code...
}

I hope this provides a much simpler solution.

Note: This only works if the target window exists. If a window was hidden via Window.Hide() or closed, then process.MainWindowHandle will equal 0, making SwitchToThisWindow throw an exception!

Source Link

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.cs):

using System.Diagnostics;

// If fUnknown is true, calling the method produces the same effect as alt-tabbing
// Thus, if the window is minimized, it is restored
// If it was minimized while maximized, it is restored maximized
// If it is behind other windows, it is sent to the front
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);

// Startup method
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e); //part of the override

    // Get all processes with the same name as this one
    Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName(current.ProcessName);

    // Skip handling for this process
    foreach (Process process in processes) if (process.Id != current.Id)
        {
            // Bring the window into view if minimized and focus it
            SwitchToThisWindow(process.MainWindowHandle, true);
            // Quit the application
            Current.Shutdown(); return;
        }

    // Normal start code...
}

I hope this provides a much simpler solution.

Note: This only works if the target window exists. If a window was hidden via Window.Hide() or closed, then process.MainWindowHandle will equal 0, making SwitchToThisWindow throw an exception!