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
| #include "WindowUtils.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <cmath>
void SimpleFollow(void* myHandle, const char* targetTitle, const char* targetClass) { HWND myHwnd = (HWND)myHandle; static HWND targetHwnd = NULL;
static int findCounter = 0; if (findCounter++ % 60 == 0) { targetHwnd = FindWindowA(targetClass, targetTitle); }
if (targetHwnd && IsWindow(targetHwnd)) { HWND foregroundHwnd = GetForegroundWindow();
if (foregroundHwnd == targetHwnd || foregroundHwnd == myHwnd) { if (!IsWindowVisible(myHwnd)) { ShowWindow(myHwnd, SW_SHOWNOACTIVATE); }
RECT rect; if (GetWindowRect(targetHwnd, &rect)) { static RECT lastRect = { 0 }; if (rect.left != lastRect.left || rect.top != lastRect.top) { SetWindowPos(myHwnd, HWND_TOPMOST, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOACTIVATE); lastRect = rect; } } } else { if (IsWindowVisible(myHwnd)) { ShowWindow(myHwnd, SW_HIDE); } } } else { if (IsWindowVisible(myHwnd)) { ShowWindow(myHwnd, SW_HIDE); } } } void EstablishLink(void* myHandle, HWND targetHwnd) { HWND myHwnd = (HWND)myHandle;
HWND currentOwner = (HWND)GetWindowLongPtr(myHwnd, GWLP_HWNDPARENT);
if (currentOwner != targetHwnd) { SetWindowLongPtr(myHwnd, GWLP_HWNDPARENT, (LONG_PTR)targetHwnd);
SetWindowPos(myHwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_FRAMECHANGED); } }
|