单击wpf中的桌面快捷方式时,从系统托盘还原应用程序

本文关键字:系统 应用程序 还原 wpf 桌面 快捷方式 单击 | 更新日期: 2023-09-27 18:28:19

#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;
        }
    }

单击wpf中的桌面快捷方式时,从系统托盘还原应用程序

您可以使用 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;
}