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

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

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

我们将使用widget作为主窗体

窗体设计

在项目pro里加上

QT += multimedia multimediawidgets

widget里添加头文件

#include <QVideoWidget>
#include <QMediaPlayer>
#include <QMediaPlaylist>

创建播放器和播放窗口

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(安装后重启电脑)

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dear_Xuan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值