vbelevcev

Untitled

Sep 17th, 2021
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. function Set-Wallpaper {
  2. param (
  3. [string]$Path,
  4. [ValidateSet('Tile', 'Center', 'Stretch', 'Fill', 'Fit', 'Span')]
  5. [string]$Style = 'Fill'
  6. )
  7.  
  8. begin {
  9. try {
  10. Add-Type @"
  11. using System;
  12. using System.Runtime.InteropServices;
  13. using Microsoft.Win32;
  14. namespace Wallpaper
  15. {
  16. public enum Style : int
  17. {
  18. Tile, Center, Stretch, Fill, Fit, Span, NoChange
  19. }
  20.  
  21. public class Setter
  22. {
  23. public const int SetDesktopWallpaper = 20;
  24. public const int UpdateIniFile = 0x01;
  25. public const int SendWinIniChange = 0x02;
  26. [DllImport( "user32.dll", SetLastError = true, CharSet = CharSet.Auto )]
  27. private static extern int SystemParametersInfo ( int uAction, int uParam, string lpvParam, int fuWinIni );
  28. public static void SetWallpaper ( string path, Wallpaper.Style style )
  29. {
  30. SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  31. RegistryKey key = Registry.CurrentUser.OpenSubKey( "Control Panel\\Desktop", true );
  32. switch( style )
  33. {
  34. case Style.Tile :
  35. key.SetValue( @"WallpaperStyle", "0" ) ;
  36. key.SetValue( @"TileWallpaper", "1" ) ;
  37. break;
  38. case Style.Center :
  39. key.SetValue( @"WallpaperStyle", "0" ) ;
  40. key.SetValue( @"TileWallpaper", "0" ) ;
  41. break;
  42. case Style.Stretch :
  43. key.SetValue( @"WallpaperStyle", "2" ) ;
  44. key.SetValue( @"TileWallpaper", "0" ) ;
  45. break;
  46. case Style.Fill :
  47. key.SetValue( @"WallpaperStyle", "10" ) ;
  48. key.SetValue( @"TileWallpaper", "0" ) ;
  49. break;
  50. case Style.Fit :
  51. key.SetValue( @"WallpaperStyle", "6" ) ;
  52. key.SetValue( @"TileWallpaper", "0" ) ;
  53. break;
  54. case Style.Span :
  55. key.SetValue( @"WallpaperStyle", "22" ) ;
  56. key.SetValue( @"TileWallpaper", "0" ) ;
  57. break;
  58. case Style.NoChange :
  59. break;
  60. }
  61. key.Close();
  62. }
  63. }
  64. }
  65. "@
  66. } catch {}
  67.  
  68. $StyleNum = @{
  69. Tile = 0
  70. Center = 1
  71. Stretch = 2
  72. Fill = 3
  73. Fit = 4
  74. Span = 5
  75. }
  76. }
  77.  
  78. process {
  79. [Wallpaper.Setter]::SetWallpaper($Path, $StyleNum[$Style])
  80.  
  81. # sometimes the wallpaper only changes after the second run, so I'll run it twice!
  82. sleep -ms 200
  83. [Wallpaper.Setter]::SetWallpaper($Path, $StyleNum[$Style])
  84. }
  85. }
  86.  
  87. Set-WallPaper -Path '%appdata%\wall.jpg' -Style Fill
Advertisement
Add Comment
Please, Sign In to add comment