活动进程忽略子进程 [英] Active process ignoring child process

查看:65
本文介绍了活动进程忽略子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我写了一个小的基本程序(progA)代替Windows shell,该程序使用户可以运行在此shell中设置的任何程序(Prog1,Prog2,Prog3,Prog4等).
我想做的是,确定Prog1是否当前正在运行(集中)的进程,并且是否正在运行但不在前台,则必须将其更改为前台.
这很容易,但是我面临的问题是Prog1本身会调用其他应用程序.
这意味着,如果Prog1启动Prog1.1,则我的软件将确定Prog1没有聚焦,这会将Prog1移到Prog1.1的顶部. (因为Prog1仍然处于活跃状态)

如果任何其他进程(不包括Prog1子进程)处于活动状态,则我需要它将Prog1仅设置为活动窗口.
希望这有道理.
在此先感谢

Hi,
I wrote a small base program(progA) replacing the Windows shell, this program lets the user run any programs(Prog1,Prog2,Prog3,Prog4 etc) that were setup in this shell.

what i am trying to do is, determine if Prog1 is the current process running(focused), and if it is running but not in the foreground, it must change it to foreground.
This is quite easy, but the problem i am facing is that Prog1 calls other applications within itself.
That means if Prog1 launches Prog1.1, my software determines that Prog1 is not focused, and this moves Prog1 ontop of Prog1.1. (It musn''t as Prog1 is still sortof active)

I need it to set Prog1 only as active window if any other process (excl Prog1 child processes) are active.
Hope this makes sense.
Thanks in advance

推荐答案


我相信我已经为那些寻找答案的人解决了:
我不确定是否有更简单的方法,如果有,请提出建议.
在我的应用程序中添加了一个类:
Hi,
I believe i''ve solved it for those out there looking for an answer:
I am not sure if there is an easier way, if there is, please suggest.
Added a class to my application:
class ActiveProcess
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
        private static Process GetProcessByHandle(IntPtr hwnd)
        {
            try
            {
                uint processID;
                GetWindowThreadProcessId(hwnd, out processID);
                return Process.GetProcessById((int)processID);
            }
            catch { return null; }
        }
        private static Process GetActiveProcess()
        {
            IntPtr hwnd = GetForegroundWindow();
            return hwnd != null ? GetProcessByHandle(hwnd) : null;
        }



        public bool IsRunning(string File)
        {
            bool Loadcative = false;
            try
            {
                Process currentProcess = GetActiveProcess();
                var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", currentProcess.Id);
                var search = new ManagementObjectSearcher("root\\CIMV2", query);
                var results = search.Get().GetEnumerator();
                var queryObj = results.Current;
                uint parentId = (uint)queryObj["ParentProcessId"];
                var parent = Process.GetProcessById((int)parentId);
                if ((parent.ProcessName == File))
                    Loadcative = false;
                else
                    Loadcative = true;
            }
            catch { }
            return Loadcative;

        }
    }


然后在计时器上这样调用它:


and then calling it like this on a timer:

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



     private void Tmr_CheckMap_Tick(object sender, EventArgs e)
     {
         string filetocheck = "enterparentfilenamehere";
         Process currentProcess = Process.GetCurrentProcess();
         Process[] pname1 = Process.GetProcessesByName(filetocheck);
         ActiveProcess AP = new ActiveProcess();

         if (pname1.Length != 0 && AP.IsRunning(filetocheck))
         {
             SetForegroundWindow(pname1[0].MainWindowHandle);
         }
      }


这篇关于活动进程忽略子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆