Last active
July 17, 2023 14:26
-
-
Save spajus/02900b3582b915541c5893f51db521da to your computer and use it in GitHub Desktop.
A tiling window manager for streaming / recording video on 3440x1440 screen with 2560x1440 capture area in the center.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <Windows.h> | |
#include <WinUser.h> | |
#include <TlHelp32.h> | |
void ResizeActiveWindow(float hScale, float vScale, int newX, int newY) { | |
HWND activeWindow = GetForegroundWindow(); | |
if (activeWindow) { | |
// Get the handle to the monitor where the window is currently located | |
HMONITOR hMonitor = MonitorFromWindow(activeWindow, MONITOR_DEFAULTTONEAREST); | |
// Get the information about the monitor | |
MONITORINFO mi = { sizeof(MONITORINFO) }; | |
GetMonitorInfo(hMonitor, &mi); | |
DWORD activeWindowProcessId; | |
GetWindowThreadProcessId(activeWindow, &activeWindowProcessId); | |
int newWidth = static_cast<int>(2560 * hScale); | |
int newHeight = static_cast<int>(1440 * vScale); | |
// Calculate the new position and size of the window | |
//int newWidth = static_cast<int>((mi.rcMonitor.right - mi.rcMonitor.left) * hScale); | |
//int newHeight = static_cast<int>((mi.rcMonitor.bottom - mi.rcMonitor.top) * vScale); | |
newX += mi.rcMonitor.left; | |
newY += mi.rcMonitor.top; | |
UINT flags = SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOOWNERZORDER; | |
ShowWindow(activeWindow, SW_RESTORE); | |
SetWindowPos(activeWindow, NULL, newX, newY, newWidth, newHeight, flags); | |
} | |
} | |
void ToggleActiveWindow() { | |
HWND activeWindow = GetForegroundWindow(); | |
if (activeWindow) { | |
WINDOWPLACEMENT wp; | |
wp.length = sizeof(WINDOWPLACEMENT); | |
GetWindowPlacement(activeWindow, &wp); | |
if (wp.showCmd == SW_SHOWMAXIMIZED) { | |
ShowWindow(activeWindow, SW_RESTORE); | |
} else { | |
ShowWindow(activeWindow, SW_MAXIMIZE); | |
} | |
} | |
} | |
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { | |
if (nCode == HC_ACTION) { | |
KBDLLHOOKSTRUCT *pKeyboard = (KBDLLHOOKSTRUCT *)lParam; | |
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) { | |
if (pKeyboard->vkCode == VK_DELETE && GetAsyncKeyState(VK_SHIFT) & 0x8000 && GetAsyncKeyState(VK_MENU) & 0x8000) { | |
PostQuitMessage(0); | |
} else if (pKeyboard->vkCode == 'M' && GetAsyncKeyState(VK_SHIFT) & 0x8000 && GetAsyncKeyState(VK_MENU) & 0x8000) { | |
int centerX = (3440 - 2560) / 2; | |
int centerY = (1440 - 1440) / 2; | |
ResizeActiveWindow(1.0f, 1.0f, centerX, centerY); | |
} else if ((pKeyboard->vkCode >= VK_NUMPAD0 && pKeyboard->vkCode <= VK_NUMPAD9) && /*GetAsyncKeyState(VK_SHIFT) & 0x8000 &&*/ GetAsyncKeyState(VK_CONTROL) & 0x8000) { | |
int centerX = (3440 - 2560) / 2; | |
int centerY = (1440 - 1440) / 2; | |
switch (pKeyboard->vkCode) { | |
case VK_NUMPAD0: | |
ToggleActiveWindow(); | |
break; | |
case VK_NUMPAD1: | |
ResizeActiveWindow(0.5f, 0.5f, centerX, centerY + 720); | |
break; | |
case VK_NUMPAD2: | |
ResizeActiveWindow(1.0f, 0.5f, centerX, centerY + 720); | |
break; | |
case VK_NUMPAD3: | |
ResizeActiveWindow(0.5f, 0.5f, centerX + 1280, centerY + 720); | |
break; | |
case VK_NUMPAD4: | |
ResizeActiveWindow(0.5f, 1.0f, centerX, centerY); | |
break; | |
case VK_NUMPAD5: | |
ResizeActiveWindow(1.0f, 1.0f, centerX, centerY); | |
break; | |
case VK_NUMPAD6: | |
ResizeActiveWindow(0.5f, 1.0f, centerX + 1280, centerY); | |
break; | |
case VK_NUMPAD7: | |
ResizeActiveWindow(0.5f, 0.5f, centerX, centerY); | |
break; | |
case VK_NUMPAD8: | |
ResizeActiveWindow(1.0f, 0.5f, centerX, centerY); | |
break; | |
case VK_NUMPAD9: | |
ResizeActiveWindow(0.5f, 0.5f, centerX + 1280, centerY); | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
} | |
return CallNextHookEx(NULL, nCode, wParam, lParam); | |
} | |
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { | |
FreeConsole(); | |
HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0); | |
MSG msg; | |
while (GetMessage(&msg, NULL, 0, 0)) { | |
TranslateMessage(&msg); | |
DispatchMessage(&msg); | |
} | |
UnhookWindowsHookEx(keyboardHook); | |
return (int)msg.wParam; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment