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("攝像監控:程序路徑配置有誤。");
            }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM