|
-
Sep 21st, 2002, 10:14 PM
#1
Thread Starter
PowerPoster
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");
}
-
Sep 22nd, 2002, 01:04 AM
#2
Frenzied Member
Why dont you try refreshing the desktop? Its a long shot but that might help.
Dont gain the world and lose your soul
-
Sep 22nd, 2002, 01:08 AM
#3
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?
-
Sep 22nd, 2002, 02:21 AM
#4
Fanatic Member
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)
-
Sep 22nd, 2002, 11:40 AM
#5
Thread Starter
PowerPoster
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.
-
Sep 22nd, 2002, 02:05 PM
#6
Thread Starter
PowerPoster
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");
}
}
-
Dec 18th, 2002, 12:02 AM
#7
New Member
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
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
|