Guest User

Untitled

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