|
-
Feb 7th, 2003, 11:29 PM
#1
Thread Starter
PowerPoster
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:
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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPic As String = "YourPicture.jpg" 'This can be a gif, bmp, or jpg and probably others.
Dim image As Bitmap = New Bitmap(myPic)
'Might need to change the hard coded path to find the actual windows directory.
Dim path As String = "C:\Windows\" + System.IO.Path.GetFileNameWithoutExtension(myPic)
'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.
Dim ext As System.IO.FileInfo = New System.IO.FileInfo(myPic)
If ext.Extension.ToUpper <> "BMP" Then
image.Save(path, System.Drawing.Imaging.ImageFormat.Bmp)
End If
'Now make the call to change the wallpaper.
Dim returncode As Integer
returncode = SystemParametersInfo(20, 0, path, 0)
'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.
Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Control Panel", True)
Try
key = key.OpenSubKey("Desktop", True)
key.SetValue("Wallpaper", path)
Catch
'An error happened when setting the registry setting.
'Do what you want here.
End Try
'C# CODE FOR CHANGING THE WALLPAPER:
'AUTHOR Brian Russell AKA hellswraith on VBForums.com
'
'// This is the API declaration for the call to set the wallpaper.
'[System.Runtime.InteropServices.DllImportAttribute("user32")]
'private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam,
' int fuWinIni);
'private void ImageToWallpaper()
'{
' // 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
' string myPicPath = "YourPicture.jpg"
' 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");
' }
'}
End Sub
-
Feb 8th, 2003, 12:02 AM
#2
-
Feb 8th, 2003, 12:07 AM
#3
-
Feb 8th, 2003, 08:03 AM
#4
Hyperactive Member
hellswraith, can I add this to the VBCodeBook.NET CodePack?
-
Feb 8th, 2003, 10:17 AM
#5
Frenzied Member
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

-
Feb 8th, 2003, 10:53 AM
#6
Frenzied Member
Never mind. I fixed it.
VB Code:
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
Private Sub btnChangeWallpaper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeWallpaper.Click
'This can be a gif, bmp, or jpg and probably others.
Dim sNewWallpaper As String = System.Environment.CurrentDirectory & "\ChangeWallpaper-01.jpg"
Dim bResult As Boolean = fc_ChangeWallpaper(sNewWallpaper)
If bResult = False Then MsgBox("Failed.")
Beep()
End Sub
Private Function fc_ChangeWallpaper(ByVal sWallpaperPath As String) As Boolean
Dim bChangeStatus As Boolean = True
'Full path to the new wallpaper file on the hard drive, but without the file extension on the end
Dim sFileDirPath As String = System.IO.Path.GetDirectoryName(sWallpaperPath)
Dim sFileNameWithoutExtension As String = System.IO.Path.GetFileNameWithoutExtension(sWallpaperPath)
Dim sCompleteWallpaperPath As String = sFileDirPath & "\" & sFileNameWithoutExtension
'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.
Dim xFileInfo As System.IO.FileInfo = New System.IO.FileInfo(sWallpaperPath)
If xFileInfo.Extension.ToUpper <> "BMP" Then
'Converts the file into a BMP
Dim xBitmapImage As Bitmap = New Bitmap(sWallpaperPath)
sCompleteWallpaperPath &= ".bmp"
xBitmapImage.Save(sCompleteWallpaperPath, System.Drawing.Imaging.ImageFormat.Bmp)
End If
'Now make the call to change the wallpaper.
Dim iReturnCode As Integer = SystemParametersInfo(20, 0, sCompleteWallpaperPath, 0)
If iReturnCode <> 1 Then
bChangeStatus = False
Else
'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.
Dim xRegKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Control Panel", True)
Try
xRegKey = xRegKey.OpenSubKey("Desktop", True)
xRegKey.SetValue("Wallpaper", sCompleteWallpaperPath)
Catch
'An error happened when setting the registry setting. Do what you want here.
'bChangeStatus = False
End Try
End If
Return bChangeStatus
End Function
~Peter

-
Feb 8th, 2003, 11:12 AM
#7
Thread Starter
PowerPoster
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.
-
Feb 9th, 2003, 11:43 AM
#8
Frenzied Member
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

-
Feb 9th, 2003, 03:32 PM
#9
Hyperactive Member
Thanks, and both have been credited
-
Feb 4th, 2005, 05:00 AM
#10
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|