Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted

1 solution

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:
C#
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);
         }
      }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900