📜 ⬆️ ⬇️

Donbass Arena on the desktop



On the eve of Euro 2012, I decided to write a program that installs an image from the webcams of Donbass Arena on the desktop.

The algorithm is very simple and consists of only a few points:
  1. Get the path to the file where Windows saves the current desktop image.
  2. Upload an image from the site and save it according to the path you received in the first step.
  3. Give a command to update the desktop image.


The path to the current desktop image file can be obtained from the registry by reading the data of the Wallpaper parameter of the “HKEY_CURRENT_USER \ Control Panel \ Desktop” key:
')
String value = ""; try { RegistryKey hkey = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false); value = (String) hkey.GetValue("Wallpaper"); hkey.Close(); } catch (Exception) { value = ""; } 

value will store something like “C: \ Users \% username% \ AppData \ Roaming \ Microsoft \ Windows \ Themes \ TranscodedWallpaper.jpg”

The second step using C # can be implemented in two lines:
 WebClient client = new WebClient(); client.DownloadFile(fromUrl, pathToFile); 

The file at fromUrl is downloaded to the specified pathToFile location, replacing the previous one, if any, with no questions asked.

In the third step I had to tinker a bit. The command to update the desktop image in WinAPI looks like this:
 SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, NULL, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE); 

But I did not know how to call this command by means of .NET, although I saw in the books of Alexander Klimov quite a while that it was possible. I came to the aid of the site pinvoke.net which helped me write this simple piece of code:

 public static void updateWallpaper() { SystemParametersInfo(SPI.SETDESKWALLPAPER, 0, null, SPIF.SENDCHANGE | SPIF.UPDATEINIFILE); } [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, String pvParam, SPIF fWinIni); private enum SPI { SETDESKWALLPAPER = 0x0014 } private enum SPIF { UPDATEINIFILE = 0x01, SENDCHANGE = 0x02 } 


The rest of the time was spent on the interface:


Total

The program hangs in the tray and mercilessly eats traffic , updating the desktop.

Sources can be downloaded here , and the program itself here .
To work, you need .NET Framework 2.0 or higher.

Thank you all for your attention!

Source: https://habr.com/ru/post/144137/


All Articles