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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现