ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-05-28 15:03:30  阅读:197  来源: 互联网

标签:IntPtr process 窗口 string C# 程序 private int 前台


     #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("摄像监控:程序路径配置有误。");
            }
}

 

标签:IntPtr,process,窗口,string,C#,程序,private,int,前台
来源: https://www.cnblogs.com/z45281625/p/10937464.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有