mikedopp icon

WallpaperChanger.ps1

mikedopp | PRO | 05/22/18 07:41:19 PM UTC | 0 ⭐ | 323 👁️ | Never ⏰ | []
PowerShell |

3.09 KB

|

None

|

0 👍

/

0 👎

Function New-BMPfileWithText {
[cmdletbinding(PositionalBinding=$false)]
PARAM ([Parameter(Mandatory=$true)] [string] $path="$home\foo.bmp",
[Parameter(Mandatory=$true)] [string] $text='text here',
[Parameter(Mandatory=$false)] [alias('bgColor')] [system.drawing.color] $bgColour = [system.drawing.color]::Blue,
[Parameter(Mandatory=$false)] [alias('fgColor')] [system.drawing.color] $fgColour = [system.drawing.color]::White,
[Parameter(Mandatory=$false)] [System.Drawing.Size] $BMPsize=[System.Drawing.Size]::new(270,61),
[Parameter(Mandatory=$false)] [int] $fontSize=10
)
 
Add-Type -AssemblyName System.Drawing
 
$bmp = new-object -TypeName System.Drawing.Bitmap -Argumentlist ($BMPsize.Width, $BMPsize.Height)
$font = new-object -TypeName System.Drawing.Font -Argumentlist ('Consolas',$fontSize)
 
$bg = New-Object -TypeName Drawing.SolidBrush -ArgumentList ($bgColour)
$fg = New-Object -TypeName Drawing.SolidBrush -ArgumentList ($fgColour)
 
$graphics = [System.Drawing.Graphics]::FromImage($bmp)
$x=0
$y=0
$graphics.FillRectangle($bg,$x,$y,$bmp.Width,$bmp.Height)
$graphics.DrawString($text,$font,$fg,10,10)
$graphics.Dispose()
 
if (Test-Path -Path $path) { Remove-Item -Path $path }
$bmp.Save($path)
 
Get-Item -Path $path
 
}
 
 
#Set-WindowsWallpaper
Add-Type -AssemblyName System.Drawing
 
Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int
{
Tile, Center, Stretch, NoChange
}
 
public class Setter {
public const int SetDesktopWallpaper = 0x14; // SPI_SETDESKWALLPAPER
public const int UpdateIniFile = 0x01; // SPIF_UPDATEINIFILE
public const int SendWinIniChange = 0x02; // SPIF_SENDWININICHANGE
 
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
 
public static void SetWallpaper ( string path, Wallpaper.Style style ) {
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
switch( style )
{
case Style.Stretch :
key.SetValue(@"WallpaperStyle", "2") ;
key.SetValue(@"TileWallpaper", "0") ;
break;
case Style.Center :
key.SetValue(@"WallpaperStyle", "1") ;
key.SetValue(@"TileWallpaper", "0") ;
break;
case Style.Tile :
key.SetValue(@"WallpaperStyle", "1") ;
key.SetValue(@"TileWallpaper", "1") ;
break;
case Style.NoChange :
break;
}
key.Close();
}
}
}
"@
 
 
 
### args for New-BMPfileWithText
$path="$HOME\Test.bmp"
$text="$ENV:COMPUTERNAME"
$bgcolor = [drawing.color]::DarkOrange
$bgcolor = [drawing.color]::DarkSlateBlue
 
$fontsize=9
$bmpsize=New-Object -TypeName System.Drawing.Size -ArgumentList 230,70
###
 
# Generate the BMP file
$BMPfile=New-BMPfileWithText -path $path -Text $text -bgColour $bgcolor -fontsize $fontsize -BMPsize $bmpsize
 
 
### now pass the generated BMP file and set the wallpaper.
$Tile = 0
$Center = 1
$Stretch = 2
$NoChange = 3
 
[Wallpaper.Setter]::SetWallpaper( $BMPfile.Fullname, $Tile)

Comments