PoshCode Archive  Artifact [c26c220502]

Artifact c26c22050216c210e3b72c0786b05e10810de6febccf5a8c9f141909dfe942c2:

  • File Set-Wallpaper-CTP2.ps1 — part of check-in [e7a5f102cb] at 2018-06-10 13:47:25 on branch trunk — Set-Wallpaper lets you set your windows desktop wallpaper, requires PInvoke and I wrote it using CTP2’s Add-Type, although it could be done in v1 using the JIT code generation tricks Lee Holmes has mentioned in the past … (user: unknown size: 2656)

# encoding: ascii
# api: csharp
# title: Set-Wallpaper (CTP2)
# description: Set-Wallpaper lets you set your windows desktop wallpaper, requires PInvoke and I wrote it using CTP2’s Add-Type, although it could be done in v1 using the JIT code generation tricks Lee Holmes has mentioned in the past …
# version: 0.1
# type: class
# license: CC0
# x-poshcode-id: 488
# x-derived-from-id: 491
# x-archived: 2009-01-05T16:19:59
#
#
#requires -version 2.0
## Set-Wallpaper - set your windows desktop wallpaper
###################################################################################################
## Usage:
##    Set-Wallpaper "C:\Users\Joel\Pictures\Others Stock\Potter Wasp.jpg" "Stretched"
##    ls *.jpg | get-random | Set-Wallpaper
###################################################################################################

add-type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
   public enum Style : int
   {
       Tiled, Centered, Stretched
   }


   public class Setter {
      public const int SetDesktopWallpaper = 20;
      public const int UpdateIniFile = 0x01;
      public const int SendWinIniChange = 0x02;

      [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.Stretched :
                 key.SetValue(@"WallpaperStyle", "2") ; 
                 key.SetValue(@"TileWallpaper", "0") ;
                 break;
             case Style.Centered :
                 key.SetValue(@"WallpaperStyle", "1") ; 
                 key.SetValue(@"TileWallpaper", "0") ; 
                 break;
             case Style.Tiled :
                 key.SetValue(@"WallpaperStyle", "1") ; 
                 key.SetValue(@"TileWallpaper", "1") ;
                 break;
         }
         key.Close();
      }
   }
}
"@

cmdlet Set-Wallpaper {
Param(
   [Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
   [Alias("FullName")]
   [string]
   $Path
,
   [Parameter(Position=1, Mandatory=$true)]
   [Wallpaper.Style]
   $Style
)
   [Wallpaper.Setter]::SetWallpaper( (Convert-Path $Path), $Style )
}