-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindowmanager.cpp
122 lines (98 loc) · 3.55 KB
/
windowmanager.cpp
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
#include <iostream>
#include <windows.h>
#include <winuser.h>
#include <list>
#include "arguments.h"
using namespace std;
list<HWND> findWindows(string partOfTitle) {
list<HWND> found;
for (HWND hwnd = GetTopWindow(NULL); hwnd != NULL; hwnd = GetNextWindow(hwnd, GW_HWNDNEXT))
{
if (!IsWindowVisible(hwnd))
continue;
int length = GetWindowTextLength(hwnd);
if (length == 0)
continue;
char* titleBuffer = new char[length+1];
GetWindowText(hwnd, titleBuffer, length+1);
string title = string(titleBuffer);
if (title.find(partOfTitle) == std::string::npos)
continue;
found.push_back(hwnd);
}
return found;
}
SIZE getScreenResolution(HWND hwnd) {
HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &info);
int monitor_width = info.rcMonitor.right - info.rcMonitor.left;
int monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;
SIZE screenResolution;
screenResolution.cx = monitor_width;
screenResolution.cy = monitor_height;
return screenResolution;
}
void sleepSecond() {
Sleep(1000);
}
void setWindowPositionWithTwoWindows(HWND window, SIZE screenSize, int windowNumber) {
SetWindowPos(window, IGNORE,
screenSize.cx / 2 * windowNumber,
0,
screenSize.cx / 2,
screenSize.cy,
SWP_NOZORDER|SWP_SHOWWINDOW);
}
void setWindowPositionWithFourWindows(HWND window, SIZE screenSize, int pos) {
// 0 | 1
// 2 | 3
int width = screenSize.cx / 2,
height = screenSize.cy / 2;
int x = width * (pos < 2 ? pos : pos - 2),
y = height * (pos < 2 ? 0 : 1);
SetWindowPos(window, IGNORE, x, y, width, height, SWP_NOZORDER|SWP_SHOWWINDOW);
}
void setWindowPositionWithThreeWindows(HWND window, SIZE screenSize, int windowNumber) {
switch (windowNumber) {
case 0:
setWindowPositionWithTwoWindows(window, screenSize, 0);
break;
case 1:
setWindowPositionWithFourWindows(window, screenSize, 1);
break;
case 2:
setWindowPositionWithFourWindows(window, screenSize, 3);
break;
}
}
void adjustWindows(list<HWND> windows, bool alwaysOnTop) {
list<HWND>::iterator windowsIterator = windows.begin();
SIZE screenSize = getScreenResolution(*windowsIterator);
const int windowsCount = windows.size();// > 2 ? 2 : windows.size();
for(int i=0; i<windowsCount; i++){
HWND window = *windowsIterator;
SetWindowLong(window, GWL_STYLE, WS_POPUP|WS_VISIBLE|WS_SYSMENU);
SetWindowPos(window, alwaysOnTop ? HWND_TOPMOST : HWND_TOP, 0,0,0,0, SWP_NOMOVE|SWP_NOSIZE);
SetForegroundWindow(window);
cout << (i+1) << " ... ";
switch (windowsCount) {
case 1:
SetWindowPos(window, IGNORE, 0, 0, screenSize.cx, screenSize.cy, SWP_NOZORDER|SWP_SHOWWINDOW);
sleepSecond();
break;
case 2:
setWindowPositionWithTwoWindows(window, screenSize, i);
break;
case 3:
setWindowPositionWithThreeWindows(window, screenSize, i);
break;
case 4:
setWindowPositionWithFourWindows(window, screenSize, i);
break;
}
sleepSecond();
++windowsIterator;
}
}