(一)源代码:
#include<iostream>
#include<windows.h>
#include<string>
using namespace std;
int main()
{
string text="Welcome!Welcome!Backhome!Backhome!";
int screenwidth=10;
int position=0;
CONSOLE_SCREEN_BUFFER_INFO csbi;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN+FOREGROUND_INTENSITY);
if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&csbi))
screenwidth=csbi.dwSize.X;
while(1)
{
system("cls");
string displaytext=text.substr(position)+text.substr(0,position);
if(displaytext.length()>static_cast<size_t>(screenwidth))
displaytext=displaytext.substr(0,screenwidth);
cout<<displaytext;
position=(position+1)%text.length();
Sleep(500);
}
return 0;
}
(二)代码运行窗口:
读者朋友可在IDE中编写代码,再编译程序,最后运行程序,即可看到滚动播放的字幕。