How to change the desktopwallpaper
-
Hello! How can I change my Windows Desktopwallpaper in C#? I need to see the result at once, so an refresh or so could also be nessesary, I don't know Thanx for any help Mario
-
Hello! How can I change my Windows Desktopwallpaper in C#? I need to see the result at once, so an refresh or so could also be nessesary, I don't know Thanx for any help Mario
Use the [Registry](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfmicrosoftwin32registryclasstopic.asp?frame=true target=) class to modify HKEY_CURRENT_USER\Control Panel\Desktop\WallPaper OR Use the Windows API function SystemParametersInfo(). You'll have to DllImport it from user32 and lookup the constants in winuser.h
-
Use the [Registry](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfmicrosoftwin32registryclasstopic.asp?frame=true target=) class to modify HKEY_CURRENT_USER\Control Panel\Desktop\WallPaper OR Use the Windows API function SystemParametersInfo(). You'll have to DllImport it from user32 and lookup the constants in winuser.h
Help! I got stuck at the moment. First I tried to change the registry value with: RegistryKey key = Registry.CurrentUser.OpenSubKey( "Control Panel\\Desktop" ); if (key==null) listBox1.Items.Add("--- Key not there!"); else { listBox1.Items.Add(key.GetValue("Wallpaper")); key.SetValue("Wallpaper","C:\\test.bmp"); } With GetValue I get the correct actual Wallpaper-filename, but when trying to write with SetValue I get a System.UnauthorizedAccessException "Cannot write to the registry key" ---------------------------------------- Then I tried to call the Windows API function with: [DllImport("user32.dll")] public static extern bool SystemParametersInfo (long uiAction, long uiParam, string pvParam, long fWinIni); (..) bool ReturnValue=false; ReturnValue=SystemParametersInfo(0x0014,0,"C:\\test.bmp",0x0002); After the call, the ReturnValue is true and the screen updates (flashes). But the desktop-wallpaper is still the same. The Bitmap "C:\\test.bmp" exists Any idea? Mario :confused:
-
Help! I got stuck at the moment. First I tried to change the registry value with: RegistryKey key = Registry.CurrentUser.OpenSubKey( "Control Panel\\Desktop" ); if (key==null) listBox1.Items.Add("--- Key not there!"); else { listBox1.Items.Add(key.GetValue("Wallpaper")); key.SetValue("Wallpaper","C:\\test.bmp"); } With GetValue I get the correct actual Wallpaper-filename, but when trying to write with SetValue I get a System.UnauthorizedAccessException "Cannot write to the registry key" ---------------------------------------- Then I tried to call the Windows API function with: [DllImport("user32.dll")] public static extern bool SystemParametersInfo (long uiAction, long uiParam, string pvParam, long fWinIni); (..) bool ReturnValue=false; ReturnValue=SystemParametersInfo(0x0014,0,"C:\\test.bmp",0x0002); After the call, the ReturnValue is true and the screen updates (flashes). But the desktop-wallpaper is still the same. The Bitmap "C:\\test.bmp" exists Any idea? Mario :confused:
Change all your longs to uint. You're passing 64bit values to a function that's expecting 32bit.
-
Change all your longs to uint. You're passing 64bit values to a function that's expecting 32bit.
Thanx! It works perfectly now (you immediately see the result). So for all who want to change the desktopwallpaper, the "final" code is: [DllImport("user32.dll")] public static extern bool SystemParametersInfo (uint uiAction, uint uiParam, string pvParam, uint fWinIni); ReturnValue=SystemParametersInfo(0x0014,0,"FILENAME",0x0002); Mario