Results 1 to 10 of 10

Thread: MrPolite - Change Wallpaper Code

  1. #1

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

    MrPolite - Change Wallpaper Code

    Here is the wallpaper changing code you asked for.
    There is a problem with the VB translation I did, it has to do
    with the api call (I haven't used them in VB.Net till today with this). Maybe you can figure it out. If you do get it working,
    repost the working code here so all others can use it.

    If all else fails, you can use the C# code, it works fine
    on my computer.


    VB Code:
    1. Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByRef lpvParam As String, ByVal fuWinIni As Integer) As Integer
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim myPic As String = "YourPicture.jpg" 'This can be a gif, bmp, or jpg and probably others.
    5.         Dim image As Bitmap = New Bitmap(myPic)
    6.  
    7.         'Might need to change the hard coded path to find the actual windows directory.
    8.         Dim path As String = "C:\Windows\" + System.IO.Path.GetFileNameWithoutExtension(myPic)
    9.  
    10.         'This only works with bitmaps, so if it isn't a bmp file, it needs to be
    11.         'converted to one before we can change the wallpaper.
    12.         Dim ext As System.IO.FileInfo = New System.IO.FileInfo(myPic)
    13.         If ext.Extension.ToUpper <> "BMP" Then
    14.             image.Save(path, System.Drawing.Imaging.ImageFormat.Bmp)
    15.         End If
    16.  
    17.         'Now make the call to change the wallpaper.
    18.         Dim returncode As Integer
    19.         returncode = SystemParametersInfo(20, 0, path, 0)
    20.  
    21.         'Need to set the registry so that you can see the image when you right-click
    22.         'the desktop and choose properties and go to the background tab.
    23.         Dim key As Microsoft.Win32.RegistryKey
    24.         key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Control Panel", True)
    25.         Try
    26.             key = key.OpenSubKey("Desktop", True)
    27.             key.SetValue("Wallpaper", path)
    28.         Catch
    29.             'An error happened when setting the registry setting.
    30.             'Do what you want here.
    31.         End Try
    32.  
    33.  
    34.  
    35.         'C# CODE FOR CHANGING THE WALLPAPER:
    36.         'AUTHOR Brian Russell AKA hellswraith on VBForums.com
    37.         '
    38.         '// This is the API declaration for the call to set the wallpaper.
    39.         '[System.Runtime.InteropServices.DllImportAttribute("user32")]
    40.         'private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam,
    41.         '   int fuWinIni);
    42.  
    43.         'private void ImageToWallpaper()
    44.         '{
    45.         '   // This only works with bitmaps, so we need to convert the image to
    46.         '   // a bmp file before it will work.  I am saving it in the C:\Windows folder
    47.         '   string myPicPath = "YourPicture.jpg"
    48.         '   Bitmap image = new Bitmap(myPicPath);
    49.         '   string path = @"C:\Windows\" + System.IO.Path.GetFileNameWithoutExtension(myPicPath);
    50.  
    51.         '   image.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
    52.  
    53.         '   // Now make the call to change the wallpaper.
    54.         '   int returncode;
    55.         '   returncode = SystemParametersInfo(20,0,path,0);
    56.  
    57.         '   // We still need this.  If we don't set the registry key
    58.         '   // you won't see your image you just set in the properties window
    59.         '   // when you right-click the desktop.
    60.         '   RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel", true);
    61.  
    62.         '      Try
    63.         '   {
    64.         '       key = key.OpenSubKey("Desktop", true);
    65.  
    66.         '       key.SetValue("Wallpaper", path);
    67.         '   }
    68.         '      Catch
    69.         '   {
    70.         '       // Something went wrong.
    71.         '       MessageBox.Show("Something went wrong");
    72.         '   }
    73.         '}
    74.     End Sub

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Thanks alot hellswraith
    Umm I have a little trouble using this. When I try your code, nothing really happens on the desktop, but when I check the Display Properties, I can see that the wallpaper has been changed. So it doesn't update the desktop automatically?

    is that because of VB.NET code?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Talking Fixed!

    yay!! thanks for the C# code. I was really getting pissed at vb.net


    when you converted the C# one, you changed one of the parameters to byref. That's the problem:


    Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer



    and thanks again
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    hellswraith, can I add this to the VBCodeBook.NET CodePack?
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  5. #5
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    Code works fine.

    The part where it converts the file into a BMP is odd because the new bitmap doesn't have a .bmp extension. It has no file extension.
    ~Peter


  6. #6
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Wink

    Never mind. I fixed it.
    VB Code:
    1. Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
    2.  
    3.     Private Sub btnChangeWallpaper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeWallpaper.Click
    4.         'This can be a gif, bmp, or jpg and probably others.
    5.         Dim sNewWallpaper As String = System.Environment.CurrentDirectory & "\ChangeWallpaper-01.jpg"
    6.         Dim bResult As Boolean = fc_ChangeWallpaper(sNewWallpaper)
    7.         If bResult = False Then MsgBox("Failed.")
    8.         Beep()
    9.     End Sub
    10.  
    11.     Private Function fc_ChangeWallpaper(ByVal sWallpaperPath As String) As Boolean
    12.         Dim bChangeStatus As Boolean = True
    13.  
    14.         'Full path to the new wallpaper file on the hard drive, but without the file extension on the end
    15.         Dim sFileDirPath As String = System.IO.Path.GetDirectoryName(sWallpaperPath)
    16.         Dim sFileNameWithoutExtension As String = System.IO.Path.GetFileNameWithoutExtension(sWallpaperPath)
    17.         Dim sCompleteWallpaperPath As String = sFileDirPath & "\" & sFileNameWithoutExtension
    18.  
    19.         'This only works with bitmaps, so if it isn't a bmp file, it needs to be converted to one before we can change the wallpaper.
    20.         Dim xFileInfo As System.IO.FileInfo = New System.IO.FileInfo(sWallpaperPath)
    21.         If xFileInfo.Extension.ToUpper <> "BMP" Then
    22.             'Converts the file into a BMP
    23.             Dim xBitmapImage As Bitmap = New Bitmap(sWallpaperPath)
    24.             sCompleteWallpaperPath &= ".bmp"
    25.             xBitmapImage.Save(sCompleteWallpaperPath, System.Drawing.Imaging.ImageFormat.Bmp)
    26.         End If
    27.  
    28.         'Now make the call to change the wallpaper.
    29.         Dim iReturnCode As Integer = SystemParametersInfo(20, 0, sCompleteWallpaperPath, 0)
    30.         If iReturnCode <> 1 Then
    31.             bChangeStatus = False
    32.         Else
    33.             'Need to set the registry so that you can see the image when you right-click the desktop and choose properties and go to the background tab.
    34.             Dim xRegKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Control Panel", True)
    35.             Try
    36.                 xRegKey = xRegKey.OpenSubKey("Desktop", True)
    37.                 xRegKey.SetValue("Wallpaper", sCompleteWallpaperPath)
    38.             Catch
    39.                 'An error happened when setting the registry setting. Do what you want here.
    40.                 'bChangeStatus = False
    41.             End Try
    42.         End If
    43.  
    44.         Return bChangeStatus
    45.     End Function
    ~Peter


  7. #7

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Go ahead and use the code RealNickyDude, but use the fixed up one that MrGTI provided, it seems to work completely.

    I have been so deep in C# lately that my VB.Net skills are lacking. I will have to work on my conversions better...lol.
    Glad it worked for you.

  8. #8
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    If hellswraith said it was okay to us it (which he did), then go ahead. But you'd better give him the credit. I just tweaked it.
    ~Peter


  9. #9
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Thanks, and both have been credited
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  10. #10
    Lively Member sameer spitfire's Avatar
    Join Date
    Nov 2001
    Location
    India
    Posts
    120

    Question Re: MrPolite - Change Wallpaper Code

    The code is ok but the file does not get referesh
    when i click the property of desktop the new wallpaper is there but the image is not referesh

    what 2 do ?

    OS is windows XP
    __________________
    with regards
    sam
    with Regards
    sameer Mulgaonkar

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