C# 判断程序是否执行 进行启动或前台显示

     #region 显示程序
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);
        /// <summary> 
        /// 该函数设置由不同线程产生的窗口的显示状态。 
        /// </summary> 
        /// <param name="hWnd">窗口句柄</param> 
        /// <param name="cmdShow">指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分。</param> 
        /// <returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。</returns> 
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        /// <summary> 
        /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。 
        /// </summary> 
        /// <param name="hWnd">将被激活并被调入前台的窗口句柄。</param> 
        /// <returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零。</returns> 
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        private const int WS_SHOWNORMAL = 1;

        Process process = null;
        IntPtr appWin;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);


        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
        #endregion
        private void ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            foreach (Control c in pnlMain.Controls)
            {
                if (c.GetType().BaseType == typeof(Form))
                {
                    ((Form)c).Close();
                }
            }

            try
            {
                string[] arrStr = IniData.VideoUrl.pathvalue.Split('.');
                arrStr = arrStr[0].Split('\');
                string winName = arrStr[arrStr.Length - 1];
                if (!string.IsNullOrEmpty(winName))
                {

                    int hWnd = FindWindow(winName, null);
                    if (hWnd == 0)
                    {
                        //不存在
                        try
                        {
                            // Start the process
                            process = System.Diagnostics.Process.Start(IniData.VideoUrl.pathvalue);
                            // Wait for process to be created and enter idle condition
                            process.WaitForInputIdle();

                            // Get the main handle
                            appWin = process.MainWindowHandle;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(this, ex.Message, "Error");
                        }
                    }
                    else
                    {
                        IntPtr p = new IntPtr(hWnd);
                        //存在
                        SetForegroundWindow(p);

                        ShowWindowAsync(p, WS_SHOWNORMAL); //显示 
                        SetForegroundWindow(p);            //放到前端 

                    }
                }
                else
                {
                    AbstractPlugin.APluginDevice.ExportLog("摄像监控:程序路径配置有误。");
                }
            }
            catch
            {
                AbstractPlugin.APluginDevice.ExportLog("摄像监控:程序路径配置有误。");
            }
}
原文地址:https://www.cnblogs.com/z45281625/p/10937464.html