简体   繁体   中英

Restore application from system tray when clicking on desktop shortcut in wpf

#region Already Instance is opened Validation

//MessageBox.Show("Notification Nexus instance is already running", "My application", MessageBoxButton.OK, MessageBoxImage.Warning);

    Process current = Process.GetCurrentProcess();
    foreach (Process process in Process.GetProcessesByName(current.ProcessName))
    {

        if (process.Id != current.Id)
        {
            SetForegroundWindow(process.MainWindowHandle);
            break;
        }
    }

You can use the WinAPI:

// SingleInstance.cs
static public class SingleInstance
{
    public const string ProgramGuid = "E2A5E185-7C2D-4A4A-97D2-D50378ADD2E3";

    public static readonly int WM_SHOWFIRSTINSTANCE = WinApi.RegisterWindowMessage("WM_SHOWFIRSTINSTANCE|{0}", ProgramGuid);
    static Mutex mutex;

    static public bool Start()
    {
        string mutexName = String.Format("Local\\{0}", ProgramGuid);
        bool onlyInstance;

        mutex = new Mutex(true, mutexName, out onlyInstance);

        return onlyInstance;
    }

    static public void ShowFirstInstance()
    {
        WinApi.PostMessage((IntPtr)WinApi.HWND_BROADCAST, WM_SHOWFIRSTINSTANCE, IntPtr.Zero, IntPtr.Zero);
    }

    static public void Stop()
    {
        mutex.ReleaseMutex();
    }
}    

// Main.cs
public static void Main()
{
    if (!SingleInstance.Start())
    {
        SingleInstance.ShowFirstInstance();
        return;
    }

    var app = new App();
    app.Run();

    SingleInstance.Stop();
}

// MainWindow.xaml.cs
protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);
    var source = PresentationSource.FromVisual(this) as HwndSource;
    source.AddHook(WndProc);
}

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == SingleInstance.WM_SHOWFIRSTINSTANCE)
    {
        this.Show();
        WinApi.ShowToFront(hwnd);
        handled = true;
    }

    return IntPtr.Zero;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM