| VB.NET | |
|
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 |
Imports System.Runtime.InteropServices Imports Microsoft.Win32 '------------------------------------------------------------------------------------- ' 名前 : WallpaperChanger ' 概要 : 壁紙の配置方法を設定するための列挙型 ' 割り当てられている数値はそのまま使うので変更しないこと '------------------------------------------------------------------------------------- Public Enum WallpaperLocation As Integer Center = 0 '中央に表示 Fill = 1 '全体に並べて表示 Stretch = 2 '拡大して表示 End Enum '------------------------------------------------------------------------------------- ' 名前 : WallpaperChanger ' 概要 : 壁紙を設定するメソッドを含むクラス '------------------------------------------------------------------------------------- Public Class WallpaperChanger ' システムに関するパラメータを取得/設定するAPI関数 <DllImport("user32")> _ Private Shared Function SystemParametersInfo _ (ByVal uAction As Integer, _ ByVal uParam As Integer, _ <MarshalAs(UnmanagedType.LPStr)> ByVal lpvParam As String, _ ByVal fuWinIni As Integer) _ As Integer End Function ' SystemParametersInfoの呼び出し時に使用する定数 Private Const SPI_SETDESKWALLPAPER As Integer = 20 'デスクトップの壁紙を設定 Private Const SPIF_UPDATEINIFILE As Integer = &H1 '更新する Private Const SPIF_SENDWININICHANGE As Integer = &H2 '全てのアプリケーションに通知して更新する '------------------------------------------------------------------------------------- ' 名前 : SetWallpaper() ' 概要 : bitmapFileで指定されたファイルを壁紙に設定する '------------------------------------------------------------------------------------- Public Shared Sub SetWallpaper(ByVal bitmapFile As String, ByVal style As WallpaperLocation) ' レジストリの値を変更 Dim regkeyDesktop As RegistryKey ' HKEY_CURRENT_USER\Control Panel\Desktopを開く regkeyDesktop = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True) If Not regkeyDesktop Is Nothing Then ' TileWallpaperを 0 にすると中央に表示、1 にすると全体に並べて表示される ' また、Wallpaperstyleを 2 にすると拡大して表示される。 ' さらに、レジストリに書き込むときは文字として書き込まなければならない If style = WallpaperLocation.Stretch Then regkeyDesktop.SetValue("TileWallpaper", CInt(0).ToString()) regkeyDesktop.SetValue("Wallpaperstyle", CInt(2).ToString()) Else regkeyDesktop.SetValue("TileWallpaper", CInt(style).ToString()) regkeyDesktop.SetValue("Wallpaperstyle", CInt(0).ToString()) End If ' 閉じる regkeyDesktop.Close() End If ' 壁紙となるファイルを指定し、更新する SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, bitmapFile, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE) End Sub '------------------------------------------------------------------------------------- ' 名前 : RemoveWallpaper() ' 概要 : 壁紙の削除する '------------------------------------------------------------------------------------- Public Shared Sub RemoveWallpaper() ' ファイル名にvbNullCharを渡すことで壁紙をはがすことができる SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, vbNullChar, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE) End Sub End Class |
| C# | |
|
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 |
using System; using System.Runtime.InteropServices; using Microsoft.Win32; //------------------------------------------------------------------------------------- // 名前 : WallpaperChanger // 概要 : 壁紙の配置方法を設定するための列挙型 // 割り当てられている数値はそのまま使うので変更しないこと //------------------------------------------------------------------------------------- public enum WallpaperLocation : int { Center = 0, // 中央に表示 Fill = 1, // 全体に並べて表示 Stretch = 2, // 拡大して表示 } //------------------------------------------------------------------------------------- // 名前 : WallpaperChanger // 概要 : 壁紙を設定するメソッドを含むクラス //------------------------------------------------------------------------------------- public class WallpaperChanger { // システムに関するパラメータを取得/設定するAPI関数 [DllImport("user32")] private extern static int SystemParametersInfo (int uAction, int uParam, [MarshalAs(UnmanagedType.LPStr)] string lpvParam, int fuWinIni); // SystemParametersInfoの呼び出し時に使用する定数 private const int SPI_SETDESKWALLPAPER = 20; // デスクトップの壁紙を設定 private const int SPIF_UPDATEINIFILE = 0x1; // 更新する private const int SPIF_SENDWININICHANGE = 0x2; // 全てのアプリケーションに通知して更新する //------------------------------------------------------------------------------------- // 名前 : SetWallpaper() // 概要 : bitmapFileで指定されたファイルを壁紙に設定する //------------------------------------------------------------------------------------- public static void SetWallpaper( string bitmapFile, WallpaperLocation style ) { // レジストリの値を変更 RegistryKey regkeyDesktop; // HKEY_CURRENT_USER\Control Panel\Desktopを開く regkeyDesktop = Registry.CurrentUser.OpenSubKey( "Control Panel\\Desktop", true ); if ( regkeyDesktop != null ) { // TileWallpaperを 0 にすると中央に表示、1 にすると全体に並べて表示される // また、Wallpaperstyleを 2 にすると拡大して表示される。 // さらに、レジストリに書き込むときは文字として書き込まなければならない if ( style == WallpaperLocation.Stretch ) { regkeyDesktop.SetValue( "TileWallpaper", (0).ToString()); regkeyDesktop.SetValue( "Wallpaperstyle", (2).ToString()); } else { regkeyDesktop.SetValue( "TileWallpaper", style.ToString()); regkeyDesktop.SetValue( "Wallpaperstyle", (0).ToString()); } // 閉じる regkeyDesktop.Close(); } // 壁紙となるファイルを指定し、更新する SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, bitmapFile, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE ); } //------------------------------------------------------------------------------------- // 名前 : RemoveWallpaper() // 概要 : 壁紙の削除する //------------------------------------------------------------------------------------- public static void RemoveWallpaper() { // ファイル名に"\0"を渡すことで壁紙をはがすことができる SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, "\0", SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE ); } } |