Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. Function Update-Wallpaper {
  2. Param(
  3. [Parameter(Mandatory=$true)]
  4. $Path,
  5.  
  6. [ValidateSet('Center','Stretch','Fill','Tile','Fit')]
  7. $Style
  8. )
  9. Try {
  10. if (-not ([System.Management.Automation.PSTypeName]'Wallpaper.Setter').Type) {
  11. Add-Type -TypeDefinition @"
  12. using System;
  13. using System.Runtime.InteropServices;
  14. using Microsoft.Win32;
  15. namespace Wallpaper {
  16. public enum Style : int {
  17. Center, Stretch, Fill, Fit, Tile
  18. }
  19. public class Setter {
  20. public const int SetDesktopWallpaper = 20;
  21. public const int UpdateIniFile = 0x01;
  22. public const int SendWinIniChange = 0x02;
  23. [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  24. private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  25. public static void SetWallpaper ( string path, Wallpaper.Style style ) {
  26. SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  27. RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  28. switch( style ) {
  29. case Style.Tile :
  30. key.SetValue(@"WallpaperStyle", "0") ;
  31. key.SetValue(@"TileWallpaper", "1") ;
  32. break;
  33. case Style.Center :
  34. key.SetValue(@"WallpaperStyle", "0") ;
  35. key.SetValue(@"TileWallpaper", "0") ;
  36. break;
  37. case Style.Stretch :
  38. key.SetValue(@"WallpaperStyle", "2") ;
  39. key.SetValue(@"TileWallpaper", "0") ;
  40. break;
  41. case Style.Fill :
  42. key.SetValue(@"WallpaperStyle", "10") ;
  43. key.SetValue(@"TileWallpaper", "0") ;
  44. break;
  45. case Style.Fit :
  46. key.SetValue(@"WallpaperStyle", "6") ;
  47. key.SetValue(@"TileWallpaper", "0") ;
  48. break;
  49. }
  50. key.Close();
  51. }
  52. }
  53. }
  54. "@ -ErrorAction Stop
  55. }
  56. }
  57. Catch {
  58. Write-Warning -Message "Wallpaper not changed because $($_.Exception.Message)"
  59. }
  60. [Wallpaper.Setter]::SetWallpaper( $Path, $Style )
  61. }
  62. (new-object System.Net.WebClient).DownloadFile('https://i.imgur.com/OoORxwh.jpg',"$Env:Temp\b.jpg");
  63. Update-Wallpaper -Path "$Env:Temp\b.jpg" -Style Center
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement