-
Notifications
You must be signed in to change notification settings - Fork 59
/
WindowManager.cpp
85 lines (77 loc) · 3.28 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
#include "WindowManager.h"
WindowManager WindowManager::instance;
void WindowManager::applyCursorCapture() {
if(captureCursor) {
RECT clientrect;
HWND hwnd = ::GetActiveWindow();
::GetClientRect(hwnd, &clientrect);
::ClientToScreen(hwnd, (LPPOINT)&clientrect.left);
::ClientToScreen(hwnd, (LPPOINT)&clientrect.right);
::ClipCursor(&clientrect);
} else {
::ClipCursor(NULL);
}
}
void WindowManager::toggleCursorCapture() {
captureCursor = !captureCursor;
}
void WindowManager::toggleCursorVisibility() {
cursorVisible = !cursorVisible;
::ShowCursor(cursorVisible);
}
void WindowManager::toggleBorderlessFullscreen() {
borderlessFullscreen = !borderlessFullscreen;
HWND hwnd = ::GetActiveWindow();
if(borderlessFullscreen) {
// store previous rect
::GetClientRect(hwnd, &prevWindowRect);
// set styles
LONG lStyle = ::GetWindowLong(hwnd, GWL_STYLE);
prevStyle = lStyle;
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
::SetWindowLong(hwnd, GWL_STYLE, lStyle);
LONG lExStyle = ::GetWindowLong(hwnd, GWL_EXSTYLE);
prevExStyle = lExStyle;
lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
::SetWindowLong(hwnd, GWL_EXSTYLE, lExStyle);
// adjust size & position
HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &info);
int monitorWidth = info.rcMonitor.right - info.rcMonitor.left;
int monitorHeight = info.rcMonitor.bottom - info.rcMonitor.top;
::SetWindowPos(hwnd, NULL, info.rcMonitor.left, info.rcMonitor.top, monitorWidth, monitorHeight, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER);
} else {
// restore previous window
::SetWindowLong(hwnd, GWL_STYLE, prevStyle);
::SetWindowLong(hwnd, GWL_EXSTYLE, prevExStyle);
RECT desiredRect = prevWindowRect;
::AdjustWindowRect(&desiredRect, prevStyle, false);
int wWidth = desiredRect.right - desiredRect.left, wHeight = desiredRect.bottom - desiredRect.top;
::SetWindowPos(hwnd, NULL, prevWindowRect.left, prevWindowRect.top, wWidth, wHeight, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER);
}
}
void WindowManager::resize(unsigned clientW, unsigned clientH) {
HWND hwnd = ::GetActiveWindow();
// Store current window rect
::GetClientRect(hwnd, &prevWindowRect);
// Get monitor size
HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &info);
int monitorWidth = info.rcMonitor.right - info.rcMonitor.left;
int monitorHeight = info.rcMonitor.bottom - info.rcMonitor.top;
// How much do we overlap or are smaller than the actual screen size
int widthDiff = monitorWidth - (clientW ? clientW : prevWindowRect.right);
int heightDiff = monitorHeight - (clientH ? clientH : prevWindowRect.bottom);
RECT desiredRect;
desiredRect.left = widthDiff / 2;
desiredRect.top = heightDiff / 2;
desiredRect.right = monitorWidth - (widthDiff / 2);
desiredRect.bottom = monitorHeight - (heightDiff / 2);
LONG lStyle = ::GetWindowLong(hwnd, GWL_STYLE);
::AdjustWindowRect(&desiredRect, lStyle, false);
::SetWindowPos(hwnd, NULL, desiredRect.left, desiredRect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}