在上一篇博客中,已经介绍了wallpaper的实现原理,并用C#实现,本文将使用C++和Qt框架实现,由于之前已经介绍过具体思路,本文将跳过分析过程,直接上代码.
新建Qt项目,系统自动生成widget.h,widget.cpp,main.cpp.
我们将使用widget作为主窗体
窗体设计
在项目pro里加上
1
| QT += multimedia multimediawidgets
|
widget里添加头文件
1 2 3
| #include <QVideoWidget> #include <QMediaPlayer> #include <QMediaPlaylist>
|
创建播放器和播放窗口
1 2
| QVideoWidget videoWidget; QMediaPlayer player;
|
在设计界面添加四个按钮,添加槽函数
1 2 3 4 5 6 7 8
| private slots: void on_openButton_clicked(); void on_playButton_clicked(); void on_pauseButton_clicked(); void on_exitButton_clicked();
|
获取背景层窗体句柄
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| 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()里加上下面这句代码
1
| SendMessage(hwnd,0x052C,0,0);
|
最终代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| HWND GetBackground() { 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); 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文件,因此这里可以直接调用
1 2 3
| void ReFreshBackground(){ WinExec("D:\\documents\\Wallpaper\\ReFreshBackground.exe",0); }
|
当我们点击“打开”按钮时,如果是第一次打开,需要新建窗体并设置为背景层窗体的子窗体,而之后就不用了,因此设立布尔变量firstPlay来判断是否第一次打开文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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(); } }
|
给别的按钮设置槽函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 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窗体设置为背景层窗体的子窗体
1 2 3
| void SetBackground(HWND child){ SetParent(child,GetBackground()); }
|
最终效果
内存和GPU占用
如果出现0x80040266错误,是因为你没有安装LAVFilters
(安装后重启电脑)