微软Bing壁纸是我非常喜欢的壁纸,怎么说呢它的壁纸非常细腻逼真,总是让人心动,我就不安利了,反正比三百六X鸟好多了。不幸的是Win10更新到最新版以后微软bing缤纷桌面不能运行了,失望加无奈。。 难道我就这么的放弃我的壁纸了么,不,党告诉我,你可以靠自己,你可以创新,然后就自己动手做了一款工具来自动下载Bing壁纸工具。 纯。。。纯到没有界面,纯到没有任何提示,纯到24小时自动更换壁纸,纯。
整体而言,就是用json network slots ,就简单贴下主要代码部分,附件有构建好的应用,win10通过。。。
//bing.cpp #include "bing.h" #pragma comment(lib, "user32.lib") Bing::Bing() { connect(&manager,&QNetworkAccessManager::finished,this,&Bing::downloadFinished); } void Bing::execute() { QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(execute())); timer->setInterval(86400000); timer->start(); QUrl url("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US"); QNetworkRequest request(url); QNetworkReply *reply = manager.get(request); currentReplys.append(reply); qDebug() << "execute "; } void Bing::doGetJson(QNetworkReply *reply) { QByteArray byteArray = reply->readAll(); QJsonParseError jsonError; QJsonDocument json = QJsonDocument::fromJson(byteArray,&jsonError); if (!json.isNull() && (jsonError.error == QJsonParseError::NoError)) { if(json.isObject()) { QJsonObject obj =json.object(); if(obj.contains("images")) { QJsonValue value = obj.value("images"); if(value.isArray()) { QJsonArray jsonArray = value.toArray(); QJsonValue imageValue = jsonArray.at(0); if(imageValue.isObject()) { QString image = imageValue["url"].toString(); QString baseUrl = "https://www.bing.com"; baseUrl.append(image); QUrl url(baseUrl); QNetworkRequest request(url); QNetworkReply *replyImage = manager.get(request); currentReplys.append(replyImage); } } } } } } void Bing::downloadFinished(QNetworkReply *reply) { if(reply->url() == QUrl("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US")) { doGetJson(reply); } else { saveImages(reply); } currentReplys.removeAll(reply); reply->deleteLater(); } void Bing::saveImages(QNetworkReply *reply) { QDate date = QDate::currentDate(); QString fileName = "d:\\bingWallPaper\\bing"; fileName.append(date.toString("yyyyMMdd")).append(".jpg"); QFile file(fileName); if(!file.open(QIODevice::WriteOnly)) { qDebug() << "open failed"; return; } file.write(reply->readAll()); file.close(); setWallPaper(); } void Bing::setWallPaper() { //const wchar_t *wimFile =reinterpret_cast<const wchar_t *>(pszWimFile.utf16()); QDate date = QDate::currentDate(); QString fileName = "d:\\bingWallPaper\\bing"; fileName.append(date.toString("yyyyMMdd")).append(".jpg"); const wchar_t *image =reinterpret_cast<const wchar_t *>(fileName.utf16()); if( !SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)image, SPIF_UPDATEINIFILE) )//调用windows的API函 return; }
源代码:https://github.com/piaodonkey/BingWallPaper 我真是越来越懒了。。
附件:链接:https://pan.baidu.com/s/1XiL4HhluEbPzEcIVo8xhmA 密码:dck1
原文地址:http://blog.51cto.com/amgodchan/2113334
时间: 2024-10-28 21:10:26