Skip to content

Commit

Permalink
add FindWindowsGreedy
Browse files Browse the repository at this point in the history
  • Loading branch information
rcktrncn committed Jul 24, 2021
1 parent aaa9475 commit df0fb7b
Showing 1 changed file with 80 additions and 7 deletions.
87 changes: 80 additions & 7 deletions taskt/Core/Automation/User32/User32Functionas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,60 @@ public static IntPtr FindWindow(string windowName)
if (potentialWindow != null)
hWnd = potentialWindow.MainWindowHandle;
}


//return hwnd
return hWnd;
}
}


public static List<IntPtr> FindWindowsGreedy(string windowName)
{
List<IntPtr> ret = new List<IntPtr>();
if (windowName.Contains("Windows Explorer -"))
{
var windowLocationName = windowName.Split('-')[1].Trim();

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

foreach (SHDocVw.InternetExplorer window in shellWindows)
{
if (window.LocationName.Contains(windowLocationName))
{
ret.Add((IntPtr)window.HWND);
}
}
return ret;
}
else
{
//try to find exact window name
IntPtr hWnd = FindWindowNative(null, windowName);

if (hWnd == IntPtr.Zero)
{
//potentially wait for some additional initialization
System.Threading.Thread.Sleep(1000);
hWnd = FindWindowNative(null, windowName);
if (hWnd != IntPtr.Zero)
{
ret.Add(hWnd);
}
}
else
{
ret.Add(hWnd);
}
//if exact window was not found, try partial match
var potentialWindows = System.Diagnostics.Process.GetProcesses().Where(prc => prc.MainWindowTitle.Contains(windowName)).ToList();
foreach (var potentialWindow in potentialWindows)
{
ret.Add(potentialWindow.MainWindowHandle);
}
//return hwnd
return ret;
}
}

public static List<IntPtr> FindTargetWindows(string windowName, bool findCurrentWindow = true)
public static List<IntPtr> FindTargetWindows(string windowName, bool findCurrentWindow = false, bool greedy = false)
{
//create list of hwnds to target
List<IntPtr> targetWindows = new List<IntPtr>();
Expand All @@ -103,7 +147,7 @@ public static List<IntPtr> FindTargetWindows(string windowName, bool findCurrent
targetWindows.Add(prc.MainWindowHandle);
}
}
else
else if (!greedy)
{
//target current or specific window
IntPtr hwnd;
Expand All @@ -129,11 +173,40 @@ public static List<IntPtr> FindTargetWindows(string windowName, bool findCurrent
//add to list
targetWindows.Add(hwnd);
}

}
else
{
//target current or specific window
if (findCurrentWindow)
{
//get active window
IntPtr hwnd;
hwnd = User32Functions.GetActiveWindow();
if (hwnd == IntPtr.Zero)
{
throw new Exception("Window not found");
}
else
{
targetWindows.Add(hwnd);
}
}
else
{
//find window by name
var hWnds = User32Functions.FindWindowsGreedy(windowName);
if (hWnds.Count == 0)
{
throw new Exception("Window not found");
}
else
{
targetWindows.AddRange(hWnds);
}
}
}

return targetWindows;

}
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
Expand Down

0 comments on commit df0fb7b

Please sign in to comment.