#include "danmu.h"
Danmu::Danmu(QWidget * parent, QString text, unsigned int color, int type, QFont danmufont, double transparency, int runtime)
:QLabel(parent),DText(text),color(color),type(type),danmuFont(danmufont),Transparency(transparency),runTime(runtime)
{
QDesktopWidget* desketopWidget;
desketopWidget=QApplication::desktop();
screenrect=desketopWidget->screenGeometry();
//QApplication::
QFontMetrics metrics(danmuFont);
palette=QPalette();
setMyPalette();
this->setPalette(palette); //设置调色盘
this->setFixedHeight(metrics.height()+5);
this->setFixedWidth(metrics.width(DText)+4);
int yy = qrand()%screenrect.height();
int y = yy<(screenrect.height()-metrics.height()-5)?(yy):(screenrect.height()-metrics.height()-5);
int xx = screenrect.width()+10;
this->move(xx,y);
this->setPosX(xx);//设置弹幕水平的位置
this->setPosY(y); //设置弹幕垂直位置
this->setWindowFlags(Qt::FramelessWindowHint|Qt::Tool|Qt::WindowStaysOnTopHint); //设置弹幕为无窗口无工具栏且呆在窗口顶端
this->setAttribute(Qt::WA_TranslucentBackground, true);
this->setFocusPolicy(Qt::NoFocus);
this->hide();
anim2=new QPropertyAnimation(this, "pos");
anim2->setDuration(runTime);
anim2->setStartValue(QPoint(PosX,PosY));
anim2->setEndValue(QPoint(-(this->width()), PosY));
anim2->setEasingCurve(QEasingCurve::Linear);
this->setWindowOpacity(Transparency);
this->show();
this->repaint();
}
void Danmu::paintEvent(QPaintEvent *)
{ //弹幕绘制函数
QPainter painter(this); //以弹幕窗口为画布
painter.save();
QFontMetrics metrics(danmuFont); //获取弹幕字体
QPainterPath path; //描绘路径用
QPen pen(QColor(0, 0, 0, 230)); //自定义画笔的样式,让文字周围有边框
painter.setRenderHint(QPainter::Antialiasing);
int penwidth = 4;
pen.setWidth(penwidth);
int len = metrics.width(DText);
int w = this->width();
int px = (len - w) / 2;
if(px < 0)
{
px = -px;
}
int py = (height() - metrics.height()) / 2 + metrics.ascent();
if(py < 0)
{
py = -py;
}
path.addText(px+2,py+2,danmuFont,DText); //画字体轮廓
painter.strokePath(path, pen);
painter.drawPath(path);
painter.fillPath(path, QBrush(qcolor)); //用画刷填充
painter.restore();
}
Danmu::~Danmu()
{
qDebug()<<"弹幕被析构"<<endl;
}
void Danmu::setPosX(int posx)
{
this->PosX = posx;
}
void Danmu::setPosY(int posy)
{
this->PosY = posy;
}
void Danmu::setQColor(QColor qcolor)
{
this->qcolor = qcolor;
}
void Danmu::setMyPalette()
{
switch (color) {
case WHITE:
palette.setColor(QPalette::WindowText,QColor(255,255,246,255));
this->setQColor(QColor(255,255,246,255));
break;
case RED:
palette.setColor(QPalette::WindowText,QColor(231,0,18,255));
this->setQColor(QColor(231,0,18,255));
break;
case YELLOW:
palette.setColor(QPalette::WindowText,QColor(254,241,2,255));
this->setQColor(QColor(254,241,2,255));
break;
case GREEN:
palette.setColor(QPalette::WindowText,QColor(0,152,67,255));
this->setQColor(QColor(0,152,67,255));
break;
case BLUE:
palette.setColor(QPalette::WindowText,QColor(0,160,234,255));
this->setQColor(QColor(0,160,234,255));
break;
case PINK:
palette.setColor(QPalette::WindowText,QColor(226,2,127,255));
this->setQColor(QColor(226,2,127,255));
break;
case GRASS:
palette.setColor(QPalette::WindowText,QColor(144,195,32,255));
this->setQColor(QColor(144,195,32,255));
break;
case DBLUE:
palette.setColor(QPalette::WindowText,QColor(0,46,114,255));
this->setQColor(QColor(0,46,114,255));
break;
case DYELLLOW:
palette.setColor(QPalette::WindowText,QColor(240,171,42,255));
this->setQColor(QColor(240,171,42,255));
break;
case DPURPLE:
palette.setColor(QPalette::WindowText,QColor(104,58,123,255));
this->setQColor(QColor(104,58,123,255));
break;
case LBLUE:
palette.setColor(QPalette::WindowText,QColor(129,193,205,255));
this->setQColor(QColor(129,193,205,255));
break;
case BROWN:
palette.setColor(QPalette::WindowText,QColor(149,119,57,255));
this->setQColor(QColor(149,119,57,255));
break;
default:
palette.setColor(QPalette::WindowText,QColor(255,255,246,255));
this->setQColor(QColor(255,255,246,255));
break;
}
}
void Danmu::enterEvent(QEvent *event)
{
anim2->stop();
anim2->setStartValue(QPoint(this->x(),PosY));
QWidget::enterEvent(event);
}
void Danmu::leaveEvent(QEvent *event)
{
runTime=(int)(runTime*((double)(this->x()+this->width())/(double)(screenrect.width()+10+this->width())));
anim2->setDuration(runTime);
anim2->start();
QWidget::enterEvent(event);
}
void Danmu::danmuLaunch()
{
anim2->start();
connect(anim2,SIGNAL(finished()),this,SLOT(deleteLater()));
}
danmu.rar_屏幕弹幕_弹幕
版权申诉
37 浏览量
2022-09-24
08:12:10
上传
评论
收藏 3KB RAR 举报
weixin_42653672
- 粉丝: 93
- 资源: 1万+
最新资源
- (完整)数据库课程设计餐厅点餐说明书-21ab6d3c8beb172ded630b1c59eef8c75ebf952c.doc
- 2023-04-06-项目笔记 - 第一百五十四阶段 - 4.4.2.152全局变量的作用域-152 -2024.06.04
- 松哥解协议松哥解协议松哥解协议松哥解协议松哥解协议
- 618节日618节日618节日
- tensorflow-gpu-2.9.1-cp37-cp37m-win-amd64.whl
- tensorflow-gpu-2.9.0-cp37-cp37m-win-amd64.whl
- tensorflow-gpu-2.9.0-cp39-cp39-win-amd64.whl
- lcd daimalcd daima
- 电影领域-推荐算法-个性化内容-观影决策-电影推荐小程序.zip
- 电气控制PLC考试题库
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈