user's current wallpaper
-
How do I get the user's current wallpaper bitmap? (keeping in mind that the user might have set the wallpaper with either the desktop properties control panel applet, or with the SetParametersInfo method in user32.dll) Is there an API call that returns a bitmap or a handle that I can use to create a Bitmap object? I'm not wanting to change or alter the wallpaper, I'm wanting to display it as a Bitmap object on a Form.
-Daniel Typing too fast fro my owngood
-
How do I get the user's current wallpaper bitmap? (keeping in mind that the user might have set the wallpaper with either the desktop properties control panel applet, or with the SetParametersInfo method in user32.dll) Is there an API call that returns a bitmap or a handle that I can use to create a Bitmap object? I'm not wanting to change or alter the wallpaper, I'm wanting to display it as a Bitmap object on a Form.
-Daniel Typing too fast fro my owngood
Microsoft.Win32 is used for accessing the registry, while System.Runtime.InteropServices is used for accessing the unmanaged user32.dll. Next comes the preparation of the unmanaged function SystemParametersInfo() - this should be located at the top of the:) class definition:
[DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
First thing is to retrieve the current desktop wallpaper, and to do that we don't need to use a function, but simply read a value in the Windows registry. Let's create the method for that:private string GetCurrentWallpaper() { // The current wallpaper path is stored in the registry at HKEY_CURRENT_USER\\Control Panel\\Desktop\\WallPaper RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false); string WallpaperPath = rkWallPaper.GetValue("WallPaper").ToString(); rkWallPaper.Close(); // Return the current wallpaper path return WallpaperPath; }
Next comes the definition of the method that actually sets the wallpaper, Additional for you . And it's only a few lines of code: private void SetWallpaper(string WallpaperLocation, int WallpaperStyle, int TileWallpaper) { // Sets the actual wallpaper SystemParametersInfo(20, 0, WallpaperLocation, 0x01 | 0x02); // Set the wallpaper style to streched (can be changed to tile, center, maintain aspect ratio, etc. RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true); // Sets the wallpaper style rkWallPaper.SetValue("WallpaperStyle", WallpaperStyle); // Whether or not this wallpaper will be displayed as a tile rkWallPaper.SetValue("TileWallpaper", TileWallpaper); rkWallPaper.Close(); } :) -
Microsoft.Win32 is used for accessing the registry, while System.Runtime.InteropServices is used for accessing the unmanaged user32.dll. Next comes the preparation of the unmanaged function SystemParametersInfo() - this should be located at the top of the:) class definition:
[DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
First thing is to retrieve the current desktop wallpaper, and to do that we don't need to use a function, but simply read a value in the Windows registry. Let's create the method for that:private string GetCurrentWallpaper() { // The current wallpaper path is stored in the registry at HKEY_CURRENT_USER\\Control Panel\\Desktop\\WallPaper RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false); string WallpaperPath = rkWallPaper.GetValue("WallPaper").ToString(); rkWallPaper.Close(); // Return the current wallpaper path return WallpaperPath; }
Next comes the definition of the method that actually sets the wallpaper, Additional for you . And it's only a few lines of code: private void SetWallpaper(string WallpaperLocation, int WallpaperStyle, int TileWallpaper) { // Sets the actual wallpaper SystemParametersInfo(20, 0, WallpaperLocation, 0x01 | 0x02); // Set the wallpaper style to streched (can be changed to tile, center, maintain aspect ratio, etc. RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true); // Sets the wallpaper style rkWallPaper.SetValue("WallpaperStyle", WallpaperStyle); // Whether or not this wallpaper will be displayed as a tile rkWallPaper.SetValue("TileWallpaper", TileWallpaper); rkWallPaper.Close(); } :)I only needed to get the wallpaper. I didn't know that the registry's value was updated that well (I just looked on mine and it's correct, despite me circumventing the control panel applet), so I'm good to go. I already know how to set the wallpaper (and I have a handy app available for free download from my blog). Thank you for the info! It helps me greatly!
-Daniel Typing too fast fro my owngood