Beispiel #1
0
 private static bool EnumWindowsFindForProcess(IntPtr hWnd, int lParam)
 {         // the lParam was the one provided to EnumDesktopWindows, containing the process ID
     if (Windows.IsWindowVisible(hWnd))
     {
         uint processID;
         Windows.GetWindowThreadProcessId(hWnd, out processID);                 // returns thread (which we don't want) and the processID is in the out param
         if (processID == (uint)lParam)
         {
             g_FoundHWND = hWnd;
             //SetForegroundWindow(hWnd);
             Debug.WriteLine("Found window for process");
             return(false);                    // can stop enumeration now
         }
     }
     return(true);
 }
Beispiel #2
0
        /// <summary>Part of PopulateFromWindows only</summary>
        private bool EnumWindowsIntoProgramsList(IntPtr hWnd, int lParam)
        {
            if (Windows.IsWindowVisible(hWnd))              // Store the window only if the window is visible
            {
                string name = GetWindowText(hWnd);
                if (string.IsNullOrEmpty(name))
                {
                    return(true);                     // we get a load of empty window names - usually followed by another for same app with title - don't really want multiple per app anyway
                }
                uint processId;
                Windows.GetWindowThreadProcessId(hWnd, out processId);
                if (processId == Process.GetCurrentProcess().Id)                 // this window is ours!
                {
                    return(true);
                }

                string        path        = null;
                int           length      = 1024;
                StringBuilder pathBuilder = new StringBuilder(length);
                // requires Vista or later:  (we no longer have the fall back that was in SAW6)
                IntPtr processHandle = Windows.OpenProcess(Windows.ProcessAccessFlags.QueryLimitedInformation, false, (int)processId);
                if (!processHandle.IsZero())                 // will fail for a few (eg System process is protected), but they're not going to be of any interest
                {
                    if (Windows.QueryFullProcessImageName(processHandle, 0, pathBuilder, ref length))
                    {
                        path = pathBuilder.ToString();
                    }
                    Windows.CloseHandle(processHandle);
                }

                Windows.RECT bounds;
                Windows.GetWindowRect(hWnd, out bounds);
                if (!string.IsNullOrEmpty(path))
                {
                    Programs.Add(new Program()
                    {
                        Name = name, Path = path, Bounds = bounds
                    });
                }
                else
                {
                    Debug.WriteLine($"Could not find path for window: {name}\r\n");
                }
            }

            return(true);
        }