C++小案例之b站弹幕
效果:
窗口透明

源代码:
点击查看代码
#include<cstdio>
#include<graphics.h>
#include<iostream>
#include<vector>
using namespace std;
struct DM
{
char buff[256];//字符串
COLORREF color;//颜色
int x,y;//坐标
int speed;//移动速度
};
int countDM = 0;
//准备弹幕1024条
struct DM dm[1024];
void function() {
while (1) {
char buff[256];
cout << "请输入要发送的弹幕:" << endl;
cin >> buff;
strcpy_s(dm[countDM].buff, buff);
dm[countDM].x = 500;
dm[countDM].y = rand() % 200 + 200;//200-400随机
dm[countDM].color = RGB(rand() % 256, rand() % 256, rand() % 256);
dm[countDM].speed = rand() % 10 + 1;//1-10
countDM++;
}
}
int main() {
int w = GetSystemMetrics(SM_CXFULLSCREEN);//桌面的宽度
int h = GetSystemMetrics(SM_CYFULLSCREEN);//桌面的高度
HWND hwnd=initgraph(w/2, h/2,SHOWCONSOLE);
//窗口没有标题栏
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) - WS_CAPTION);
//设置窗口大小,位置,置顶
SetWindowPos(hwnd, HWND_TOPMOST,//置顶
0, 0,//窗口位置
w / 2, h / 2,SWP_SHOWWINDOW
);
//设置窗口透明
SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_EX_LAYERED);
//显示的时候作颜色运算
SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 0, LWA_COLORKEY);
CreateThread(NULL, NULL,(LPTHREAD_START_ROUTINE)function, NULL, NULL, NULL);
while (1) {
cleardevice();//清屏
for (int i = 0; i < countDM; i++) {
settextcolor(dm[i].color);
outtextxy(dm[i].x, dm[i].y, dm[i].buff);
dm[i].x -= dm[i].speed;
}
Sleep(200);
}
while (1);
return 0;
}
浙公网安备 33010602011771号