Guest User

Untitled

a guest
Feb 28th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15.  
  16. namespace changeWallpaper
  17. {
  18.  
  19.     public enum Style : int
  20.     {
  21.         Tiled, Centered, Stretched
  22.     }
  23.  
  24.     public partial class Form1 : Form
  25.     {
  26.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  27.         static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
  28.         const int SPI_SETDESKWALLPAPER = 20;
  29.         const int SPIF_UPDATEINIFILE = 0x01;
  30.         const int SPIF_SENDWININICHANGE = 0x02;
  31.         public Form1()
  32.         {
  33.             InitializeComponent();
  34.         }
  35.  
  36.         private void button1_Click(object sender, EventArgs e)
  37.         {
  38.             RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  39.             key.SetValue(@"WallpaperStyle", "2");
  40.             key.SetValue(@"TileWallpaper", "0");
  41.  
  42.  
  43.             string localFilename = @"M:\space_garden-wide - Kopia.jpg";
  44.             /*
  45.              * teraz jak zapisuje zamiast localFilename ten plik z internetu to sie tapeta nie chce ustawic. Ale jak ustawiam "statycznie" to, czyli
  46.              * klikajac PPM -> ustaw tapete to sie ona ustawia :(
  47.             WebClient client = new WebClient();
  48.             client.DownloadFile("http://cdnm.tutorialchip.com/wp-content/uploads/2011/10/Create-a-Shiny-Bucket-Icon-520x520.png", localFilename);    
  49.              * usypiam proces, daje 10 sekund na sciagniecie
  50.             //Thread.Sleep(10000);
  51.              */
  52.             // pobieram i jak tego nie komentuje to sie zmienia, jak zmieniam tapete to juz d..a :(
  53.             //Form1.DownloadRemoteImageFile("http://cdnm.tutorialchip.com/wp-content/uploads/2011/10/Create-a-Shiny-Bucket-Icon-520x520.png", @localFilename);
  54.             SetWallpaper(@localFilename, 2, 0);
  55.             string homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  56.             //string secondHomeDirectory = homeDirectory.Replace("\\","\\");
  57.             MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
  58.             //System.Enum.GetNames(GetType())  
  59.         }
  60.  
  61.  
  62.         private static void DownloadRemoteImageFile(string uri, string fileName)
  63.         {
  64.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  65.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  66.  
  67.             if ((response.StatusCode == HttpStatusCode.OK ||
  68.                 response.StatusCode == HttpStatusCode.Moved ||
  69.                 response.StatusCode == HttpStatusCode.Redirect) &&
  70.                 response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
  71.             {
  72.  
  73.                 // if the remote file was found, download oit
  74.                 using (Stream inputStream = response.GetResponseStream())
  75.                 using (Stream outputStream = File.OpenWrite(fileName))
  76.                 {
  77.                     byte[] buffer = new byte[4096];
  78.                     int bytesRead;
  79.                     do
  80.                     {
  81.                         bytesRead = inputStream.Read(buffer, 0, buffer.Length);
  82.                         outputStream.Write(buffer, 0, bytesRead);
  83.                     } while (bytesRead != 0);
  84.                 }
  85.             }
  86.         }
  87.  
  88.         private void SetWallpaper(string WallpaperLocation, int WallpaperStyle, int TileWallpaper)
  89.         {
  90.             // Sets the actual wallpaper
  91.             SystemParametersInfo(20, 0, WallpaperLocation, 0x01 | 0x02);
  92.             // Set the wallpaper style to streched (can be changed to tile, center, maintain aspect ratio, etc.
  93.             RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  94.             // Sets the wallpaper style
  95.             rkWallPaper.SetValue("WallpaperStyle", WallpaperStyle);
  96.             // Whether or not this wallpaper will be displayed as a tile
  97.             rkWallPaper.SetValue("TileWallpaper", TileWallpaper);
  98.             rkWallPaper.Close();
  99.         }
  100.  
  101.  
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment