如何从托盘图标还原WPF应用程序,而不是通过单击桌面上的快捷方式将其打开?

时间:2014-10-30 13:01:45

标签: c#

考虑:

public partial class App : Application
{
    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    private Mutex myMutex;

    protected override void OnStartup(StartupEventArgs theArgs)
    {
        bool aIsNewInstance = false;
        myMutex = new Mutex(true, "Testing", out aIsNewInstance);
        if (!aIsNewInstance)
        {
            App.Current.Shutdown();
            Process current = Process.GetCurrentProcess();
            foreach (Process process in Process.GetProcessesByName(current.ProcessName))
            {
                if (process.Id != current.Id)
                {
                    IntPtr pointer = FindWindow(null, "Testing");
                    App.Current.Shutdown();
                    ShowWindow(pointer, 1);
                    break;
                }
            }
        }

当我双击图标时,如何阻止启动应用程序的第二个副本,而是让已经运行且最小化的托盘应用程序显示窗口?

1 个答案:

答案 0 :(得分:0)

当用户点击您的托盘图标时,只需激活您的窗口。

激活窗口:myWindowToFront.Activate();

WPF-Tray-Icon:WPF NotifyIcon(非常棒的东西)