menu
30 文章
3 评论
86269 浏览
1 当前访客
ღゝ◡╹)ノ

开源一个表白小程序

#define UNICODE
#define _UNICODE
#include <windows.h>
#include <windowsx.h>
#include <string>
#include <commctrl.h>
#include <d2d1.h>
#include <dwrite.h>
#include <math.h>
#include <shellapi.h>
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "msimg32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(linker, "/ENTRY:wWinMainCRTStartup")
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")

HWND hHeartLabel, hQuestionLabel, hYesButton, hNoButton, hAuthorLabel, hStartBtn;
HFONT hTitleFont, hButtonFont, hHeartFont, hQuestionFont, hAuthorFont;
HBRUSH hBgBrush;
COLORREF bgColor = RGB(250, 240, 245);
HCURSOR hHandCursor = nullptr;
ID2D1Factory* pD2DFactory = nullptr;
ID2D1HwndRenderTarget* pRenderTarget = nullptr;
ID2D1SolidColorBrush* pBrush = nullptr;
IDWriteFactory* pDWriteFactory = nullptr;
bool isAnimating = false;
int animationStep = 0;
bool desktopIconsHidden = false;

const int WINDOW_WIDTH = 600;
const int WINDOW_HEIGHT = 700;

void ToggleDesktopIcons(bool show) {
    HWND hDesktop = FindWindow(L"Progman", L"Program Manager");
    if (hDesktop) {
        HWND hDesktopIcons = FindWindowEx(hDesktop, NULL, L"SHELLDLL_DefView", NULL);
        if (hDesktopIcons) {
            ShowWindow(hDesktopIcons, show ? SW_SHOW : SW_HIDE);
            UpdateWindow(hDesktopIcons);
        }
    }
}

void InitD2D(HWND hwnd) {
    if (FAILED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory))) {
        return;
    }

    RECT rc;
    GetClientRect(hwnd, &rc);

    if (FAILED(pD2DFactory->CreateHwndRenderTarget(
        D2D1::RenderTargetProperties(),
        D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)),
        &pRenderTarget
    ))) {
        return;
    }

    if (pRenderTarget) {
        pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::HotPink, 0.8f), &pBrush);
    }
}

HFONT CreateGradientFont(int height, const wchar_t* faceName, bool bold = false) {
    LOGFONTW lf = { 0 };
    lf.lfHeight = height;
    lf.lfWeight = bold ? FW_BOLD : FW_NORMAL;
    lf.lfQuality = CLEARTYPE_NATURAL_QUALITY;
    wcscpy_s(lf.lfFaceName, LF_FACESIZE, faceName);
    return CreateFontIndirectW(&lf);
}

void CenterControl(HWND hwnd, HWND hControl, int yPos, int width, int height) {
    RECT rc;
    GetClientRect(hwnd, &rc);
    int xPos = (rc.right - width) / 2;
    SetWindowPos(hControl, NULL, xPos, yPos, width, height, SWP_NOZORDER);
}

void DrawParticles(HWND hwnd) {
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);

    HDC hdcMem = CreateCompatibleDC(hdc);
    HBITMAP hbmMem = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
    SelectObject(hdcMem, hbmMem);

    RECT rc = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
    FillRect(hdcMem, &rc, hBgBrush);

    for (int i = 0; i < 50; i++) {
        int x = rand() % WINDOW_WIDTH;
        int y = rand() % WINDOW_HEIGHT;
        int size = 2 + rand() % 4;
        COLORREF color = RGB(255, 180 + rand() % 60, 200 + rand() % 40);
        HBRUSH hParticleBrush = CreateSolidBrush(color);
        SelectObject(hdcMem, hParticleBrush);
        Ellipse(hdcMem, x, y, x + size, y + size);
        DeleteObject(hParticleBrush);
    }

    BitBlt(hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCCOPY);

    DeleteObject(hbmMem);
    DeleteDC(hdcMem);
    EndPaint(hwnd, &ps);
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_CREATE: {
        ToggleDesktopIcons(false);
        desktopIconsHidden = true;

        hHandCursor = LoadCursor(NULL, IDC_HAND);

        InitD2D(hwnd);

        hTitleFont = CreateGradientFont(42, L"微软雅黑", true);
        hButtonFont = CreateGradientFont(20, L"微软雅黑");
        hHeartFont = CreateGradientFont(120, L"Segoe UI Emoji");
        hQuestionFont = CreateGradientFont(26, L"微软雅黑", true);
        hAuthorFont = CreateGradientFont(16, L"微软雅黑");

        HWND hTitle = CreateWindowW(
            L"STATIC", L"❤ 致某某某 ❤",
            WS_VISIBLE | WS_CHILD | SS_CENTER,
            0, 40, WINDOW_WIDTH, 70,
            hwnd, NULL, NULL, NULL
        );
        SendMessageW(hTitle, WM_SETFONT, (WPARAM)hTitleFont, TRUE);

        hStartBtn = CreateWindowW(
            L"BUTTON", L"✨ 点击看看 ✨",
            WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_CENTER,
            0, 130, 180, 50,
            hwnd, (HMENU)1, NULL, NULL
        );
        CenterControl(hwnd, hStartBtn, 130, 180, 50);
        SendMessageW(hStartBtn, WM_SETFONT, (WPARAM)hButtonFont, TRUE);

        hHeartLabel = CreateWindowW(
            L"STATIC", L"",
            WS_CHILD | SS_CENTER,
            0, 200, WINDOW_WIDTH, 140,
            hwnd, NULL, NULL, NULL
        );
        SendMessageW(hHeartLabel, WM_SETFONT, (WPARAM)hHeartFont, TRUE);

        hQuestionLabel = CreateWindowW(
            L"STATIC", L"✨ 我喜欢你,可以做我的恋人吗? ✨",
            WS_CHILD | SS_CENTER,
            0, 350, WINDOW_WIDTH, 50,
            hwnd, NULL, NULL, NULL
        );
        SendMessageW(hQuestionLabel, WM_SETFONT, (WPARAM)hQuestionFont, TRUE);

        hYesButton = CreateWindowW(
            L"BUTTON", L"💖 我愿意 💖",
            WS_CHILD | BS_PUSHBUTTON | BS_CENTER,
            0, 420, 180, 50,
            hwnd, (HMENU)2, NULL, NULL
        );
        CenterControl(hwnd, hYesButton, 420, 180, 50);
        SendMessageW(hYesButton, WM_SETFONT, (WPARAM)hButtonFont, TRUE);

        hNoButton = CreateWindowW(
            L"BUTTON", L"✨ 再考虑 ✨",
            WS_CHILD | BS_PUSHBUTTON | BS_CENTER,
            0, 490, 180, 50,
            hwnd, (HMENU)3, NULL, NULL
        );
        CenterControl(hwnd, hNoButton, 490, 180, 50);
        SendMessageW(hNoButton, WM_SETFONT, (WPARAM)hButtonFont, TRUE);

        hAuthorLabel = CreateWindowW(
            L"STATIC", L"©钦州的一个辣呆",
            WS_VISIBLE | WS_CHILD | SS_CENTER,
            0, 580, WINDOW_WIDTH, 30,
            hwnd, NULL, NULL, NULL
        );
        SendMessageW(hAuthorLabel, WM_SETFONT, (WPARAM)hAuthorFont, TRUE);

        ShowWindow(hHeartLabel, SW_HIDE);
        ShowWindow(hQuestionLabel, SW_HIDE);
        ShowWindow(hYesButton, SW_HIDE);
        ShowWindow(hNoButton, SW_HIDE);

        hBgBrush = CreateSolidBrush(bgColor);
        break;
    }
    case WM_SETCURSOR: {
        if (LOWORD(lParam) == HTCLIENT) {
            POINT pt;
            GetCursorPos(&pt);
            ScreenToClient(hwnd, &pt);
            HWND hChild = ChildWindowFromPoint(hwnd, pt);
            if (hChild == hStartBtn || hChild == hYesButton || hChild == hNoButton) {
                SetCursor(hHandCursor);
                return TRUE;
            }
        }
        break;
    }
    case WM_CTLCOLORSTATIC: {
        HDC hdc = (HDC)wParam;
        HWND hStatic = (HWND)lParam;
        if (hStatic == hAuthorLabel) {
            SetTextColor(hdc, RGB(100, 100, 100));
        }
        else {
            SetTextColor(hdc, RGB(220, 20, 100));
        }
        SetBkMode(hdc, TRANSPARENT);
        return (LRESULT)hBgBrush;
    }
    case WM_CTLCOLORBTN: {
        return (LRESULT)hBgBrush;
    }
    case WM_COMMAND: {
        if (LOWORD(wParam) == 1) {
            isAnimating = true;
            animationStep = 0;
            SetTimer(hwnd, 1, 30, NULL);

            MessageBoxW(
                hwnd,
                L"✨ 我喜欢你! ✨\n\n"
                L"从第一次见到你,我的心就被你俘获了。\n"
                L"你是我生命中最美的风景,\n"
                L"我想和你一起走过每一个春夏秋冬。",
                L"💌 爱的告白 💌",
                MB_OK
            );

            ShowWindow(hStartBtn, SW_HIDE);

            SetWindowTextW(hHeartLabel, L"❤");
            ShowWindow(hHeartLabel, SW_SHOW);
            ShowWindow(hQuestionLabel, SW_SHOW);
            ShowWindow(hYesButton, SW_SHOW);
            ShowWindow(hNoButton, SW_SHOW);
        }
        else if (LOWORD(wParam) == 2) {
            for (int i = 0; i < 5; i++) {
                SetWindowTextW(hHeartLabel, L"❤❤");
                Sleep(150);
                SetWindowTextW(hHeartLabel, L"💕💕");
                Sleep(150);
                SetWindowTextW(hHeartLabel, L"💖💖");
                Sleep(150);
            }

            MessageBoxW(
                hwnd,
                L"💝 太棒了!我会永远珍惜你! 💝\n\n"
                L"从今以后,你就是我的全世界。\n"
                L"我会用一生的时间去爱你、呵护你。",
                L"🎉 太开心了 🎉",
                MB_OK
            );

            ShellExecuteW(NULL, L"open", L"https://www.napan.top", NULL, NULL, SW_SHOWNORMAL);
            DestroyWindow(hwnd);
        }
        else if (LOWORD(wParam) == 3) {
            for (int i = 0; i < 3; i++) {
                SetWindowTextW(hHeartLabel, L"💔");
                Sleep(300);
                SetWindowTextW(hHeartLabel, L"❤");
                Sleep(300);
            }
            SetWindowTextW(hHeartLabel, L"💔");

            MessageBoxW(
                hwnd,
                L"我会继续努力的!\n\n"
                L"也许现在的我还不够好,\n"
                L"但我会不断进步,直到能配得上你。",
                L"💪 我会努力 💪",
                MB_OK
            );
            DestroyWindow(hwnd);
        }
        break;
    }
    case WM_TIMER: {
        if (wParam == 1) {
            if (animationStep < 20) {
                bgColor = RGB(250, 240 - animationStep * 5, 245 - animationStep * 2);
                DeleteObject(hBgBrush);
                hBgBrush = CreateSolidBrush(bgColor);
                InvalidateRect(hwnd, NULL, TRUE);
                animationStep++;
            }
            else {
                KillTimer(hwnd, 1);
                isAnimating = false;
            }
        }
        break;
    }
    case WM_PAINT: {
        if (isAnimating) {
            DrawParticles(hwnd);
        }
        else {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            FillRect(hdc, &ps.rcPaint, hBgBrush);
            EndPaint(hwnd, &ps);
        }
        break;
    }
    case WM_DESTROY: {
        if (desktopIconsHidden) {
            ToggleDesktopIcons(true);
        }

        if (pBrush) pBrush->Release();
        if (pRenderTarget) pRenderTarget->Release();
        if (pD2DFactory) pD2DFactory->Release();

        DeleteObject(hTitleFont);
        DeleteObject(hButtonFont);
        DeleteObject(hHeartFont);
        DeleteObject(hQuestionFont);
        DeleteObject(hAuthorFont);
        DeleteObject(hBgBrush);

        if (hHandCursor) {
            DestroyCursor(hHandCursor);
        }

        PostQuitMessage(0);
        return 0;
    }
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_STANDARD_CLASSES | ICC_BAR_CLASSES;
    InitCommonControlsEx(&icex);

    WNDCLASSW wc = { 0 };
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = L"LoveWindow";
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    RegisterClassW(&wc);

    HWND hwnd = CreateWindowW(
        L"LoveWindow", L"💖 Love confession program 💖",
        WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
        CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT,
        NULL, NULL, hInstance, NULL
    );

    if (!hwnd) return 0;

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

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

tup

这是今天用C语言编写的一款“表白”小程序源代码,可以自己继续脑补加功能.....

☆实属无聊的作品。


🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨🤨


标题:开源一个表白小程序
作者:钦州的一个辣呆
地址:https://www.napan.top/articles/2025/10/06/1765719896355.html

评论
取消