跳到正文

个人记录

winpaoku

zhouhaoGG约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年7月26日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 6 分钟

版本

  1. v1当前
C++
#include<iostream>
#include<windows.h>
#include<vector>
using namespace std;
HWND hWnd=GetForegroundWindow();
int srcw=GetSystemMetrics(SM_CXSCREEN);
int srch=GetSystemMetrics(SM_CYSCREEN);
bool key[4];//WSAD
POINT mousepos;
void getkey(){
	key[0]=(GetAsyncKeyState('W')|GetAsyncKeyState(VK_UP))&0x8000;
	key[1]=(GetAsyncKeyState('S')|GetAsyncKeyState(VK_DOWN))&0x8000;
	key[2]=(GetAsyncKeyState('A')|GetAsyncKeyState(VK_LEFT))&0x8000;
	key[3]=(GetAsyncKeyState('D')|GetAsyncKeyState(VK_RIGHT))&0x8000;
}
bool inline inside(int x,int y,int w,int h,int mx,int my){
	return mx>=x&&mx<x+w&&my>=y&&my<y+h;
}
bool inline inside(int x,int l,int r){
	return x>l&&x<r;
}
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam){
	vector<HWND>* data=reinterpret_cast<vector<HWND>*>(lParam);
	data->push_back(hwnd);
	return TRUE;
}
vector<RECT> windowsRect;
vector<HWND> hwnds;
void getWindowsRect(){
	windowsRect.clear();
	//static vector<HWND> hwnds;
	hwnds.clear();RECT rect;
	EnumWindows(EnumWindowsProc,(LPARAM)&hwnds);
	for(int i=0; i<hwnds.size(); i++) {
		HWND hwnd=hwnds[i];
		GetWindowRect(hwnd,&rect);

		char buf[256]={0};
		GetWindowText(hwnd,buf,sizeof(buf));
		LONG style=GetWindowLong(hwnd,GWL_STYLE);
		LONG exStyle=GetWindowLong(hwnd,GWL_EXSTYLE);

		bool isValid=
			IsWindowVisible(hwnd)&&		// 排除不可见窗口
			!IsIconic(hwnd)&&			// 排除最小化窗口
			(style&WS_VISIBLE)&&		// 必须包含可见样式
			!(style&WS_MINIMIZE)&&		// 排除最小化样式
			(rect.right-rect.left>5)&&	// 增加宽度阈值
			(rect.bottom-rect.top>5)&&	// 增加高度阈值
			(buf[0]!='\0')&&
			(hwnd!=hWnd);

		if(isValid) windowsRect.push_back(rect);
	}
}
int collideY(int x,int y,int w,int h){
	if(y<0) return 0;
	if(y+h>srch) return srch-h;
	for(auto& r:windowsRect){
		if(inside(x,r.left,r.right)||inside(x+w,r.left,r.right)){
			if(inside(r.top,y,y+h)){
				if(y+h/2>r.top) return r.top;
				return r.top-h;
			}else if(inside(r.bottom,y,y+h)){
				if(y+h/2<r.bottom) return r.bottom-h;
				return r.bottom;
			}
		}
	}
	return -114514;
}
int collideX(int x,int y,int w,int h){
	if(x<0) return 0;
	if(x+w>srcw) return srcw-w;
	for(auto& r:windowsRect){
		if(inside(r.top,y,y+h)||inside(r.bottom,y,y+h)){
			if(inside(r.left,x,x+w)) return r.left-w;
			else if(inside(r.right,x,x+w)) return r.right;
		}
	}
	return -114514;
}
int main(){
	cout<<"By zhou\nhaoGG!\n";
	float x=1,y=1,sx=0,sy=0,cx=200,cy=200,cyy=0;
	while(true){
		getkey();
		GetCursorPos(&mousepos);
		getWindowsRect();
		sx+=(key[3]-key[2]);sy+=0.5f;
		if(key[0])
			if(collideY(x,y+cyy,cx,cy-cyy+2)!=-114514||inside(x,y+cy*0.8f,cx,cy*0.2f,mousepos.x,mousepos.y-2))
				sy=-15;
		y+=sy;
		if(inside(x,y+cy*0.8f,cx,cy*0.2f,mousepos.x,mousepos.y-2)){ y=mousepos.y-cy;sy=0; }
		int c=collideY(x,y+cyy,cx,cy-cyy);
		if(c!=-114514){ y=c-cyy;sy=0; }
		x+=sx;
		if(inside(x,y+cy*0.2f,cx,cy*0.6f,mousepos.x,mousepos.y)){
			if(x+cx/2>mousepos.x) x=mousepos.x;
			else x=mousepos.x-cx;
			if(key[0]){ sy=-15;sx=sx<0?10:-10; }else sx=0;
		}
		c=collideX(x,y+cyy,cx,cy-cyy);
		if(c!=-114514){
			x=c;
			if(key[0]){ sy=-15;sx=sx<0?10:-10; }else sx=0;
		}
		sx*=0.9f;
		cyy+=(key[1]*cy/2-cyy)*0.5f;
		SetWindowPos(hWnd,HWND_TOPMOST,x,y+cyy,cx,cy-cyy,SWP_SHOWWINDOW);
		Sleep(20);
	}
	return 0;
}
原文