C# 程序隐藏在托盘,快捷方式启动Show出来。

22 篇文章 0 订阅

1、第一种情况: 最小化+托盘

    public App()
        {
            this.Startup += App_Startup;
        }
    private void App_Startup(object sender, StartupEventArgs e)
        {
            bool ret;
            mutex = new System.Threading.Mutex(true, "RsVPN", out ret);

            if (!ret)
            {
                int hWnd = FindWindow(null, "RsVPN");
                int id = -1;
                IntPtr p = new IntPtr(hWnd);
                GetWindowThreadProcessId(p, out id);
                ShowWindow(p, 9);
                SetForegroundWindow(p);
                Environment.Exit(-1);
            }


            //or
             Process current = Process.GetCurrentProcess();
            foreach (Process process in Process.GetProcessesByName(current.ProcessName))
            {
                string str = current.ProcessName + "::" + current.Id.ToString();

                if (process.Id != current.Id)
                {
                    int hWnd = FindWindow(null, "RsVPN");
                    IntPtr p = new IntPtr(hWnd);
                    ShowWindowAsync(p, WS_SHOWNORMAL); //显示 
                    SetForegroundWindow(p);//放到前端 

                    Environment.Exit(-1);
                }

            }


            //or

               Process[] p = Process.GetProcessesByName("RsVPN");
            if (p.Length > 0)
            {
                IntPtr handle = FindWindowByCaption(IntPtr.Zero, p[0].ProcessName);
                ShowWindowAsync(handle, 9);
                SetForegroundWindow(handle);
                Environment.Exit(-1);
            }


}

 

主窗口中处理

       private void button_close_Click(object sender, RoutedEventArgs e)
        {
             e.Handled = true;
            this.WindowState = WindowState.Minimized;
        }

1、第二种情况: 最小化 + 隐藏+托盘

主窗口中添加事件

 private void MainWindow_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Hide();
            }
            if (this.WindowState == WindowState.Normal)
            {
                this.Show();
            }
        }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值