-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowFinder.cpp
More file actions
77 lines (71 loc) · 2.02 KB
/
WindowFinder.cpp
File metadata and controls
77 lines (71 loc) · 2.02 KB
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
#include "stdafx.h"
#include "WindowFinder.h"
// Window enumeration callback function
// lparam is a pointer to SearchData structure
BOOL CALLBACK WindowFinder::EnumWindowProc(HWND hwnd, LPARAM lparam)
{
_ASSERT(hwnd != NULL);
_ASSERT(lparam != NULL);
SearchData* data = (SearchData*)lparam;
// Check parent window
if (data->wnd_parent != NULL)
{
if (GetAncestor(hwnd, GA_PARENT) != data->wnd_parent)
return TRUE;
}
// Check PID
if (data->wnd_pid != 0)
{
DWORD pid;
GetWindowThreadProcessId(hwnd, &pid);
if (pid != data->wnd_pid)
return TRUE;
}
// Check class name
if (data->wnd_class != NULL)
{
WCHAR class_name[CLASS_BUF_SZ];
if (GetClassName(hwnd, class_name, CLASS_BUF_SZ) == 0)
return TRUE;
if (cf_wcsncmp(class_name, data->wnd_class, CLASS_BUF_SZ) != 0)
return TRUE;
}
if (data->find_all)
{
data->res_wnds->Append(hwnd);
return TRUE;
}
else
{
data->res_wnd = hwnd;
return FALSE;
}
}
// Find window that is a direct child of 'parent', has class name 'wnd_class'
// and belongs to process 'pid'.
// If either of the arguments is NULL or 0 it is ignored.
HWND WindowFinder::FindWnd(HWND parent, bool direct_child, const WCHAR* wclass, ULONG_PTR pid)
{
SearchData data;
data.find_all = false;
data.wnd_parent = (direct_child ? parent : NULL);
data.wnd_class = wclass;
data.wnd_pid = pid;
data.res_wnd = NULL;
EnumChildWindows(parent, EnumWindowProc, (LPARAM)&data);
return data.res_wnd;
}
// Find all windows that are direct children of 'parent', have class name 'wnd_class'
// and belong to process 'pid'.
// If either of the arguments is NULL or 0 it is ignored.
ArrayHWND* WindowFinder::FindWnds(HWND parent, bool direct_child, const WCHAR* wclass, ULONG_PTR pid)
{
SearchData data;
data.find_all = true;
data.wnd_parent = (direct_child ? parent : NULL);
data.wnd_class = wclass;
data.wnd_pid = pid;
data.res_wnds = new ArrayHWND;
EnumChildWindows(parent, EnumWindowProc, (LPARAM)&data);
return data.res_wnds;
}