一个恶搞 C++ 代码(仅供娱乐)

考虑用 windows.h 内部的函数模拟按键,发送按 Enter 就行了。
代码:

#include"games.h"//自定义头文件,主要用于读入和输出,去掉也不影响
#include<windows.h>
#include<stdio.h>
#define el putchar('\n')
bool SetClipboardText(const std::wstring& text) {
    if (!OpenClipboard(nullptr)) {
        write_string_pf("打开剪贴板失败", 0);
        return false;
    }
    EmptyClipboard();
    size_t size = (text.size() + 1) * sizeof(wchar_t);
    HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, size);
    if (!hMem) {
        write_string("分配全局内存失败", 0);
        CloseClipboard();
        return false;
    }
    wchar_t* pMem = (wchar_t*)GlobalLock(hMem);
    if (pMem) {
        wcsncpy(pMem, text.c_str(), text.size() + 1);
        GlobalUnlock(hMem);
        if (SetClipboardData(CF_UNICODETEXT, hMem) == nullptr) {
            write_string_pf("设置剪贴板数据失败", 0);
            GlobalFree(hMem);
            CloseClipboard();
            return false;
        }
    } else {
        write_string_pf("锁定内存失败", 0);
        GlobalFree(hMem);
        CloseClipboard();
        return false;
    }
    CloseClipboard();
    return true;
}
void SendPaste() {
    INPUT inputs[4] = {};
    inputs[0].type = INPUT_KEYBOARD;
    inputs[0].ki.wVk = VK_CONTROL;
    inputs[1].type = INPUT_KEYBOARD;
    inputs[1].ki.wVk = 'V';
    inputs[2].type = INPUT_KEYBOARD;
    inputs[2].ki.wVk = 'V';
    inputs[2].ki.dwFlags = KEYEVENTF_KEYUP;
    inputs[3].type = INPUT_KEYBOARD;
    inputs[3].ki.wVk = VK_CONTROL;
    inputs[3].ki.dwFlags = KEYEVENTF_KEYUP;

    UINT uRet = SendInput(4, inputs, sizeof(INPUT));
    if (uRet != 4) {
        if (uRet == 0) {
            write_string_pf("Error:", 0);
            write(GetLastError());
            el;
        }
    }
}
void send_str(const std::wstring& s) {
    if (!SetClipboardText(s)) {
        write_string_pf("Error");
        return;
    }
    SendPaste();
}
void send_str(const std::string& s) {
    int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, nullptr, 0);
    if (len == 0) {
        write_string_pf("Error");
        return;
    }
    std::wstring ws(len, L'\0');
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, &ws[0], len);
    ws.pop_back();
    send_str(ws);
}
void SendText() {
    std::vector<INPUT> inputs;
    INPUT ipEnter = {0};
    ipEnter.type = INPUT_KEYBOARD;
    ipEnter.ki.wVk = VK_RETURN;
    inputs.push_back(ipEnter);
    ipEnter.ki.dwFlags = KEYEVENTF_KEYUP;
    inputs.push_back(ipEnter);
    SendInput(inputs.size(), inputs.data(), sizeof(INPUT));
}
int main(){
	HWND win=FindWindowW(NULL,L"QQ");
	if(win==NULL){
		printf("未找到窗口句柄\n");
		return 0;
	}
	string s;
	int n,t;
	printf("输入发送的字符串:");
	getline(cin,s);
	write_string_pf(s);
	printf("请确认...");
	getch(); 
	printf("\n输入发送次数:");
	read(n);
	printf("\n输入间隔(ms):");
	read(t);
	if (IsIconic(win)){
	    ShowWindow(win,SW_RESTORE);
	}
	BOOL ret=SetForegroundWindow(win);
	if(!ret){
		write_string("Error:");
		write(GetLastError());
		el;
	}
	SetFocus(win);
	Sleep(100);
	for(int i=0;i<n;i++){
		send_str(s);
		Sleep(t/2);
		SendText();
		Sleep(t/2);
	}
	return 0;
}

另注:部分使用 AI 生成,但保证本人贡献大于 AI

posted @ 2026-08-20 20:58  sqrt2  阅读(38)  评论(0)    收藏  举报