Skip to content

Instantly share code, notes, and snippets.

@ricky26
Created May 4, 2018 21:57
Show Gist options
  • Save ricky26/5477c14dfc9c07b2c69cce1894d93979 to your computer and use it in GitHub Desktop.
Save ricky26/5477c14dfc9c07b2c69cce1894d93979 to your computer and use it in GitHub Desktop.
Workaround for scrolling and clicking on other windows in Skyrim
#include <Windows.h>
#include <Psapi.h>
static const wchar_t * const targetProcesses[] = {
L"\\skyrimse.exe",
L"\\skyrim.exe",
//L"\\notepad.exe",
};
bool clipProcess(wchar_t const* processName)
{
for (wchar_t const* const* targetProcess = targetProcesses;
targetProcess < (targetProcesses + ARRAYSIZE(targetProcesses));
targetProcess++)
{
wchar_t const* exe = wcsstr(processName, *targetProcess);
if (exe == (processName + wcslen(processName) - wcslen(*targetProcess)))
{
return true;
}
}
return false;
}
void CALLBACK handleForegroundWindow(
HWINEVENTHOOK hWinEventHook,
DWORD event,
HWND hwnd,
LONG idObject,
LONG idChild,
DWORD idEventThread,
DWORD dwmsEventTime)
{
auto fgWindow = GetForegroundWindow();
DWORD fgProcessId;
GetWindowThreadProcessId(fgWindow, &fgProcessId);
auto fgProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, fgProcessId);
wchar_t processName[MAX_PATH];
auto nameLen = GetProcessImageFileNameW(fgProcess, processName, ARRAYSIZE(processName));
processName[nameLen] = 0;
_wcslwr_s(processName);
bool enable = clipProcess(processName);
RECT fgRect;
GetClientRect(fgWindow, &fgRect);
POINT tl { fgRect.left, fgRect.top };
POINT br { fgRect.right, fgRect.bottom };
ClientToScreen(fgWindow, &tl);
ClientToScreen(fgWindow, &br);
fgRect = { tl.x, tl.y, br.x, br.y };
ClipCursor(enable ? &fgRect : nullptr);
}
int main(int argc, char **argv)
{
SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, nullptr, handleForegroundWindow, 0, 0, WINEVENT_OUTOFCONTEXT);
handleForegroundWindow(NULL, 0, NULL, 0, 0, 0, 0);
for (;;)
{
MSG msg;
if (GetMessage(&msg, NULL, 0, 0))
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment