Skip to content

Instantly share code, notes, and snippets.

@vabock
Created March 31, 2011 22:43
Show Gist options
  • Save vabock/897417 to your computer and use it in GitHub Desktop.
Save vabock/897417 to your computer and use it in GitHub Desktop.
指定されたフォルダから*.BMPをランダムに選ぶ壁紙チェンジャー
namespace chwp
import System
import System.Text
#import System.Collections
import System.Runtime.InteropServices
import System.IO
import Microsoft.Win32
#import System.Linq.Enumerable
class Changer:
private static final SPI_SETDESKWALLPAPER as uint = 0x0014
private static final SPI_GETDESKWALLPAPER as uint = 0x0073
private static final SPIF_SENDWININICHANGE as uint = 0x02
[DllImport("user32.dll", SetLastError: true, CharSet: CharSet.Auto)]
private static def SystemParametersInfo(uiAction as uint, uiParam as uint,
pvParam as String, fWinIni as uint) as int:
pass
[DllImport("user32.dll", EntryPoint: "SystemParametersInfo", SetLastError: true, CharSet: CharSet.Auto)]
public static def SystemParametersInfoGet(uiAction as uint, uiParam as uint,
pvParam as StringBuilder, fWinIni as uint) as int:
pass
private static final MB_OK as int = 0
[DllImport("user32.dll", CharSet: CharSet.Auto)]
private static def MessageBox(hWnd as IntPtr, text as String,
caption as String, options as int) as uint:
pass
private def GetCurrentWallPaper() as string:
path = StringBuilder(255)
SystemParametersInfoGet(SPI_GETDESKWALLPAPER, path.Capacity, path, 0)
return FileInfo(path.ToString()).FullName
private def SetWallPaper(path as string):
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_SENDWININICHANGE)
SetRegistry(path)
private def ShowMessage(msg as string):
ShowMessage(msg, 'chwp')
private def ShowMessage(msg as string, caption as string):
MessageBox(0, msg, caption, MB_OK)
private def SetRegistry(path as string):
r = Registry.CurrentUser.OpenSubKey("""Control Panel\Desktop""", true)
r.SetValue('Wallpaper', path)
#ShowMessage(w)
public def Run(folder as string):
current = GetCurrentWallPaper().ToLower()
di = DirectoryInfo(folder)
if not di.Exists:
ShowMessage('フォルダが存在しません')
return
ifdef DEBUG:
ShowMessage(current, '現在')
bmp = [x.FullName for x in di.GetFiles('*.bmp') if x.FullName.ToLower() != current]
if len(bmp) == 0:
ShowMessage('指定されたフォルダに選択可能なBMPファイルが見つかりません')
return
#a = bmp.Cast[of string]().ToArray()
#MessageBox(0, String.Join('\n', a), 'テスト', MB_OK)
rnd = Random()
idx = rnd.Next(len(bmp))
ifdef DEBUG:
ShowMessage(bmp[idx], '設定')
SetWallPaper(bmp[idx])
public def NoFolder():
ShowMessage('フォルダを指定してください')
[STAThread]
public def Main(argv as (string)) as void:
changer = Changer()
ifdef DEBUG:
changer.Run("""N:\Windows""")
return
if len(argv) == 0:
changer.NoFolder()
else:
changer.Run(argv[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment