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
base / win / window_enumerator.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/win/window_enumerator.h"
#include <windows.h>
#include <string>
#include "base/functional/callback.h"
namespace base::win {
namespace {
BOOL CALLBACK OnWindowProc(HWND hwnd, LPARAM lparam) {
return !reinterpret_cast<WindowEnumeratorCallback*>(lparam)->Run(hwnd);
}
} // namespace
void EnumerateChildWindows(HWND parent, WindowEnumeratorCallback filter) {
::EnumChildWindows(parent, &OnWindowProc, reinterpret_cast<LPARAM>(&filter));
}
bool IsTopmostWindow(HWND hwnd) {
return ::GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST;
}
bool IsSystemDialog(HWND hwnd) {
static constexpr wchar_t kSystemDialogClass[] = L"#32770";
return GetWindowClass(hwnd) == kSystemDialogClass;
}
bool IsShellWindow(HWND hwnd) {
const std::wstring class_name = GetWindowClass(hwnd);
// 'Button' is the start button, 'Shell_TrayWnd' the taskbar, and
// 'Shell_SecondaryTrayWnd' is the taskbar on non-primary displays.
return class_name == L"Button" || class_name == L"Shell_TrayWnd" ||
class_name == L"Shell_SecondaryTrayWnd";
}
std::wstring GetWindowClass(HWND hwnd) {
constexpr int kMaxWindowClassNameLength = 256;
std::wstring window_class(kMaxWindowClassNameLength, L'\0');
const int name_len =
::GetClassName(hwnd, window_class.data(), kMaxWindowClassNameLength);
if (name_len <= 0 || name_len > kMaxWindowClassNameLength) {
return {};
}
window_class.resize(static_cast<size_t>(name_len));
return window_class;
}
std::wstring GetWindowTextString(HWND hwnd) {
auto num_chars = ::GetWindowTextLength(hwnd);
if (num_chars <= 0) {
return {};
}
std::wstring text(static_cast<size_t>(num_chars), L'\0');
// MSDN says that GetWindowText will not write anything other than a string
// terminator to the last position in the buffer.
auto len = ::GetWindowText(hwnd, text.data(), num_chars + 1);
if (len <= 0) {
return std::wstring();
}
// GetWindowText may return a shorter string than reported above.
text.resize(static_cast<size_t>(len));
return text;
}
} // namespace base::win