Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to change the desktopwallpaper

How to change the desktopwallpaper

Scheduled Pinned Locked Moved C#
questioncsharphelptutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mario12345
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • M Mario12345

      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

      C Offline
      C Offline
      Chris Rickard
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • C Chris Rickard

        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

        M Offline
        M Offline
        Mario12345
        wrote on last edited by
        #3

        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:

        C 1 Reply Last reply
        0
        • M Mario12345

          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:

          C Offline
          C Offline
          Chris Rickard
          wrote on last edited by
          #4

          Change all your longs to uint. You're passing 64bit values to a function that's expecting 32bit.

          M 1 Reply Last reply
          0
          • C Chris Rickard

            Change all your longs to uint. You're passing 64bit values to a function that's expecting 32bit.

            M Offline
            M Offline
            Mario12345
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups