【问题标题】:How do I restore a WPF application from tray icon instead of turning it on by clicking a shortcut on the desktop?如何从托盘图标恢复 WPF 应用程序,而不是通过单击桌面上的快捷方式来打开它?
【发布时间】:2014-10-30 13:01:44
【问题描述】:

考虑:

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

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

【问题讨论】:

    标签: c#


    【解决方案1】:

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

    激活窗口:myWindowToFront.Activate();

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 2011-06-30
      相关资源
      最近更新 更多