This repository has been archived by the owner on May 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFindWindowLike.cs
121 lines (98 loc) · 3.89 KB
/
FindWindowLike.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
namespace WindowSearch
{
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// Window Finding Utilities
/// </summary>
internal class FindWindowLike
{
private const int GWLID = -12;
private const int GWHWNDNEXT = 2;
private const int GWCHILD = 5;
/// <summary>
/// Find all windows matching a given title and class
/// </summary>
/// <param name="hwndStart">Beginning hwnd to start searching at</param>
/// <param name="findText">Window title to search</param>
/// <param name="findClassName">Class name to search</param>
/// <returns>Returns an array of windows that matches the given arguments</returns>
public static Window[] Find(int hwndStart, string findText, string findClassName)
{
ArrayList windows = DoSearch(hwndStart, findText, findClassName);
return (Window[])windows.ToArray(typeof(Window));
}
private static ArrayList DoSearch(int hwndStart, string findText, string findClassName)
{
ArrayList list = new ArrayList();
if (hwndStart == 0)
{
hwndStart = GetDesktopWindow();
}
int hwnd = GetWindow(hwndStart, GWCHILD);
while (hwnd != 0)
{
// Recursively search for child windows.
list.AddRange(DoSearch(hwnd, findText, findClassName));
StringBuilder text = new StringBuilder(255);
int rtn = GetWindowText(hwnd, text, 255);
string windowText = text.ToString();
windowText = windowText.Substring(0, rtn);
StringBuilder cls = new StringBuilder(255);
rtn = GetClassName(hwnd, cls, 255);
string className = cls.ToString();
className = className.Substring(0, rtn);
if (GetParent(hwnd) != 0)
{
rtn = GetWindowLong(hwnd, GWLID);
}
if (windowText.Length > 0 && windowText.StartsWith(findText) &&
(className.Length == 0 || className.StartsWith(findClassName)))
{
Window currentWindow = new Window
{
Title = windowText,
Class = className,
Handle = hwnd
};
list.Add(currentWindow);
}
hwnd = GetWindow(hwnd, GWHWNDNEXT);
}
return list;
}
[DllImport("user32")]
private static extern int GetWindow(int hwnd, int wCmd);
[DllImport("user32")]
private static extern int GetDesktopWindow();
[DllImport("user32", EntryPoint = "GetWindowLongA")]
private static extern int GetWindowLong(int hwnd, int nIndex);
[DllImport("user32")]
private static extern int GetParent(int hwnd);
[DllImport("user32", EntryPoint = "GetClassNameA")]
private static extern int GetClassName(
int hWnd, [Out] StringBuilder lpClassName, int nMaxCount);
[DllImport("user32", EntryPoint = "GetWindowTextA")]
private static extern int GetWindowText(
int hWnd, [Out] StringBuilder lpString, int nMaxCount);
/// <summary>
/// Window Parameters
/// </summary>
internal class Window
{
/// <summary>
/// Gets or sets window title
/// </summary>
internal string Title { get; set; }
/// <summary>
/// Gets or sets class name
/// </summary>
internal string Class { get; set; }
/// <summary>
/// Gets or sets handle
/// </summary>
internal int Handle { get; set; }
}
}
}