(透明代碼雨/屏保)c++百行代碼,超簡單


極簡 代碼雨/屏保 c++代碼,原創。

此代碼雨也可以作秀告白。

基於Easyx,

在vs端使用,

若是其他編譯器自行修改scanf等函數。

也就   百行   而已。

先放效果圖吧:

 

 

 

 

 

 

 

 

 

 

 

若想直接體驗,

百度網盤軟件如下:

鏈接:https://pan.baidu.com/s/1h_k7K863o55Z7q6lhkuQWw
提取碼:1234

 ------------------------------------------- 重大更新 -------------------------------------------

2020.12.2——如果vs報錯,outtextxy無法找到重構,

解決辦法:項目——屬性

1.頂部平台(改為所有平台)

2.左側配置屬性——高級——字符集(改為使用多節字符集)

------------------------------------------- 重大更新 -------------------------------------------

 

代碼如下:

#include<graphics.h>
#include<cstdio>
#include<Windows.h>
#include<cstring>
#define N 50
#define M 10
int X, Y, n, choice;
char SS[20][9];
struct pt
{
    TCHAR s[M];
    int x, y;
    int a, b, c;
}S[N];
void Init(int i)
{
    for (int k = 0; k <= M; ++k)
    {
        if (rand() % 3)
        {
            S[i].s[k] = 'a' + rand() % 26;
        }
        else S[i].s[k] = '0' + rand() % 9;
    }
    S[i].x = rand() % X;
    S[i].y = rand() % Y;
    if (!rand() % 3)S[i].y = 0;
    do
    {
        S[i].a = rand() % 240;
        S[i].b = rand() % 240;
        S[i].c = rand() % 240;
    } while (S[i].a + S[i].b + S[i].c < 510);
}
void out_s()
{
    int i, k;
    for (i = 0, k = 0; i < N; ++i, k = 0)
    {
        settextcolor(RGB(255, 255, 255));
        outtextxy(S[i].x, S[i].y + k * 16, S[i].s[k]);
        for (; k < n; ++k)
        {
            int x = (M - k + 1) / (double)M * S[i].a;
            int y = (M - k + 1) / (double)M * S[i].b;
            int z = (M - k + 1) / (double)M * S[i].c;
            settextcolor(RGB(x, y, z));
            if(choice!=3)outtextxy(S[i].x, S[i].y + k * 16, SS[k]);
            else outtextxy(S[i].x, S[i].y + k * 16, S[i].s[k]);
        }
        S[i].y++;
        if (S[i].y > Y)
            Init(i);
    }
}
int main()
{
    do
    {
        system("cls");
        puts("\n輸入選擇:");
        printf_s("**************\n");
        printf_s("|   1.告白   |\n");
        printf_s("|   2.自由   |\n");
        printf_s("|   3.耍帥   |\n");
        printf_s("**************\n");
        scanf_s("%d", &choice);
    } while (choice != 1 && choice != 2 && choice != 3);
    if (choice == 1)
    {
        puts("輸入名字個數");
        scanf_s("%d", &n);
        puts("輸入名字,格式如:張 二 蛋\n每個字之間必須要空格");
        for (int i = 0; i < n; ++i)
            scanf_s("%s", SS[i],10);
        strcpy_s(SS[n], "");
        strcpy_s(SS[n + 1], "");
        strcpy_s(SS[n + 2], "");
        n += 3;
    }
    else if (choice == 2)
    {
        puts("先輸入所想語句的字數,20字以內");
        scanf_s("%d", &n);
        puts("輸入語句,格式如:張 二 蛋\n每個字之間必須要空格");
        for (int i = 0; i < n; ++i)
            scanf_s("%s", SS[i], 10);
    }
    else n = M;
    int Choice;
    do
    {
        system("cls");
        puts("\n請選擇模式:");
        printf_s("**************\n");
        printf_s("|   1.慕雨   |\n");
        printf_s("|   2.屏保   |\n");
        printf_s("**************\n");
        puts("若要關掉屏保模式,請按開始鍵,在任務欄關掉程序嗷");
        scanf_s("%d", &Choice);
    } while (Choice != 1 && Choice != 2);
    X = GetSystemMetrics(SM_CXSCREEN);
    Y = GetSystemMetrics(SM_CYSCREEN);
    HWND hwnd = initgraph(X, Y);// 獲取窗口句柄
    for (int i = 0; i < N; ++i)Init(i);
    //SetWindowText(hwnd, "Hello!");// 設置窗口標題文字
    SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) - WS_CAPTION);//透明框架
    SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, X, Y, SWP_SHOWWINDOW);//定位
    
    //屏保模式注意:
    //要返回界面就  按開始鍵  在  任務欄  關掉程序
    //要返回界面就  按開始鍵  在  任務欄  關掉程序
    //要返回界面就  按開始鍵  在  任務欄  關掉程序
    //下面這部分注釋掉就變成屏保了(實際占用偏高cpu,並不算是屏保)
    ///*
    if (Choice == 1)
    {
        LONG WindowLong = GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED;
        SetWindowLong(hwnd, GWL_EXSTYLE, WindowLong);//窗口風格
        SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 0, LWA_COLORKEY);//透明底色
    }
    //*/

    while (1)
    {
        BeginBatchDraw();
        out_s();
        EndBatchDraw();
        Sleep(5);
        cleardevice();
    }
    return 0;
}
c++

2020.8.10——(更新)1.添加20以內的自由模式,提高功能性  2.添加屏保選項,不用改代碼直接選擇。沒有更新圖片,都差不多,沒必要。

此放所用函數說明網址:

1.GetSystemMetrics,我在前博客Easyx基礎里發過說明

2.SetWindowLong,https://baike.baidu.com/item/SetWindowLong/6375538?fr=aladdin

3.GetWindowLong,https://blog.csdn.net/hnhyhongmingjiang/article/details/2154410 + https://baike.baidu.com/item/GetWindowLong/6375452?fr=aladdin

4.WS_CAPTION,https://blog.csdn.net/weixin_36929359/article/details/54342859

5.SetWindowPos,https://baike.baidu.com/item/SetWindowPos/6376849?fr=aladdin

6.WS_EX_LAYERED,https://blog.csdn.net/tyhjtw/article/details/38107577

7.SetLayeredWindowAttributes,https://baike.baidu.com/item/SetLayeredWindowAttributes/10134503


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM