c# 在一个程序里唤醒另一个程序(最小化时显示在前面)

 

方式一:

 

[DllImport("user32.dll ")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        //根据任务栏应用程序显示的名称找相应窗口的句柄
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        private const int SW_RESTORE = 9;
        private void OpenSerialPortUtility(object sender, EventArgs e)
        {  
            //遍历进程列表查找目标程序是否运行运行则前置否则启动
            Process[] pList = Process.GetProcesses(".");
            bool b = false;
            foreach (Process p in pList)
            {
                if (p.ProcessName == "wpfYourSystem")
                {
                    b = true; 
                    ShowWindow(p.MainWindowHandle, SW_RESTORE); //将窗口还原,如果不用此方法,缩小的窗口不能激活                    
                    break;
                }
            }
            if (b == false)
            {
                System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\9.18测试\医生端\Debug\wpfYourSystem.exe");
            }
        } 

方式二(只是唤醒程序):

 [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
      


 string pName = Path.GetFileNameWithoutExtension(Application.ExecutablePath);//要启动的进程名称,可以在任务管理器里查看,一般是不带.exe后缀的;
                Process[] temp = Process.GetProcessesByName(pName);//在所有已启动的进程中查找需要的进程;
                if (temp.Length > 0)//如果查找到
                {
                    IntPtr handle = temp[0].MainWindowHandle;
                    SwitchToThisWindow(handle, true); // 激活,显示在最前
                }

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值