Results 1 to 7 of 7

Thread: Help with changing desktop wallpaper...Almost there.

  1. #1

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

    Help with changing desktop wallpaper...Almost there.

    Hello everyone,

    I am using this code below to set the desktop wallpaper. I need a way to actually apply the setting though. It properly sets the image because when you right-click the desktop, select properties, and click on the Desktop tab, it shows the image I set. But not until you click apply on that dialog will it actually change the image. Is there a way to tell windows that there was a change of this registry setting and apply it in code? (the code is in C#, but the only difference between this and VB is the declaration of the registry object)

    Please help.
    Code:
    RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel", true);
    
    try
    {
         key = key.OpenSubKey("Desktop", true);
    
         key.SetValue("Wallpaper", sPathOfImage);
    }
    catch
    {
        // Something went wrong.
        MessageBox.Show("Something went wrong");
    }

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Why dont you try refreshing the desktop? Its a long shot but that might help.
    Dont gain the world and lose your soul

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Does this help?

    http://www.geocities.com/siliconvall...Tips/tip11.htm

    If not here are some more:
    http://www.codehound.com/vb/results/...ktop+wallpaper

    Its API but you can do that in .NET, or was that what you are trying to avoid?

  4. #4
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I really don't think you want to do it by registry edit, there's an OS function call to set the wallpaper:

    ms-help://MS.VSCC/MS.MSDNVS/sysinfo/sysinfo_4p67.htm

    SPI_SETDESKWALLPAPER Sets the desktop wallpaper. The value of the pvParam parameter determines the new wallpaper. To specify a wallpaper bitmap, set pvParam to point to a null-terminated string containing the name of a bitmap file. Setting pvParam to "" removes the wallpaper. Setting pvParam to SETWALLPAPER_DEFAULT or NULL reverts to the default wallpaper.

    (snip rest - Edneeis' example is a lot more concise than the one I found in MSDN and is in VB to boot)

  5. #5

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Yes, I was trying to avoid the API if at all possible, but I will use it if I have to. I was just wondering if there was anything in the framework that would allow me to do this without the API.

    Refreshing the desktop doesn't work.

    Thanks for the links, I will probably be going with the API unless someone can come up with something.

  6. #6

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Just to let you know, I ended up using the API call to do this. For anyone interested, here is the code I used (it is in C#):
    Code:
    [System.Runtime.InteropServices.DllImportAttribute("user32")]
    private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
    private void ImageToWallpaper(string myPicPath)
    {
       // This only works with bitmaps, so we need to convert the image to
       // a bmp file before it will work.  I am saving it in the C:\Windows folder
       Bitmap image = new Bitmap(myPicPath);
       string path = @"C:\Windows\" + System.IO.Path.GetFileNameWithoutExtension(myPicPath);
       image.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
    
       // Now make the call to change the wallpaper.
       int returncode;
       returncode = SystemParametersInfo(20,0,path,0);
    
       // We still need this.  If we don't set the registry key
       // you won't see your image you just set in the properties window
       // when you right-click the desktop.
       RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel", true);
    
       try
       {
           key = key.OpenSubKey("Desktop", true);
    
           key.SetValue("Wallpaper", path);
       }
       catch
       {
           // Something went wrong.
           MessageBox.Show("Something went wrong");
       }
    }

  7. #7
    New Member
    Join Date
    Feb 2002
    Posts
    1
    Here is the code in VB.NET

    Imports Microsoft.Win32
    Imports System.Runtime.InteropServices

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim imagepath As String = "X:\Path\ImageName"
    Dim Key As RegistryKey
    Dim returncode As Integer

    returncode = SystemParametersInfo(20, 0, imagepath, 0)
    Key = Registry.CurrentUser.OpenSubKey("Control Panel", True)
    Try
    Key = Key.OpenSubKey("Desktop", True)
    Key.SetValue("Wallpaper", imagepath)
    Catch
    MsgBox("Error")
    End Try
    End Sub

    <DllImport("user32")> _
    Private Shared Function _
    SystemParametersInfo(ByVal uAction As Integer, ByVal uParam As Integer, _
    ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
    ' Leave the body of the function empty.
    End Function
    Lorne Green.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width