2

I am trying to reproduce alt tab on code,but I am having trouble with getting only processes that have a window and sorting them by order of use like, just like alt + tab would do.

Process[] ram = Process
  .GetProcesses()
  .Where(x => tryGetHandle(x) != IntPtr.Zero)
  .ToArray();

List<IntPtr> hwnds = new List<IntPtr>(); 

foreach(Process proc in ram)
{
   hwnds.Add(proc.Handle);
}

I have a list of handles because I was trying to get the Z order of them, but the answer I found here is not working for me, the result end up being every z on the list is -1.

Context: I am making a work for my computer interface class on college and I am making an app that connects with a cellphone to read its sensors and identify which monitor the person is looking to and then change focus (alt+tab) to the last window used on that monitor.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 2
    I don't know what you tried from the answer you linked to, but windows does not expose the actual z-order value of any windows; the GetTopWindow and GetNextWindow calls will just yield the windows in descending z-order. – Mike Nakis Jun 14 '17 at 12:22
  • Get all the processes. Then use the parent process ids to find out which processes are the descendant of the cellphones. You probably need a recursive algorithms to get all the grandchildren of the parents. – jdweng Jun 14 '17 at 12:46
  • 1
    `alt + tab` does not shows processes, it shows top-level windows. Try to enumerate windows instead. The [EnumWindows](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633497(v=vs.85).aspx) API function could be a good place to start. – Alejandro Jun 14 '17 at 14:16
  • Why `foreach` and `Add`? Why not `List hwnds = ram.Select(x => x.Handle).ToList();`? – Yeldar Kurmangaliyev Jun 14 '17 at 15:03
  • no reason at all, I am just not used with linq – Augusto Timm Jun 15 '17 at 22:07

0 Answers0