1. 程式人生 > 實用技巧 >(程式碼雨/屏保)c++百行程式碼,超簡單

(程式碼雨/屏保)c++百行程式碼,超簡單

(程式碼雨/屏保)c++程式碼

基於Easyx,

在vs端使用,

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

也就百行而已。

程式碼如下:

#include<graphics.h>
#include<cstdio>
#include<Windows.h>
#include<cstring>
#define N 50
#define M 10
int X, Y, n, choice;
char SS[9][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 < 3
+ 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==1)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("**************\n"); scanf_s("%d", &choice); } while (choice != 1 && choice != 2); if (choice == 1) { puts("輸入名字個數"); scanf_s("%d", &n); puts("輸入名字,格式如:張 二 蛋"); 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 = M - 3; 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,並不算是屏保) ///* 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; }

此放所用函式說明網址:

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