Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace changeWallpaper
- {
- public partial class Form1 : Form
- {
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
- const int SPI_SETDESKWALLPAPER = 20;
- const int SPIF_UPDATEINIFILE = 0x01;
- const int SPIF_SENDWININICHANGE = 0x02;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
- key.SetValue(@"WallpaperStyle", "2");
- key.SetValue(@"TileWallpaper", "0");
- string localFilename = @"M:\funnybackground.jpg";
- /*
- * teraz jak zapisuje zamiast localFilename ten plik z internetu to sie tapeta nie chce ustawic. Ale jak ustawiam "statycznie" to, czyli
- * klikajac PPM -> ustaw tapete to sie ona ustawia :(
- * usypiam proces, daje 10 sekund na sciagniecie
- //Thread.Sleep(10000);
- */
- // pobieram i zapisuje zamiast localFilename
- Form1.DownloadRemoteImageFile("http://cdnm.tutorialchip.com/wp-content/uploads/2011/10/Create-a-Shiny-Bucket-Icon-520x520.png", localFilename);
- Thread.Sleep(10000);
- SetWallpaper(localFilename, 2, 0);
- string homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
- //string secondHomeDirectory = homeDirectory.Replace("\\","\\");
- MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
- //System.Enum.GetNames(GetType())
- }
- private static void DownloadRemoteImageFile(string uri, string fileName)
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- if ((response.StatusCode == HttpStatusCode.OK ||
- response.StatusCode == HttpStatusCode.Moved ||
- response.StatusCode == HttpStatusCode.Redirect) &&
- response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
- {
- // if the remote file was found, download oit
- using (Stream inputStream = response.GetResponseStream())
- using (Stream outputStream = File.OpenWrite(fileName))
- {
- byte[] buffer = new byte[4096];
- int bytesRead;
- do
- {
- bytesRead = inputStream.Read(buffer, 0, buffer.Length);
- outputStream.Write(buffer, 0, bytesRead);
- } while (bytesRead != 0);
- }
- }
- }
- private void SetWallpaper(string WallpaperLocation, int WallpaperStyle, int TileWallpaper)
- {
- // Sets the actual wallpaper
- SystemParametersInfo(20, 0, WallpaperLocation, 0x01 | 0x02);
- // Set the wallpaper style to streched (can be changed to tile, center, maintain aspect ratio, etc.
- RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
- // Sets the wallpaper style
- rkWallPaper.SetValue("WallpaperStyle", WallpaperStyle);
- // Whether or not this wallpaper will be displayed as a tile
- rkWallPaper.SetValue("TileWallpaper", TileWallpaper);
- rkWallPaper.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment