QT +VS 自制小应用程序播放音频,更改win10桌面壁纸

Qt版本5.14.2 + VS2017
当前主要功能:播放音频,更改win10桌面壁纸

QT +VS 自制小应用程序播放音频,更改win10桌面壁纸

总结几个要点如下:

1 关于程序透明边框+背景图片背景的切换

	setWindowFlags(Qt::FramelessWindowHint);//无框
	setAttribute(Qt::WA_TranslucentBackground);//背景透明
	
	roletimer = new QTimer;//创建程序背景图片切换定时器
	roletimer->start(5000);
	connect(roletimer, &QTimer::timeout, this, &QTwallpaper::roleCut);
	//程序背景 默认看板娘
	ui.BGwidget->setStyleSheet(QString("background-image:url(:/image/res/appgirl/appgirl1.png);background-repeat:no-repeat;"));
---------------------------------------------
	//程序背景3张图片的定时切换
void QTwallpaper::roleCut()
{
     
	static int flag = 1;
	flag = flag % 3 + 1;
	ui.BGwidget->setStyleSheet(QString("background-image:url(:/image/res/appgirl/appgirl%1.png);background-repeat:no-repeat;").arg(flag));//%1 %2
}

2 由于背景透明,使用原鼠标事件已经不能准确移动

void QTwallpaper::mouseMoveEvent(QMouseEvent * ev)
{
     
	this->move(ev->globalPos() - moveLeftTop); //窗口的左上角坐标
}

void QTwallpaper::mousePressEvent(QMouseEvent * ev)
{
     
	moveLeftTop = ev->pos(); //获取鼠标相当于窗口的坐标
}

3 设置桌面壁纸(目前只能是静态的)

//设置桌面壁纸
void setting::on_selectImg_Button_clicked()
{
     
	QString fileName = QFileDialog::getOpenFileName(this,
		tr("select Image"),
		"F:",
		tr("(*png *jpg)")
			//"(*gif);;"
			//"(*mp4 *mp3)")
	);
	this->srcPath = fileName;
	//壁纸注册表
	QSettings wallPaper("HKEY_CURRENT_USER\\Control Panel\\Desktop",
		QSettings::NativeFormat);


	//给壁纸注册表设置新的值(新的图片路径)
	if (this->srcPath != "") {
     
		wallPaper.setValue("Wallpaper", this->srcPath);
		QByteArray byte = this->srcPath.toLocal8Bit();

		//调用windowsAPI
		SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, byte.data(), SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
	}
}

4 播放音频

#include [^1]	
-----------------------------------
//播放音频
void setting::setbkMusic()
{
     
	QString fileName = QFileDialog::getOpenFileName(this,
		tr("select Music"),
		"F:",
		tr("(*wav *mp3)"
		)
	);
	this->srcPath = fileName;
	
	if (srcPath != " ")
	{
     
		QMediaPlayer *player = new QMediaPlayer;
		
		player->setMedia(QUrl::fromLocalFile(this->srcPath));
		player->play();
		
	}
}

[^1]注:需要加这个库到你的项目里
QT +VS 自制小应用程序播放音频,更改win10桌面壁纸_第1张图片

你可能感兴趣的:(QT,qt,c++)