SystemParametersInfoにSPI_SETDESKWALLPAPERを指定して呼び出すことで、指定した画像ファイルを壁紙として設定することができる。
また、レジストリのHKEY_CURRENT_USER\Control Panel\Desktop\にある次のキーの値を変えることで、壁紙の表示方法も変更することができる。 キーWallpaperStyleとTileWallpaperの値と、表示方法の組み合わせは次のとおり。
WallpaperStyle | TileWallpaper | 表示方法 | 備考 |
---|---|---|---|
0 | 0 | 中央に表示 | |
0 | 1 | 並べて表示 | |
2 | 0 | 拡大して表示/画面に合わせて伸縮 (アスペクト比を維持せず、画面全体の大きさにあわせて拡大) |
|
6 | 0 | ページ縦幅に合わせる (アスペクト比を維持し、画面全体の大きさにあわせて拡大、画像が画面に収まるよう余白が入る) |
Windows 7以降 |
10 | 0 | ページ横幅に合わせる (アスペクト比を維持し、画面全体の大きさにあわせて拡大、画面に収まらない部分ははみ出す) |
Windows 7以降 |
以下のコードは、壁紙を変更する処理をラップしたクラスと、それを使ってさまざまな表示方法を試すサンプル。
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Win32;
class Sample {
static void Main()
{
const string file = @"wallpaper.bmp";
foreach (var style in new[] {
WallpaperStyle.Center,
WallpaperStyle.Tile,
WallpaperStyle.Stretch,
WallpaperStyle.ResizeFit,
WallpaperStyle.ResizeFill,
}) {
Wallpaper.SetWallpaper(file, style);
Thread.Sleep(2500);
}
Wallpaper.UnsetWallpaper();
}
}
/// <summary>壁紙の表示スタイル</summary>
class WallpaperStyle {
/// <summary>中央に表示</summary>
public static readonly WallpaperStyle Center = new WallpaperStyle(0, 0);
/// <summary>並べて表示</summary>
public static readonly WallpaperStyle Tile = new WallpaperStyle(0, 1);
/// <summary>拡大して表示 (画面に合わせて伸縮)</summary>
public static readonly WallpaperStyle Stretch = new WallpaperStyle(2, 0);
/// <summary>リサイズして表示 (ページ縦幅に合わせる)</summary><remarks>Windows 7以降のみ</remarks>
public static readonly WallpaperStyle ResizeFit = new WallpaperStyle(6, 0);
/// <summary>リサイズして全体に表示 (ページ横幅に合わせる)</summary><remarks>Windows 7以降のみ</remarks>
public static readonly WallpaperStyle ResizeFill = new WallpaperStyle(10, 0);
internal readonly int _WallpaperStyle;
internal readonly int _TileWallpaper;
private WallpaperStyle(int wallpaperStyle, int tileWallpaper)
{
_WallpaperStyle = wallpaperStyle;
_TileWallpaper = tileWallpaper;
}
}
/// <summary>壁紙の設定を行うためのクラス</summary>
static class Wallpaper {
/// <summary>壁紙を設定する</summary>
/// <param name="file">壁紙として設定する画像ファイル</praram>
public static void SetWallpaper(string file, WallpaperStyle style)
{
if (string.IsNullOrEmpty(file))
throw new ArgumentException("ファイル名が空", "file");
if (style == null)
throw new ArgumentNullException("style");
// HKEY_CURRENT_USER\Control Panel\Desktopを開く
using (var regkeyDesktop = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true)) {
if (regkeyDesktop == null)
throw new Win32Exception(Marshal.GetLastWin32Error());
// TileWallpaperとWallpaperStyleを設定
regkeyDesktop.SetValue("TileWallpaper", style._TileWallpaper.ToString());
regkeyDesktop.SetValue("WallpaperStyle", style._WallpaperStyle.ToString());
// 「並べて表示」、「拡大して表示」などの原点も変えたい場合は、この値を0以外に変更する
regkeyDesktop.SetValue("WallpaperOriginX", "0");
regkeyDesktop.SetValue("WallpaperOriginY", "0");
// Wallpaperの値をセットすることでも壁紙を設定できるが、
// SystemParametersInfoを呼び出さないと、壁紙を設定しても即座には反映されない
//regkeyDesktop.SetValue("Wallpaper", file);
}
SetDeskWallpaper(file);
}
/// <summary>壁紙をはがす</summary>
public static void UnsetWallpaper()
{
// ファイル名に"\0"を指定すると、
// 壁紙をはがす(背景色のみの状態にする)ことができる
SetDeskWallpaper("\0");
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SystemParametersInfo(int uAction,
int uParam,
string lpvParam,
int fuWinIini);
private static void SetDeskWallpaper(string file)
{
const int SPI_SETDESKWALLPAPER = 0x0014; // デスクトップの壁紙を設定
const int SPIF_UPDATEINIFILE = 0x0001; // 設定を更新する
const int SPIF_SENDWININICHANGE = 0x0002; // 設定の更新を全てのアプリケーションに通知(WM_SETTIMGCHANGE)する
var flags = (Environment.OSVersion.Platform == PlatformID.Win32NT)
? SPIF_SENDWININICHANGE
: SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE;
// 壁紙となるファイルを設定する
// なお、XP以前の場合はBMP形式のみが指定可能、Vista/7では加えてJPEG形式も指定可能
if (!SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, file, flags))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
Imports System
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Threading
Imports Microsoft.Win32
Class Sample
Public Shared Sub Main()
Const file As String = "wallpaper.bmp"
For Each style As WallpaperStyle In New WallpaperStyle() { _
WallpaperStyle.Center, _
WallpaperStyle.Tile, _
WallpaperStyle.Stretch, _
WallpaperStyle.ResizeFit, _
WallpaperStyle.ResizeFill _
}
Wallpaper.SetWallpaper(file, style)
Thread.Sleep(2500)
Next
Wallpaper.UnsetWallpaper()
End Sub
End Class
''' <summary>壁紙の表示スタイル</summary>
Class WallpaperStyle
''' <summary>中央に表示</summary>
Public Shared ReadOnly Center As New WallpaperStyle(0, 0)
''' <summary>並べて表示</summary>
Public Shared ReadOnly Tile As New WallpaperStyle(0, 1)
''' <summary>拡大して表示 (画面に合わせて伸縮)</summary>
Public Shared ReadOnly Stretch As New WallpaperStyle(2, 0)
''' <summary>リサイズして表示 (ページ縦幅に合わせる)</summary><remarks>Windows 7以降のみ</remarks>
Public Shared ReadOnly ResizeFit As New WallpaperStyle(6, 0)
''' <summary>リサイズして全体に表示 (ページ横幅に合わせる)</summary><remarks>Windows 7以降のみ</remarks>
Public Shared ReadOnly ResizeFill As New WallpaperStyle(10, 0)
Friend ReadOnly _WallpaperStyle As Integer
Friend ReadOnly _TileWallpaper As Integer
Private Sub New(ByVal wallpaperStyle As Integer, ByVal tileWallpaper As Integer)
_WallpaperStyle = wallpaperStyle
_TileWallpaper = tileWallpaper
End Sub
End Class
''' <summary>壁紙の設定を行うためのクラス</summary>
Module Wallpaper
''' <summary>壁紙を設定する</summary>
''' <param name="file">壁紙として設定する画像ファイル</praram>
Public Sub SetWallpaper(ByVal file As String, ByVal style As WallpaperStyle)
If String.IsNullOrEmpty(file) Then Throw New ArgumentException("ファイル名が空", "file")
If style Is Nothing Then Throw New ArgumentNullException("style")
' HKEY_CURRENT_USER\Control Panel\Desktopを開く
Using regkeyDesktop As RegistryKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
If regkeyDesktop Is Nothing Then Throw New Win32Exception(Marshal.GetLastWin32Error())
' TileWallpaperとWallpaperStyleを設定
regkeyDesktop.SetValue("TileWallpaper", style._TileWallpaper.ToString())
regkeyDesktop.SetValue("WallpaperStyle", style._WallpaperStyle.ToString())
' 「並べて表示」、「拡大して表示」などの原点も変えたい場合は、この値を0以外に変更する
regkeyDesktop.SetValue("WallpaperOriginX", "0")
regkeyDesktop.SetValue("WallpaperOriginY", "0")
' Wallpaperの値をセットすることでも壁紙を設定できるが、
' SystemParametersInfoを呼び出さないと、壁紙を設定しても即座には反映されない
'regkeyDesktop.SetValue("Wallpaper", file)
End Using
SetDeskWallpaper(file)
End Sub
''' <summary>壁紙をはがす</summary>
Public Sub UnsetWallpaper()
' ファイル名にvbNullCharを指定すると、
' 壁紙をはがす(背景色のみの状態にする)ことができる
SetDeskWallpaper(vbNullChar)
End Sub
<DllImport("user32.dll", SetLastError := True)> _
Private Function SystemParametersInfo(ByVal uAction As Integer, _
ByVal uParam As Integer, _
ByVal lpvParam As String, _
ByVal fuWinIini As Integer) As Boolean
End Function
Private Sub SetDeskWallpaper(ByVal file As String)
Const SPI_SETDESKWALLPAPER As Integer = &h0014 ' デスクトップの壁紙を設定
Const SPIF_UPDATEINIFILE As Integer = &h0001 ' 設定を更新する
Const SPIF_SENDWININICHANGE As Integer = &h0002 ' 設定の更新を全てのアプリケーションに通知(WM_SETTIMGCHANGE)する
Dim flags As Integer = SPIF_SENDWININICHANGE
If Environment.OSVersion.Platform <> PlatformID.Win32NT Then
flags = flags Or SPIF_UPDATEINIFILE
End If
' 壁紙となるファイルを設定する
' なお、XP以前の場合はBMP形式のみが指定可能、Vista/7では加えてJPEG形式も指定可能
If Not SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, file, flags) Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
End Sub
End Module
なお、マルチディスプレイ環境では、個々の画面に別々の壁紙を設定する方法がないので、壁紙を変更する (マルチディスプレイ対応版)で解説しているような手段をとる必要がある。