Я хочу иметь инструмент, который предоставляет функцию Alt + Tab
, но вместо переключения между всеми открывающимися окнами я хочу сузить их до 1 конкретной программы (например, firefox.exe).Переключение между окнами программы
Все, что я мог придумать, это использовать GetWindowText
, чтобы получить список окон, содержащих «Mozilla Firefox» в их названиях, а затем ShowWindowAsync
, чтобы показать их, но, похоже, это не сработает, если некоторые другие открывающиеся окна также имеют эту фразу «Mozilla Firefox» в своих названиях.
Есть ли лучшее решение? Вот код:
/// <summary>Contains functionality to get all the open windows.</summary>
public static class OpenWindowGetter
{
/// <summary>Returns a dictionary that contains the handle and title of all the open windows.</summary>
/// <returns>A dictionary that contains the handle and title of all the open windows.</returns>
public static IDictionary<HWND, string> GetOpenWindows()
{
HWND shellWindow = GetShellWindow();
Dictionary<HWND, string> windows = new Dictionary<HWND, string>();
EnumWindows(delegate(HWND hWnd, int lParam)
{
if (hWnd == shellWindow) return true;
if (!IsWindowVisible(hWnd)) return true;
int length = GetWindowTextLength(hWnd);
if (length == 0) return true;
StringBuilder builder = new StringBuilder(length);
GetWindowText(hWnd, builder, length + 1);
windows[hWnd] = builder.ToString();
return true;
}, 0);
return windows;
}
private delegate bool EnumWindowsProc(HWND hWnd, int lParam);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(HWND hWnd);
[DllImport("USER32.DLL")]
private static extern bool IsWindowVisible(HWND hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr GetShellWindow();
}
Это, как я использую его:
/// <summary>
/// Get a window handle by a title
/// </summary>
/// <param name="windowTitle"></param>
/// <param name="wildCard">match if window title contains input windowTitle</param>
/// <returns></returns>
public static int GetWindowHandle(string windowTitle, bool wildCard = false)
{
var processList = OpenWindowGetter.GetOpenWindows();
foreach (var process in processList)
{
if ((wildCard && process.Value.ToLower().Contains(windowTitle.ToLower())) //Find window by wildcard
|| !wildCard && process.Value.Equals(windowTitle)) //Find window with exact name
{
int a = (int)process.Key;
return a;
}
}
return 0;
}
Различные (верхние) окна ap Обычно использование одного и того же класса окон. Вызовите [GetClassName] (https://msdn.microsoft.com/en-us/library/windows/desktop/ms633582.aspx) в обратном вызове 'EnumWindowProc' для фильтрации имени класса. – IInspectable
Только вы можете принять решение по вашим критериям. Кто они такие? Как только вы решите, используйте EnumWindows для получения окон верхнего уровня. –