弹跳窗口病毒

效果:使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;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值