效果:使Windows7系统中所有窗口在屏幕内来回弹跳
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <map>
using namespace std;
#define SLEEP 10
#define G 0.098
#define SHAKE 5
#define WEXTRA 10
#define HEXTRA 30
#define HOTKEY VK_F2
#define RANDMAX 2147483647
#define rand() (seed = ((long long)seed * 998244351 + 1000000007) % RANDMAX)
#define random() ((double)(rand()) / RANDMAX)
#define randrange(l, r) (l + random() * (r - l))
int xs, xe, ys, ye;
map<HWND, int> mp;
int cnt = 0;
DWORD WINAPI ShakeWindow(LPVOID lParam)
{
int seed = time(0);
HWND hWnd = *(HWND *)lParam;
delete lParam;
lParam = NULL;
RECT rect;
GetWindowRect(hWnd, &rect);
int tw = rect.right - rect.left, th = rect.bottom - rect.top;
double tx = rect.left, ty = rect.top;
double vx = randrange(-SHAKE, SHAKE), vy = randrange(-SHAKE, SHAKE);
while (1) {
while (IsWindow(hWnd)) {
GetWindowRect(hWnd, &rect);
int tw = rect.right - rect.left, th = rect.bottom - rect.top;
if (tx < xs && tw < xe - xs)
tx = xs;
if (ty < ys && th < ye - ys)
ty = ys;
if (tx + tw > xe && tw < xe - xs)
tx = xe - tw;
if (ty + th > ye && th < ye - ys)
ty = ye - th;
if (tx + vx < xs) {
if (tx + vx + tw >= xe) {
vx = 0;
} else {
vx = -vx;
if (vy < 0) {
if(vy > 0)
vy = 0;
} else {
if(vy < 0)
vy = 0;
}
}
} else {
if (tx + vx + tw >= xe) {
vx = -vx;
if (vy < 0) {
if(vy > 0)
vy = 0;
} else {
if(vy < 0)
vy = 0;
}
}
else
tx += vx;
}
if (ty + vy < ys) {
if (ty + vy + th >= ye) {
vy = 0;
} else {
vy = -vy;
if (vx < 0) {
if(vx > 0)
vx = 0;
} else {
if(vx < 0)
vx = 0;
}
}
} else {
if (ty + vy + th >= ye) {
vy = -vy;
if (vx < 0) {
if(vx > 0)
vx = 0;
} else {
if(vx < 0)
vx = 0;
}
} else {
ty += vy;
}
}
vy += G;
MoveWindow(hWnd, tx, ty, tw, th, true);
Sleep(SLEEP);
if ((GetAsyncKeyState(HOTKEY) || GetAsyncKeyState(VK_LBUTTON)) && hWnd == GetForegroundWindow()) {
POINT p;
GetCursorPos(&p);
GetWindowRect(hWnd,&rect);
tw = rect.right - rect.left, th = rect.bottom - rect.top;
const int ttx = p.x - rect.left;
const int tty = p.y - rect.top;
int px = p.x, py = p.y;
if (ttx >= 0 && tty >= 0 && ttx < tw && tty < th) {
while (GetAsyncKeyState(HOTKEY) || GetAsyncKeyState(VK_LBUTTON)) {
px = p.x;
py = p.y;
GetCursorPos(&p);
GetWindowRect(hWnd,&rect);
tw = rect.right - rect.left;
th = rect.bottom - rect.top;
tx = rect.left;
ty = rect.top;
vx = p.x - px;
vy = p.y - py;
MoveWindow(hWnd, tx + vx, ty + vy, tw, th, true);
Sleep(SLEEP);
}
}
}
}
if (!IsWindowVisible(hWnd))
break;
}
HANDLE hMutex = CreateMutex(NULL, false, "shake");
WaitForSingleObject(hMutex, INFINITE);
mp.erase(hWnd);
cnt--;
ReleaseMutex(hMutex);
CloseHandle(hMutex);
return 0;
}
map<HWND, HANDLE> mp2;
BOOL CALLBACK lpEnumFunc(HWND hwnd, LPARAM lParam)
{
if(mp.find(hwnd) == mp.end() && IsWindowVisible(hwnd) && hwnd != GetStdHandle(STD_OUTPUT_HANDLE)) {
HWND *p = new HWND(hwnd);
cnt++;
HANDLE hMutex = CreateMutex(NULL, false, "shake");
WaitForSingleObject(hMutex, INFINITE);
mp.insert(pair<HWND, int>(hwnd, 1));
ReleaseMutex(hMutex);
CloseHandle(hMutex);
CreateThread(NULL, 10, ShakeWindow, p, 0, NULL);
}
return true;
}
int main()
{
RECT rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
xs = -WEXTRA;
ys = -HEXTRA;
xe = rect.right - rect.left + WEXTRA;
ye = rect.bottom - rect.top + HEXTRA;
while (1) {
Sleep(100);
EnumWindows(lpEnumFunc, NULL);
printf("%d\n", cnt);
}
return 0;
}