使用C++实现的仿Wallpaper动态壁纸软件(含源文件)

您所在的位置:网站首页 wallpaper壁纸制作如何让效果应用给所有图层 使用C++实现的仿Wallpaper动态壁纸软件(含源文件)

使用C++实现的仿Wallpaper动态壁纸软件(含源文件)

2024-07-14 21:43| 来源: 网络整理| 查看: 265

在上一篇博客中,已经介绍了wallpaper的实现原理,并用C#实现,本文将使用C++和Qt框架实现,由于之前已经介绍过具体思路,本文将跳过分析过程,直接上代码。

 新建Qt项目,系统自动生成widget.h,widget.cpp,main.cpp。

我们将使用widget作为主窗体

窗体设计

在项目pro里加上

QT += multimedia multimediawidgets

widget里添加头文件

#include #include #include

创建播放器和播放窗口

QVideoWidget videoWidget; QMediaPlayer player;

在设计界面添加四个按钮,添加槽函数

private slots: void on_openButton_clicked(); void on_playButton_clicked(); void on_pauseButton_clicked(); void on_exitButton_clicked();

获取背景层窗体句柄

HWND GetBackground(){ HWND background = NULL; HWND hwnd = FindWindowA("progman","Program Manager"); HWND worker = NULL; do{ worker = FindWindowExA(NULL,worker,"workerW",NULL); if(worker != NULL){ char buff[200] = {0}; int ret = GetClassNameA(worker,(PCHAR)buff, sizeof(buff) * 2); if(ret == 0) { return NULL; } } if(GetParent(worker) == hwnd){ background = worker; } } while (worker != NULL); return background; }

这里有可能会返回NULL,因为某些系统没有WorkerW窗体,可以在GetBackground()里加上下面这句代码

SendMessage(hwnd,0x052C,0,0);

 最终效果

//获取背景窗体句柄 HWND GetBackground() { //背景窗体没有窗体名,但是知道它的类名是workerW,且有父窗体Program Maneger,所以只要 //遍历所有workW类型的窗体,逐一比较它的父窗体是不是Program Manager就可以找到背景窗体 HWND hwnd = FindWindowA("progman", "Program Manager"); HWND worker = NULL; do { worker = FindWindowExA(NULL, worker, "workerW", NULL); if (worker != NULL) { char buff[200] = {0}; int ret = GetClassNameA(worker, (PCHAR) buff, sizeof(buff) * 2); if (ret == 0) { return NULL; } } if (GetParent(worker) == hwnd) { return worker;//返回结果 } } while (worker != NULL); //没有找到 //发送消息生成一个WorkerW窗体 SendMessage(hwnd,0x052C,0,0); //重复上面步骤 do { worker = FindWindowExA(NULL, worker, "workerW", NULL); if (worker != NULL) { char buff[200] = {0}; int ret = GetClassNameA(worker, (PCHAR) buff, sizeof(buff) * 2); if (ret == 0) { return NULL; } } if (GetParent(worker) == hwnd) { return worker;//返回结果 } } while (worker != NULL); return NULL; }

在C#制作动态壁纸软件时,我专门为刷新背景做了一个exe文件,因此这里可以直接调用

void ReFreshBackground(){ WinExec("D:\\documents\\Wallpaper\\ReFreshBackground.exe",0); }

当我们点击“打开”按钮时,如果是第一次打开,需要新建窗体并设置为背景层窗体的子窗体,而之后就不用了,因此设立布尔变量firstPlay来判断是否第一次打开文件

bool firstPlay = true; void Widget::on_openButton_clicked() { QString file = QFileDialog::getOpenFileName( this, "打开文件", "", ""); if(!file.isEmpty()){ if(firstPlay){ HWND hwnd = (HWND) videoWidget.winId(); SetBackground(hwnd); videoWidget.setWindowFlags(Qt::FramelessWindowHint); videoWidget.showFullScreen(); firstPlay = false; } player.setMedia(QMediaContent(QUrl::fromLocalFile(file))); player.setVideoOutput(&videoWidget); player.play(); } }

给别的按钮设置槽函数

void Widget::on_playButton_clicked() { player.play(); } void Widget::on_pauseButton_clicked() { player.pause(); } void Widget::on_exitButton_clicked() { if(!firstPlay){ ReFreshBackground(); } qApp->exit(0); }

在退出时也需要先判断是否打开了文件,如果没有打开,则不需要刷新壁纸。

将child窗体设置为背景层窗体的子窗体

void SetBackground(HWND child){ SetParent(child,GetBackground()); }

最终结果

内存和GPU占用

 源文件:https://dearx.lanzoui.com/iMZJkryfg4b

 如果出现0x80040266错误,是因为你没有安装LAVFilters

LAVFilters下载地址:https://dearx.lanzoui.com/iz21rryfkja(安装后重启电脑)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3