wpf 打开外部程序并在需要时激活

来源:互联网 发布:python之父谈go 编辑:程序博客网 时间:2024/05/11 19:57

打开外部程序使用Process

激活窗体,通过SetForegroundWindow函数传递Process获取的句柄激活


_external = new Process();

[DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]        public static extern int SetForegroundWindow(IntPtr hWnd);public void Run(string file, string args)        {            bool _bExisted = true;            try            {                if (_external.HasExited == false)                {                    _bExisted = false;                    SetForegroundWindow(_external.MainWindowHandle);                    return;                }            }            catch (Exception e)            {                Global.MainLogging.LogError("ExternalProcess.Run:param1:" + file + ",param2:" + args + "," + e.ToString());            }            finally            {                if (_bExisted) RunExternal(file, args);            }        }

0 0