C语言 随机位置显示窗口 (窗口病毒)

这篇博客详细介绍了如何使用 Win32 API 在 Windows 上创建多个窗口。通过包含必要的头文件、定义全局变量和窗口过程函数、注册窗口类并创建多个窗口,最终实现了在屏幕上随机分布多个窗口的效果。每个窗口的中心都有一个信息图标。博客还提供了完整的代码示例和运行结果展示。

如果你对 Windows 编程感兴趣,这篇博客将是一个很好的入门指南。你可以学习到如何处理窗口消息、绘制图标以及创建多个窗口等基本操作。希望这些内容对你有所帮助!如果你有任何问题或需要进一步的帮助,请随时告诉我。

说明

  1. 依赖库:本文使用了 Win32 API 库,包含在 windows.h 头文件中。
  2. 编译环境:确保你的编译器支持 Win32 API,例如 Visual Studio 或 MinGW。

步骤:

步骤1:包含必要的头文件
#include <windows.h>

这个头文件包含了我们需要的大部分Win32 API函数和类型。

步骤2:定义全局变量
HINSTANCE hInstance;
LPCSTR szClassName = "MyWindowClass"; 

hInstance 是当前程序的实例句柄,szClassName 是我们将要注册的窗口类的名称。

步骤3:定义窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);

                // Draw an icon at the center of the window
                int x = (GetSystemMetrics(SM_CXSCREEN) - GetSystemMetrics(SM_CXICON)) / 2;
                int y = (GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYICON)) / 2;
                DrawIcon(hdc, x, y, LoadIcon(NULL, IDI_INFORMATION));

                EndPaint(hwnd, &ps);
            }
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

这个函数处理窗口接收到的所有消息。在这个函数中,我们处理了 WM_CLOSEWM_DESTROY 和 WM_PAINT 三种消息。

步骤4:定义 WinMain 函数
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
    hInstance = hInst;

    // Register the window class
    WNDCLASS wc = { 0 };
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = szClassName;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    if (!RegisterClass(&wc)) {
        MessageBox(NULL, "Window Registration Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Create multiple windows
    for (int i = 0; i < 3; ++i) {  // Change 3 to the desired number of windows
        int windowWidth = 640;
        int windowHeight = 480;

        int randX = rand() % (GetSystemMetrics(SM_CXSCREEN) - windowWidth);
        int randY = rand() % (GetSystemMetrics(SM_CYSCREEN) - windowHeight);

        HWND hwnd = CreateWindow(szClassName, "My Window", WS_OVERLAPPEDWINDOW, randX, randY,
            windowWidth, windowHeight, NULL, NULL, hInstance, NULL);

        if (hwnd == NULL) {
            MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }

        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    }

    // Message loop
    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

在 WinMain 函数中,我们首先注册窗口类,然后创建多个窗口,并进入消息循环。

完整代码如下:

#include <windows.h>

HINSTANCE hInstance;
LPCSTR szClassName = "MyWindowClass"; 

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);

                // Draw an icon at the center of the window
                int x = (GetSystemMetrics(SM_CXSCREEN) - GetSystemMetrics(SM_CXICON)) / 2;
                int y = (GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYICON)) / 2;
                DrawIcon(hdc, x, y, LoadIcon(NULL, IDI_INFORMATION));

                EndPaint(hwnd, &ps);
            }
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {
    hInstance = hInst;

    // Register the window class
    WNDCLASS wc = { 0 };
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = szClassName;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    if (!RegisterClass(&wc)) {
        MessageBox(NULL, "Window Registration Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Create multiple windows
    for (int i = 0; i < 100; ++i) {  // Change 3 to the desired number of windows
        int windowWidth = 100;
        int windowHeight = 120;

        int randX = rand() % (GetSystemMetrics(SM_CXSCREEN) - windowWidth);
        int randY = rand() % (GetSystemMetrics(SM_CYSCREEN) - windowHeight);

        HWND hwnd = CreateWindow(szClassName, "My Window", WS_OVERLAPPEDWINDOW, randX, randY,
            windowWidth, windowHeight, NULL, NULL, hInstance, NULL);

        if (hwnd == NULL) {
            MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }

        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    }

    // Message loop
    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

运行结果

运行上述代码后,你将看到多个窗口随机分布在屏幕上,每个窗口的中心都有一个信息图标。

其他文章推荐

相关类型推荐

  • Win32 API 编程指南
  • Windows 编程实例教程

扩展

  1. 窗口样式和属性:了解更多关于窗口样式(如无边框窗口、工具窗口等)和属性(如透明度、置顶等)的设置。
  2. 消息处理:深入学习如何处理更多的窗口消息,如键盘输入、鼠标事件等。
  3. 多线程编程:在创建多个窗口时,如何使用多线程来提高程序的响应速度和性能。
  4. 自定义控件:学习如何创建和使用自定义控件,如按钮、文本框等。

Windows 操作推荐

  1. Win32 API 编程指南:全面介绍 Win32 API 的使用,包括窗口管理、图形绘制、文件操作等。
  2. Windows 编程实例教程:通过实例学习 Windows 编程的各个方面,从基础到高级应用。
  3. Windows 界面设计:学习如何设计和实现用户友好的 Windows 界面,包括布局、配色、交互设计等。
  4. 高级 Windows 编程:深入探讨 Windows 编程中的高级技术,如钩子、DLL、COM 等。

结论

以上就是如何在 Windows 上创建多个窗口的方法。通过这些步骤,你可以创建多个窗口,并在每个窗口中展示内容或与用户交互。

总结

通过本文,我们学习了如何使用 Win32 API 在 Windows 上创建多个窗口。我们首先定义了窗口过程函数,然后在 WinMain 函数中注册窗口类并创建多个窗口。最后,我们进入消息循环,处理窗口消息。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LIY若依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值