@@ -22,15 +22,15 @@ std::vector<WindowHandle> getWindows() {
22
22
}
23
23
24
24
WindowHandle getActiveWindow () {
25
- HWND foregroundWindow = GetForegroundWindow ();
25
+ auto foregroundWindow = GetForegroundWindow ();
26
26
if (IsWindow (foregroundWindow)) {
27
27
return reinterpret_cast <WindowHandle>(foregroundWindow);
28
28
}
29
29
return -1 ;
30
30
}
31
31
32
32
MMRect getWindowRect (const WindowHandle windowHandle) {
33
- HWND hWnd = reinterpret_cast <HWND>(windowHandle);
33
+ auto hWnd = reinterpret_cast <HWND>(windowHandle);
34
34
RECT windowRect;
35
35
if (IsWindow (hWnd) && GetWindowRect (hWnd, &windowRect)) {
36
36
return MMRectMake (windowRect.left , windowRect.top , windowRect.right - windowRect.left , windowRect.bottom - windowRect.top );
@@ -39,65 +39,71 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
39
39
}
40
40
41
41
std::string getWindowTitle (const WindowHandle windowHandle) {
42
- HWND hWnd = reinterpret_cast <HWND>(windowHandle);
42
+ auto hWnd = reinterpret_cast <HWND>(windowHandle);
43
43
if (IsWindow (hWnd)) {
44
44
auto BUFFER_SIZE = GetWindowTextLength (hWnd) + 1 ;
45
45
if (BUFFER_SIZE) {
46
- LPSTR windowTitle = new CHAR[BUFFER_SIZE];
46
+ auto windowTitle = new CHAR[BUFFER_SIZE];
47
47
if (GetWindowText (hWnd, windowTitle, BUFFER_SIZE)) {
48
- return std::string ( windowTitle) ;
48
+ return { windowTitle} ;
49
49
}
50
50
}
51
51
}
52
52
return " " ;
53
53
}
54
54
55
55
bool focusWindow (const WindowHandle windowHandle) {
56
- HWND hWnd = reinterpret_cast <HWND>(windowHandle);
56
+ auto hWnd = reinterpret_cast <HWND>(windowHandle);
57
57
if (IsWindow (hWnd)) {
58
58
// Restore the window if it's minimized
59
59
if (IsIconic (hWnd)) {
60
60
ShowWindow (hWnd, SW_RESTORE);
61
61
}
62
-
62
+
63
+ auto processId = GetCurrentProcessId ();
64
+ // const auto allowSetForeground = AllowSetForegroundWindow(ASFW_ANY);
65
+ const auto allowSetForeground = AllowSetForegroundWindow (processId);
66
+ const auto setTopLevel = BringWindowToTop (hWnd);
67
+ const auto setForeground = SetForegroundWindow (hWnd);
68
+
63
69
// Try to set the window to the foreground
64
- return SetForegroundWindow (hWnd) ;
70
+ return allowSetForeground && setTopLevel && setForeground ;
65
71
}
66
72
return false ;
67
73
}
68
74
69
75
bool resizeWindow (const WindowHandle windowHandle, const MMSize newSize) {
70
- HWND hWnd = reinterpret_cast <HWND>(windowHandle);
76
+ auto hWnd = reinterpret_cast <HWND>(windowHandle);
71
77
if (IsWindow (hWnd)) {
72
78
// size
73
- auto width = newSize.width ;
74
- auto height = newSize.height ;
79
+ const auto width = newSize.width ;
80
+ const auto height = newSize.height ;
75
81
76
82
RECT currentPosition;
77
83
GetWindowRect (reinterpret_cast <HWND>(windowHandle), ¤tPosition);
78
84
79
85
// origin
80
- auto x = currentPosition.left ;
81
- auto y = currentPosition.top ;
86
+ const auto x = currentPosition.left ;
87
+ const auto y = currentPosition.top ;
82
88
83
89
return MoveWindow (hWnd, x, y, width, height, TRUE );
84
90
}
85
91
return false ;
86
92
}
87
93
88
94
bool moveWindow (const WindowHandle windowHandle, const MMPoint newOrigin) {
89
- HWND hWnd = reinterpret_cast <HWND>(windowHandle);
95
+ auto hWnd = reinterpret_cast <HWND>(windowHandle);
90
96
if (IsWindow (hWnd)) {
91
97
// origin
92
- auto x = newOrigin.x ;
93
- auto y = newOrigin.y ;
98
+ const auto x = newOrigin.x ;
99
+ const auto y = newOrigin.y ;
94
100
95
101
RECT currentPosition;
96
102
GetWindowRect (reinterpret_cast <HWND>(windowHandle), ¤tPosition);
97
103
98
104
// size
99
- auto width = currentPosition.right - currentPosition.left ;
100
- auto height = currentPosition.bottom - currentPosition.top ;
105
+ const auto width = currentPosition.right - currentPosition.left ;
106
+ const auto height = currentPosition.bottom - currentPosition.top ;
101
107
102
108
return MoveWindow (hWnd, x, y, width, height, TRUE );
103
109
}
0 commit comments