mikedopp

WallpaperChanger.ps1

May 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function New-BMPfileWithText {
  2. [cmdletbinding(PositionalBinding=$false)]
  3. PARAM ([Parameter(Mandatory=$true)] [string] $path="$home\foo.bmp",
  4. [Parameter(Mandatory=$true)] [string] $text='text here',
  5. [Parameter(Mandatory=$false)] [alias('bgColor')] [system.drawing.color] $bgColour = [system.drawing.color]::Blue,
  6. [Parameter(Mandatory=$false)] [alias('fgColor')] [system.drawing.color] $fgColour = [system.drawing.color]::White,
  7. [Parameter(Mandatory=$false)] [System.Drawing.Size] $BMPsize=[System.Drawing.Size]::new(270,61),
  8. [Parameter(Mandatory=$false)] [int] $fontSize=10
  9. )
  10.  
  11. Add-Type -AssemblyName System.Drawing
  12.  
  13. $bmp = new-object -TypeName System.Drawing.Bitmap -Argumentlist ($BMPsize.Width, $BMPsize.Height)
  14. $font = new-object -TypeName System.Drawing.Font -Argumentlist ('Consolas',$fontSize)
  15.  
  16. $bg = New-Object -TypeName Drawing.SolidBrush -ArgumentList ($bgColour)
  17. $fg = New-Object -TypeName Drawing.SolidBrush -ArgumentList ($fgColour)
  18.  
  19. $graphics = [System.Drawing.Graphics]::FromImage($bmp)
  20. $x=0
  21. $y=0
  22. $graphics.FillRectangle($bg,$x,$y,$bmp.Width,$bmp.Height)
  23. $graphics.DrawString($text,$font,$fg,10,10)
  24. $graphics.Dispose()
  25.  
  26. if (Test-Path -Path $path) { Remove-Item -Path $path }
  27. $bmp.Save($path)
  28.  
  29. Get-Item -Path $path
  30.  
  31. }
  32.  
  33.  
  34. #Set-WindowsWallpaper
  35. Add-Type -AssemblyName System.Drawing
  36.  
  37. Add-Type @"
  38. using System;
  39. using System.Runtime.InteropServices;
  40. using Microsoft.Win32;
  41. namespace Wallpaper
  42. {
  43. public enum Style : int
  44. {
  45. Tile, Center, Stretch, NoChange
  46. }
  47.  
  48. public class Setter {
  49. public const int SetDesktopWallpaper = 0x14; // SPI_SETDESKWALLPAPER
  50. public const int UpdateIniFile = 0x01; // SPIF_UPDATEINIFILE
  51. public const int SendWinIniChange = 0x02; // SPIF_SENDWININICHANGE
  52.  
  53. [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  54. private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  55.  
  56. public static void SetWallpaper ( string path, Wallpaper.Style style ) {
  57. SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  58. RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  59. switch( style )
  60. {
  61. case Style.Stretch :
  62. key.SetValue(@"WallpaperStyle", "2") ;
  63. key.SetValue(@"TileWallpaper", "0") ;
  64. break;
  65. case Style.Center :
  66. key.SetValue(@"WallpaperStyle", "1") ;
  67. key.SetValue(@"TileWallpaper", "0") ;
  68. break;
  69. case Style.Tile :
  70. key.SetValue(@"WallpaperStyle", "1") ;
  71. key.SetValue(@"TileWallpaper", "1") ;
  72. break;
  73. case Style.NoChange :
  74. break;
  75. }
  76. key.Close();
  77. }
  78. }
  79. }
  80. "@
  81.  
  82.  
  83.  
  84. ### args for New-BMPfileWithText
  85. $path="$HOME\Test.bmp"
  86. $text="$ENV:COMPUTERNAME"
  87. $bgcolor = [drawing.color]::DarkOrange
  88. $bgcolor = [drawing.color]::DarkSlateBlue
  89.  
  90. $fontsize=9
  91. $bmpsize=New-Object -TypeName System.Drawing.Size -ArgumentList 230,70
  92. ###
  93.  
  94. # Generate the BMP file
  95. $BMPfile=New-BMPfileWithText -path $path -Text $text -bgColour $bgcolor -fontsize $fontsize -BMPsize $bmpsize
  96.  
  97.  
  98. ### now pass the generated BMP file and set the wallpaper.
  99. $Tile = 0
  100. $Center = 1
  101. $Stretch = 2
  102. $NoChange = 3
  103.  
  104. [Wallpaper.Setter]::SetWallpaper( $BMPfile.Fullname, $Tile)
Add Comment
Please, Sign In to add comment