Get Bing pictures and automatically set to desktop background (using Urldownloadtofile API functions)

Source: Internet
Author: User

As we all know, Bing search site home page will be updated daily a picture, Zhang Zhang Beautiful (the amount, there are some not my taste), especially suitable for making computer wallpaper.

The usual way we want to set up a Bing site background image as a computer desktop background is:

    • Surf the web, search Bing
    • Find the download link for the image and download it
    • Set the picture to the desktop background (there are also some browsers that support right-clicking the picture in the Web page to set the wallpaper)
    • Maybe you'll also delete the downloaded pictures

As a coder, you don't feel any trouble? It takes at least three steps!

So ... I wrote a small program in the C + + language to get a picture of the Bing website and set it as the desktop background. The entire process only needs to click the. exe file, one click to complete!

Of course, you can also through this program, a glimpse of the Windows API a little usage, ingenious C + + knowledge!

Of course, there are mana on the Internet, and some of my programs have borrowed their ideas, thanks to the internet and hard-working contributors.

The following are the technical points of the program design:

--------------------------------------------------------------

Technical points:

    1, access to network address   directly using the network address or download Note If downloaded, to convert \, of course, you can also use \ \
       The network address can be obtained from here: http://cn.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1
       Find a specific image address (XML parsing) in the returned XML page (Images->image->url), stitched to the Bing domain to form the full address
       Note: XML parsing uses the TINYXML2
    2. Convert picture Format (jpg->bmp), the SYSTEMPARAMETERSINFOA function in this program only supports BMP
       Automatic conversion in the program (it is useless to change the suffix name alone), the conversion program is downloaded from the Internet, written in C language
       Consider the need to change the suffix name, then download the image directly, and by the way storage
    3, the picture save path is c:/users/administrator/bingpicture/, the format is. jpg convenient for later browsing

Note: Some users of the computer may not exist path c:/users/administrator/, resulting in the program cannot execute, you can create a path directly under the C packing directory, such as C:/bingpicture/
       Note that the converted BMP format picture is not saved (deleted after setting the background) because the volume is larger
       Determine if the folder exists and if it does not, automatically create a folder


4, note that this program obtains the image size is 1366x768, if your screen resolution is 1920x1080, but also need to parse the XML image URL address string replacement (1366x768 to 1920x1080)

Premise: Image address for 1920x1080 size exists


5, if the program in the International network operation, access to the international version of Bing Wallpaper, if running under the domestic network, access to the Chinese version of Bing Wallpaper. Therefore, in the same day, the images obtained in different network environments may be different
* Features not Implemented *:
    1, get the Daily wallpaper story (using the Bing Story interface), update the wallpaper after the show in the Execution box
    2, boot from the boot, and hidden into the tray (to reduce CPU consumption and increase interest, set to boot automatically start, prompting the network connection, and enter "go" to perform functions)
       If the computer is not turned off, it will start automatically at 24:00, changing the background
    3, the software automatically updated version features

--------------------------------------------------------------

The program is provided later in the article, the source code has been commented very detailed, no longer repeat.

Note that before using the program, be sure to look at the following considerations:

--------------------------------------------------------------

Please note:
1, the software uses QT development, you can also add files to their own projects, using other IDE development
One thing to note is that the software needs to be added to the URLMON.DLL (source package)

2, the software open source (source is located in Xiaoxi666 's blog Park and GitHub, not the other address is responsible for the link), only for learning communication, do not use for commercial purposes

3, in order to prevent the software to be added malicious function, do not provide executable files, if you need to use recompile, the compiler needs to support C++11

4, the software program contains the deletion of temporary files function, please carefully confirm before the change, to avoid path errors and delete other important files

--------------------------------------------------------------

However many small partners to experience the effect, I will put the executable file (do not arbitrarily spread the. exe file to prevent anyone from adding malicious features.) Of course open source can be shared)

Download area:

    • Source
    • Executable file (image saved in executable file is c:/bingpicture/)

But be sure to check the document verification code (to ensure safety):

    • Executable file compression package wallpaper Check Code

Md5:48173ba7dcf2120f2822226a5d4a90cf
Sha1:ceb0ed570af613ec3829ac8fde4f8c50ddef4101

    • Executable Wallpaper_1366x768_common Check code (this version is for 1366x768 resolution)

Md5:acda8e5e4cf0b2916254b233d1243fd3
Sha1:6041c813dc8e2ae29ea8675ef279cfc0e7921d53

    • Executable Wallpaper_1920x1080_common Check code (this version is for 1920x1080 resolution)

Md5:474570808a56efdc7b589f605d08c5b6
sha1:b78921ad655b35f079076904a22f1a0e5122eb7f

For easy browsing, post the main source program:

Main.cpp file

  1//main.cpp 2/******************windows desktop Background replace C + + program *********************************************************** 3 Features: Get daily Bing search home images, set as desktop wallpapers of the day.        and save it to a local folder for later browsing 4 xiaoxi666 5th: 2017/03/12 6 7 Technical points: 8 1, get network address directly using the network address or download Note If you want to convert \ to/After downloading, you can also use \ 9 The network address can be obtained from here: Http://cn.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1 10 in the Returned XML page (Images->im Age->url) Find a specific image address (XML parsing), stitched to the back of the Bing domain, constitutes the full address 11 Note: XML parsing uses TINYXML2 12 13 2, conversion picture Format (jpg->bmp), Systempa in this program     The RAMETERSINFOA function only supports BMP 14 in the program automatic conversion (the suffix name is no use), the conversion program is downloaded from the Internet, written in C and 15 considering the need to change the suffix name, then directly download the picture good, by the way storage 16 17 3, the picture save path is C:\bingPicture\, the format is. jpg Convenient later Browse 18 note Do not save the converted BMP format picture (after setting the background is deleted), because the volume is larger 19 determine if the folder exists, if not, automatically create a folder 2 0 21 * Features not Implemented *: 22 * * * get the Daily wallpaper story (using the Bing Story interface), update the wallpaper after display in the execution box 23 24 * * * Boot from boot, and hidden into the tray (to reduce CPU consumption and increase interest, set to boot automatically start, prompting the network connection, and enter "Go" to perform the function) 25 if the computer is not turned off, it will start automatically at 24:00, replace the background 26 27 * * * Software update automatically version features 28 29 ******************************************/#include <iostream>/input/output #include <c string>//File naming processing requires a string #include <windows.h>//Calling operating system various APIs #include <ctime>//Get time, various file naming #include <UrlMon.h>//Includes API with download service #include "tinyxml2.h"//Parse XML PNS #include <io.h>//Determine if folder Presence #include <direct.h>//Create Folder "C" All-in-#include "jpeg.h"//Convert Picture format jpg->bmp convert format program using C language 42} 43 44//Create local bingpicture path and TMP path Createdir () 46 {47//local bingpicture path std::string localfolder= "C: /bingpicture/";             if (0!=access (Localfolder.c_str (), 0))//Determine if the folder exists, if it does not exist, create an if (0!=mkdir (Localfolder.c_str ())) 52 std::cout<< "Create Folder Bingpicture failed! "<<std::endl; std::cout<< Else "Create folder Bingpicture success!" "<<std::endl; "Else" std::cout<< "folder Bingpicture already exists! "<<std::endl; 57 58//local TMP RoadDiameter std::string localxmlfolder= "c:/bingpicture/tmp/"; if (0!=access (Localxmlfolder.c_str (), 0))//Determine if the folder exists, if it does not exist, create a if (0!=mkdir (Localxmlfolder.c_str ())) std::cout<< "Create Temp folder tmp failed!" "<<std::endl; All else std::cout<< "Create Temp folder tmp successfully!" "<<std::endl; std::cout<< "Temp folder tmp already exists! "<<std::endl; 68 69} 70 71/************************************************************************************** 72 first understand a concept, That is, string replaces all strings, replacing all "12" of the string "12212" with "21", what is the result? 73 can be 22211, or 21221, sometimes the application of the scene is different, you will want to get different results, so both of the answers have been implemented. 74 **************************************************************************************/75//Replace String Method 1 (full polling, Replace it once and then scan again, because a string that meets the replacement criteria may appear again after replacement) Std::string & Replace_all (std::string& str,const std::string& old         _value,const std::string& New_value) (true) {Std::string::size_type pos (0); 80 if (Pos=str.find (Old_vaLue))!=std::string::npos) str.replace (Pos,old_value.length (), new_value); A. (+)---return str; 86} 87 88//Replace String Method 2 (replace only once) in this project, only replace \ for/With Method 2 can be std::string & replace_all_distinct (std::string& str,const std::  string& old_value,const std::string& new_value) (Std::string::size_type pos (0); Pos!=std::string::npos; Pos+=new_value.length ()) (Pos=str.find (Old_value,pos)!=std::string::npos) 94 Str.repla CE (Pos,old_value.length (), new_value); The else break; (+)} 98} 99 100//Get Month day (named with) 101 std::string Getyearmonthday () 102 {103 time_t timer;104 time (&timer); tm* t_ TM = LocalTime (&timer); 106 107 std::string year=std::to_string (t_tm->tm_year+1900); 108 std::string Month=st D::to_string (t_tm->tm_mon+1); 109 std::string day=std::to_string (t_tm->tm_mday); std::string PictureName=Y Ear+ "_" +month+ "_"+day;111 picturename;113}114 115//Get the XML of the picture and parse the URL path of the picture std::string Getpicturexmlandurl () 117 {118/// XML path on the network 119 std::string Webxmlpath = "Http://cn.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1"; 120//     Ground XML Path 121 std::string localxmlfolder= "c:/bingpicture/tmp/"; 122 std::string localxmleach=getyearmonthday (); 123                          std::string localxmlfullpath=localxmlfolder+localxmleach+ ". xml"; 124 if (Urldownloadtofilea (NULL,126                          Webxmlpath.c_str (), 127 localxmlfullpath.c_str (), 128 0,129 NULL) (==S_OK) 131 {std::cout<< "XML download succeeded! About to parse today wallpaper url! "<<std::endl;133 134/*************** begins parsing the URL path in XML *******************/135 tinyxml2::xmldocument D oc;136 if (tinyxml2::xml_success! = doc. LoadFile (Localxmlfullpath.c_str ())) 137 std::cout<< "Read XML file Exception! "<<std::endl;138 TinYxml2::xmlelement *images=doc.         RootElement (); 139 tinyxml2::xmlelement *image =images->firstchildelement ("image"); 140 141//Picture Url142 std::string webpicturedomain= "http://cn.bing.com"; 143 std::string webpictureurl= ""; 144 145 if (image! =null) 146 webpictureurl=image->firstchildelement ("url")->gettext (); 147 148 std::string WebPictur efullpath1366x768 =webpicturedomain+webpictureurl;149 std::cout<< "Today wallpaper URL resolution successful!" <<std::endl;150/*********************************************************/151 return WebPictureFull path1366x768;152////convert 1366x768 to 1920x1080153//std::string webpicturefullpath1920x1080 =replace_all_distin     CT (webpicturefullpath1366x768, "1366x768", "1920x1080"), 154 155//Return webpicturefullpath1920x1080;156}157 else158 {159 std::cout<< "XML download failed! Unable to get picture url! Please check the network connection is OK! "<<std::endl;160 return" error "; 161}162 163}164 165//Download images from the network and store to local 166 std::string getpicture (std::string webfullpath) 167 {168//local storage path 169 std::string localfolder= "C:/bingpicture/"; std::string localeach=getyearmonthday (); 171 std::string localfullpath=localfolder+localeach+                          ". jpg"; 172 173 if (Urldownloadtofilea (null,174 webfullpath.c_str (), 175 Localfullpath.c_str (), 176 0,177 NULL) 178 ==s_ok) 179 {1 std::cout<< "Today wallpaper download success!         "<<std::endl;181 182/*************** below convert picture format jpg->bmp******************/183//Temp folder tmp path 184 std::string tmpfolder= "c:/bingpicture/tmp/"; 185//.bmp Picture Path 186 std::string Bmpfolder=tmpfolder+getyearmo Nthday () + ". bmp"; 187 Loadjpegfile (Const_cast<char *> (Localfullpath.c_str ()), Const_cast<char *> (bmpFold Er.c_str ())); 188/*******************************************************/189 return Bmpfolder;190}191 else192 {193 std::cout<< "wallpaper download failed! Please check the network connection is OK! "<<std::endl;194 return" error "; 195}196}197 198//change desktop background after successful, delete BMP file and XML file (only keep JPG file), this step need to be careful to avoid deleting the error path under the Content 199 void Deletebmpandxml () 200 {201//Temp folder TMP path 202 std::string tmpfolder= "c:/bingpicture/tmp/"; 203//.bmp Picture Road Diameter 204 std::string bmpfolder=tmpfolder+getyearmonthday () + ". bmp"; 205//xml file path 206 std::string xmlfolder=tmpfolder +getyearmonthday () + ". Xml"; 207 208 if (0==access ("c:/bingpicture/tmp/", 0)//Determine if the folder exists, delete if present 209 {210//deleted                 Except BMP picture 211 if (0==access (Bmpfolder.c_str (), 0)) 212 {213 if (0==remove (Bmpfolder.c_str ())) 214 std::cout<< "Delete temporary BMP format picture Successfully! "<<std::endl;215 else216 std::cout<<" Delete temporary BMP format picture Failed! "<<std::endl;217}218 else219 std::cout<<" Temporary BMP format picture does not exist! "<<std::endl;220 221//Delete XML file 222 if (0==access (xmlfolder.c_str (), 0)) 223 {224 if (0==remove (Xmlfolder.c_str ())) 225 std::cout<< "Delete the XML file successfully! "<<std::endl;226 else227 std::cout<<" failed to delete the XML file! "<<std::endl;228}229 else230 std::cout<<" XML file does not exist!             "<<std::endl;231 232//Delete tmp folder (Note that this function can only delete empty folders, so delete the files in the folder first) 233 if (0==rmdir (Tmpfolder.c_str ())) 234 std::cout<< "Temp folder tmp has been removed! "<<std::endl;235 else236 std::cout<<" Temp folder tmp Delete failed! "<<std::endl;237}238 else239 std::cout<<" Temp folder tmp does not exist! "<<std::endl;240 241}242 243//change desktop background (picturefullpath: Picture full path) 244 void Changepicture (std::string                           Picturefullpath) 245 {246 bool result=false;247 Result=systemparametersinfoa (spi_setdeskwallpaper,248 0,249 (PVOID) picturefullpath.c_str (), 0); 251 if (re     Sult==false) 252 {253    std::cout<< "Today wallpaper update failed! Please contact the developer!                                   "<<std::endl;254}255 else257 {258 SystemParametersInfoA (spi_setdeskwallpaper,259                                   0,260 (PVOID) picturefullpath.c_str (), 261    Spif_sendchange); 262//deletebmpandxml (); Windows8 and above will turn black, put this statement on the last side of main is no problem, the specific reason unknown 263 system ("CLS"); std::cout<< "version:1.0.0 (Author : xiaoxi666) "<<std::endl<<std::endl;265 std::cout<<" Today wallpaper updated successfully! "<<std::endl<<std::endl;266 std::cout<<" a wonderful day begins! Enjoy it with your heart! "<<std::endl<<std::endl;267}268}269 int main () 271 {272 std::string startorder=" "; 273 Std::co ut<< "Hey! Little buddy! Your intimate wallpaper Small assistant has been launched! will set for you today wallpaper Oh! "<<std::endl<<std::endl;274 std::cout<<" Please make sure your computer network connection is in good condition and be ready to enter Go "&LT;&LT;STD::ENDL&LT;&LT;STD:: endl;275 std::cout<< "Please enter directive:"; 276 std::cin>>startorder;277 WHILe ("Go"!=startorder) 278 {279 std::cout<< "Oh, wrong, re-enter it:"; 280 std::cin>>startorder;281 }282 if ("Go" ==startorder) 283 {284 createdir (); 285 changepicture (Getpicture (Getpicturexmlandurl ())) ; 286}287 288/******************************* The following is a personalized subtitle output, independent of the program core functions ************************/289 std::string Umu a0= "* * * * ******** ******* ******* * * * * * *; 290 std::string umua1=" * * * *    ******* * * * * * "; 291 std::string umua2=" * * * * * * * * * * * * * "; 292 std::string umua3=" * * * * * * * * * * * * * * * * * * *; 293 s          Td::string umua4= "********* ******** ******* ******* * *"; 294 std::string umua5= "        ******* * * * * "; 295 std::string umua6=" * * * * * * * *  **           **      "; 296 std::string umua7=" * * * * * * * * * * * * * "; 297 std::string    umua8= "* * * * * * * * * * * * *"; 298 std::string umua9= "* * *     * * * * * * * "; 299 #define MUA (n) std::cout<<umua# #n <<std::endl;301 std::cout<<std::endl<<std::endl;302 MUA (0); MUA (1); MUA (2); MUA (3); MUA (4); MUA (5); MUA (6); MUA (7); MUA (8); MUA (9); 303 std::cout<<std::endl<<std::endl<<std::endl;304 System ("pause"); 305/************* /306 Deletebmpandxml (); 307 return 0; 308}

Image format Conversion program (this is the online download of C source, I changed the interface, in this thanks)

Jpeg.h

View Code

Jpeg2bmp.c

View Code

XML parsing, I use the TINYXML2 Open Source Library, this is not affixed to the source code.

About QT Project icon making

The QT version I used is 4.8.5.

The steps are as follows:

    1. Create a new text file in the QT project directory and change its name to *.RC (name whichever)
    2. Add your icon file. ico to the project
    3. Open the RC file and add the following line of text to the RC file (replace the * in the text with the name of your icon):

      Idi_icon   ICON    discardable     
    4. Add the following text to the project file. Pro (replace * with your RC file name):

Rc_file = \
*.rc

5. Recompile the project to

   

About the QT Project release

QT Project Release the most troublesome is the dynamic dependent library, the common tool is hap-depends, It can view the software's dependent library (*.dll), directly with it to open your. exe file, to see which. dll files are missing, find them and EXE in the same directory.

Of course, there are several platforms that need to be tested before publishing.

Finally, I wish you a happy life!

Get Bing pictures and automatically set to desktop background (using Urldownloadtofile API functions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.