赞
踩
第一次发博客,好激动~~~
皮卡丘镇压
程序效果图
**闲来无聊写了个小程序,闲话少说上程序。**
main.cpp
#include "widget.h"
#include <QApplication>
#include <QIcon>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
//以下4句代码设置窗体透明、全屏、隐藏标题栏、设置Icon
w.setAttribute(Qt::WA_TranslucentBackground,true);
w.setWindowState( Qt::WindowFullScreen );
w.setWindowFlags(Qt::FramelessWindowHint);
w.setWindowIcon(QIcon("marryme.png"));
w.show();//显示窗口
w.start();//do something
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#define MAX_PIC 26 //花瓣数
#include <QWidget>
#include <QPixmap>
#include <qlist.h>
#include <QTime>
#include <QDesktopWidget>
#include <QRect>
#include <QSize>
#include <QApplication>
#include <QPainter>
#include <QImage>
#include <QMediaPlayer>
#include <QFont>
#include <QLinearGradient>
//花瓣结构体
struct arose{
QPixmap type;//花瓣类型
int speed; //下落速度
int Xpos; //横纵
int Ypos; //纵轴
};
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void ramCreateRose(void); //随机产生花瓣
void start(void); //do something
protected:
void timerEvent(QTimerEvent *e); //重写的定时器处理器
void paintEvent(QPaintEvent *event);//重写的绘制处理器
private:
int m_scrW; //屏幕宽
int m_scrH; //屏幕高
int m_createRoseTimerID;//定时器ID
int m_drawTimerID; //同上
int m_strNum; //字符标记
int m_strYpos; //字符纵坐标
int m_fontSize; //字体大小
QString m_str[4]; //显示字符
QList<arose> m_roseList;//玫瑰表链
QPixmap m_rosePic[MAX_PIC];//花瓣QPixmap对象
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
for(int i=0; i<MAX_PIC; i++)
{
char fileName[100];
sprintf(fileName,"rose\\rose%d.png",i);
QImage img(fileName);
m_rosePic[i]= QPixmap::fromImage(img);
}
QDesktopWidget* desktopWidget = QApplication::desktop();//获取屏幕大小
QRect screenRect = desktopWidget->screenGeometry();
m_scrW = screenRect.width();
m_scrH = screenRect.height();
QTime time = QTime::currentTime();
qsrand(time.msec()+time.second()*1000);
//嘿嘿嘿!自己好好写,以下是模版
m_str[0].append("HELLO!");
m_str[1].append("I HAVE A SECRET TO TELL YOU.");
m_str[2].append("I LOVE YOU FOREVER!");
m_str[3].append("Miss XXXX,WOULD YOU MARRY ME!--by:XX");
m_strNum = 0;
m_fontSize = 80;
m_strYpos = 0;
}
Widget::~Widget()
{
}
void Widget::ramCreateRose(void)
{
int randTypeIndex = qrand() % MAX_PIC;
QSize size = m_rosePic[randTypeIndex].size();
int minXpos = -size.width()/2;
int maxXpos = m_scrW - size.width()/2;
int randXpos = qrand() % (maxXpos - minXpos + 1) + minXpos;
arose rose;
rose.speed = qrand() % 4 + 1;
rose.type = m_rosePic[randTypeIndex];
rose.Xpos = randXpos;
rose.Ypos = -size.height();
m_roseList.append(rose);
}
void Widget::start()
{
m_createRoseTimerID = startTimer(200);//设置两个定时器
m_drawTimerID = startTimer(15);
QMediaPlayer *player=new QMediaPlayer(this);//播放音乐
player->setMedia(QUrl::fromLocalFile("music\\Top Of The World.mp3"));
player->setVolume(100);
player->play();
}
void Widget::timerEvent(QTimerEvent *e)
{
if(e->timerId() == m_createRoseTimerID)
{
ramCreateRose();
}
else if(e->timerId() == m_drawTimerID)
{
update();//刷新显示
}
}
void Widget::paintEvent(QPaintEvent *event)
{
QPixmap pixmap=QPixmap(size());//构造大小一样的临时画布
//以下是绘制临时画布
pixmap.fill(this,0,0);
QPainter painter(&pixmap);
painter.initFrom(this);
QList<arose>::iterator i;
for (i = m_roseList.begin(); i != m_roseList.end(); ++i) {
arose rose = *i;
painter.drawPixmap(QPoint(rose.Xpos,rose.Ypos), rose.type);
}
for(int m = 0; m < m_roseList.size(); ++m)
{
m_roseList[m].Ypos += m_roseList[m].speed;
if(m_roseList[m].Ypos > m_scrH)
{
m_roseList.removeAt(m);
-- m;
}
}
QFont font;
font.setPixelSize(m_fontSize);
font.setBold(true);
painter.setPen(QColor(128,0,128,255));
painter.setFont(font);
QLinearGradient linearGradient(20,20,150,150);
painter.setBrush(QBrush(linearGradient));
painter.drawText((m_scrW-m_fontSize*m_str[m_strNum].size()/2) / 2, m_strYpos,m_str[m_strNum]);
if(m_strNum < 3)
{
m_strYpos += 1;
if(m_strYpos > m_scrH + 100)
{
++ m_strNum;
m_strYpos = 0;
}
}
else
{
if(m_strYpos < m_scrH / 2)
{
m_strYpos += 1;
}
}
//画到屏幕上去
QPainter pp(this);
pp.drawPixmap(0,0,pixmap);
}
设置图标,*.ico文件可以通过网上的放在源码目录下,可以通过网上的小工具生成
IDI_ICON1 ICON DISCARDABLE "marryme.ico"
工程文件
QT += core gui
QT += multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = marryMe
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
RC_FILE = app.rc
DISTFILES += \
app.rc
嘿嘿嘿!大功告成!
附带玫瑰花瓣图片
只放这么多,还需要的请自行使用ps制作!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。