-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathwindow_manager.cc
137 lines (114 loc) · 4.47 KB
/
window_manager.cc
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "../window_manager.h"
#include <windows.h>
/**
* Documentation regarding the `EnumWindowsProc` already seems a bit stale (https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms633498(v=vs.85))
* We should keep this in mind for the future, just in case there should be any deprecations or strange behaviour
*/
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lparam) {
std::vector<WindowHandle>* windowHandles = reinterpret_cast<std::vector<WindowHandle>*>(lparam);
if (windowHandles != nullptr) {
windowHandles->push_back(reinterpret_cast<WindowHandle>(hwnd));
}
return TRUE;
}
std::vector<WindowHandle> getWindows() {
std::vector<WindowHandle> windowHandles;
EnumWindows (&EnumWindowsProc, reinterpret_cast<LPARAM>(&windowHandles));
return windowHandles;
}
WindowHandle getActiveWindow() {
auto foregroundWindow = GetForegroundWindow();
if (IsWindow(foregroundWindow)) {
return reinterpret_cast<WindowHandle>(foregroundWindow);
}
return -1;
}
MMRect getWindowRect(const WindowHandle windowHandle) {
auto hWnd = reinterpret_cast<HWND>(windowHandle);
RECT windowRect;
if (IsWindow(hWnd) && GetWindowRect(hWnd, &windowRect)) {
return MMRectMake(windowRect.left, windowRect.top, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top);
}
return MMRectMake(0, 0, 0, 0);
}
std::string getWindowTitle(const WindowHandle windowHandle) {
auto hWnd = reinterpret_cast<HWND>(windowHandle);
if (IsWindow(hWnd)) {
auto BUFFER_SIZE = GetWindowTextLength(hWnd) + 1;
if (BUFFER_SIZE) {
auto windowTitle = new CHAR[BUFFER_SIZE];
if (GetWindowText(hWnd, windowTitle, BUFFER_SIZE)) {
return {windowTitle};
}
}
}
return "";
}
bool focusWindow(const WindowHandle windowHandle) {
auto hWnd = reinterpret_cast<HWND>(windowHandle);
if (GetForegroundWindow() == hWnd) {
return true;
}
if (IsWindow(hWnd)) {
// Restore the window if it's minimized
if (IsIconic(hWnd)) {
ShowWindow(hWnd, SW_RESTORE);
}
auto processId = GetCurrentProcessId();
// const auto allowSetForeground = AllowSetForegroundWindow(ASFW_ANY);
const auto allowSetForeground = AllowSetForegroundWindow(processId);
const auto setTopLevel = BringWindowToTop(hWnd);
const auto setForeground = SetForegroundWindow(hWnd);
const auto setActive = SetActiveWindow(hWnd);
// Try to set the window to the foreground
if (allowSetForeground && setTopLevel && setForeground && setActive)
{
return true;
}
// get the current thread id
DWORD dwCurrentThread = GetCurrentThreadId();
DWORD dwFGThread = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
// attach the input to the current thread
AttachThreadInput(dwCurrentThread, dwFGThread, TRUE);
SetForegroundWindow(hWnd);
SetCapture(hWnd);
SetFocus(hWnd);
SetActiveWindow(hWnd);
EnableWindow(hWnd, TRUE);
// detach the input from the current thread
AttachThreadInput(dwCurrentThread, dwFGThread, FALSE);
// check if the window is now in the foreground
return GetForegroundWindow() == hWnd;
}
return false;
}
bool resizeWindow(const WindowHandle windowHandle, const MMSize newSize) {
auto hWnd = reinterpret_cast<HWND>(windowHandle);
if (IsWindow(hWnd)) {
//size
const auto width = newSize.width;
const auto height = newSize.height;
RECT currentPosition;
GetWindowRect(reinterpret_cast<HWND>(windowHandle), ¤tPosition);
//origin
const auto x = currentPosition.left;
const auto y = currentPosition.top;
return MoveWindow(hWnd, x, y, width, height, TRUE);
}
return false;
}
bool moveWindow(const WindowHandle windowHandle, const MMPoint newOrigin) {
auto hWnd = reinterpret_cast<HWND>(windowHandle);
if (IsWindow(hWnd)) {
// origin
const auto x = newOrigin.x;
const auto y = newOrigin.y;
RECT currentPosition;
GetWindowRect(reinterpret_cast<HWND>(windowHandle), ¤tPosition);
// size
const auto width = currentPosition.right - currentPosition.left;
const auto height = currentPosition.bottom - currentPosition.top;
return MoveWindow(hWnd, x, y, width, height, TRUE);
}
return false;
}