C语言代码

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// 修改为鼓励自己、爱自己的积极话语
const char* tips[] = {
    "好好爱自己", "多喝水哦~", "保持微笑呀", "每天都要元气满满",
    "记得吃水果", "保持好心情", "梦想成真", "顺顺利利", 
    "早点休息", "愿所有烦恼都消失", "别熬夜", "今天过得开心嘛", 
    "天冷了,多穿衣服", "你是最棒的", "相信自己", "一切都会好的",
    "深呼吸,放松", "今天也要加油", "你很特别", "接纳不完美的自己",
    "给自己一个拥抱", "你已经做得很好了", "慢慢来,不着急",
    "坚持就是胜利", "感恩今天", "活在当下", "未来可期",
    "心若向阳,无畏悲伤", "每一天都是新的开始", "做自己的太阳"
};

int tipCount = 30;

// 窗口过程函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_PAINT: {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            
            // 积极向上的颜色方案
            COLORREF colors[] = {
                RGB(255, 182, 193), // 浅粉色 - 温柔
                RGB(135, 206, 235), // 天蓝色 - 宁静
                RGB(144, 238, 144), // 浅绿色 - 生机
                RGB(255, 255, 224), // 浅黄色 - 温暖
                RGB(230, 230, 250), // 薰衣草色 - 平和
                RGB(175, 238, 238), // 淡蓝色 - 清新
                RGB(152, 251, 152), // 淡绿色 - 希望
                RGB(255, 228, 196), // 桃色 - 温馨
                RGB(240, 255, 240), // 蜜白色 - 纯净
                RGB(255, 250, 205)  // 柠檬绸 - 明亮
            };
            
            HBRUSH hBrush = CreateSolidBrush(colors[rand() % 10]);
            FillRect(hdc, &ps.rcPaint, hBrush);
            DeleteObject(hBrush);
            
            // 设置文字
            SetTextColor(hdc, RGB(0, 0, 0));
            SetBkMode(hdc, TRANSPARENT);
            
            // 创建字体
            HFONT hFont = CreateFont(20, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, 
                                    GB2312_CHARSET, OUT_DEFAULT_PRECIS,
                                    CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                                    DEFAULT_PITCH | FF_SWISS, "宋体");
            SelectObject(hdc, hFont);
            
            // 绘制文字
            RECT rect;
            GetClientRect(hwnd, &rect);
            const char* tip = (const char*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
            DrawTextA(hdc, tip, -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            
            EndPaint(hwnd, &ps);
            return 0;
        }
        
        case WM_LBUTTONDOWN:
            DestroyWindow(hwnd);
            return 0;
            
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int main() {
    srand((unsigned)time(NULL));
    
    // 注册窗口类
    WNDCLASSEX wc = {0};
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpszClassName = "TipWindow";
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    RegisterClassEx(&wc);
    
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    
    printf("屏幕尺寸: %dx%d\n", screenWidth, screenHeight);
    printf("开始创建自我关爱提醒窗口...\n");
    
    // 窗口尺寸
    int windowWidth = 320, windowHeight = 120;
    
    // 创建窗口数量
    int windowCount = 50;
    
    printf("将创建 %d 个自我关爱提醒窗口\n", windowCount);
    
    // 创建多个窗口,使用完全随机位置
    for (int i = 0; i < windowCount; i++) {
        // 完全随机位置,确保窗口在屏幕内
        int x = rand() % (screenWidth - windowWidth);
        int y = rand() % (screenHeight - windowHeight);
        
        const char* tip = tips[rand() % tipCount];
        
        HWND hwnd = CreateWindowEx(
            WS_EX_TOPMOST, "TipWindow", "自我关爱提醒",
            WS_POPUP | WS_CAPTION | WS_SYSMENU,
            x, y, windowWidth, windowHeight, NULL, NULL, GetModuleHandle(NULL), NULL
        );
        
        if (hwnd) {
            SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)tip);
            ShowWindow(hwnd, SW_SHOW);
            UpdateWindow(hwnd);
        }
        
        // 稍微延迟
        Sleep(10);
    }
    
    printf("所有自我关爱提醒窗口创建完成!点击任意窗口可关闭\n");
    printf("记得好好爱自己!\n");
    
    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    
    return 0;
}

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部