From 405976b7a635b3f63ebcca39233b3a8d9d814a4c Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sat, 4 Feb 2012 15:54:30 +0000 Subject: [PATCH] Added !SetWallpaper bang. Usage: !SetWallpaper [File] (Style) where style is CENTER, TILE, STRETCH, FIT, or FILL. FIT/FILL available only on Windows 7 (and higher). --- Library/Rainmeter.cpp | 28 ++++++++++++++ Library/Rainmeter.h | 13 ++----- Library/System.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++ Library/System.h | 1 + 4 files changed, 121 insertions(+), 9 deletions(-) diff --git a/Library/Rainmeter.cpp b/Library/Rainmeter.cpp index 9c006c37..bd7f9786 100644 --- a/Library/Rainmeter.cpp +++ b/Library/Rainmeter.cpp @@ -420,6 +420,30 @@ void CRainmeter::Bang_SetClip(const WCHAR* arg) } } +/* +** Bang_SetWallpaper +** +** !SetWallpaper bang +** +*/ +void CRainmeter::Bang_SetWallpaper(const WCHAR* arg) +{ + std::vector subStrings = ParseString(arg); + + if (subStrings.size() == 1) + { + CSystem::SetWallpaper(subStrings[0], L""); + } + else if (subStrings.size() == 2) + { + CSystem::SetWallpaper(subStrings[0], subStrings[1]); + } + else + { + Log(LOG_ERROR, L"!SetWallpaper: Invalid parameters"); + } +} + /* ** Bang_SkinMenu ** @@ -1756,6 +1780,10 @@ void CRainmeter::ExecuteBang(const std::wstring& name, std::wstring& arg, CMeter { Bang_SetClip(args); } + else if (_wcsicmp(bang, L"SetWallpaper") == 0) + { + Bang_SetWallpaper(args); + } else if (_wcsicmp(bang, L"About") == 0) { CDialogAbout::Open(args); diff --git a/Library/Rainmeter.h b/Library/Rainmeter.h index fc16d8f2..f7c01c4e 100644 --- a/Library/Rainmeter.h +++ b/Library/Rainmeter.h @@ -221,6 +221,7 @@ private: void Bang_ToggleConfig(const WCHAR* arg); void Bang_DeactivateConfigGroup(const WCHAR* arg); void Bang_SetClip(const WCHAR* arg); + void Bang_SetWallpaper(const WCHAR* arg); void Bang_SkinMenu(const WCHAR* arg); void Bang_TrayMenu(); void Bang_WriteKeyValue(const WCHAR* arg, CMeterWindow* meterWindow); @@ -309,17 +310,11 @@ private: GlobalConfig m_GlobalConfig; }; -#ifdef LIBRARY_EXPORTS -#define EXPORT_PLUGIN __declspec(dllexport) -#else -#define EXPORT_PLUGIN __declspec(dllimport) -#endif - extern "C" { - EXPORT_PLUGIN int Initialize(HWND hWnd, HINSTANCE hInstance, LPCWSTR lpCmdLine); - EXPORT_PLUGIN void Quit(); - EXPORT_PLUGIN void ExecuteBang(LPCTSTR szBang); + int Initialize(HWND hWnd, HINSTANCE hInstance, LPCWSTR lpCmdLine); + void Quit(); + void ExecuteBang(LPCTSTR szBang); } #endif diff --git a/Library/System.cpp b/Library/System.cpp index af4c8a51..adbb4e7c 100644 --- a/Library/System.cpp +++ b/Library/System.cpp @@ -24,6 +24,8 @@ #include "MeasureNet.h" #include "Error.h" +using namespace Gdiplus; + #define DEBUG_VERBOSE (0) // Set 1 if you need verbose logging. #define ZPOS_FLAGS (SWP_NOMOVE | SWP_NOSIZE | SWP_NOOWNERZORDER | SWP_NOACTIVATE | SWP_NOSENDCHANGING) @@ -1176,6 +1178,92 @@ void CSystem::SetClipboardText(const std::wstring& text) } } +/* +** SetWallpaper +** +** Sets the system wallpapar. +** +*/ +void CSystem::SetWallpaper(const std::wstring& wallpaper, const std::wstring& style) +{ + auto setWallpaperStyle = [&]() + { + if (!style.empty()) + { + HKEY hKey; + if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Control Panel\\Desktop", 0, KEY_SET_VALUE, &hKey) == ERROR_SUCCESS) + { + const WCHAR* wallStyle = NULL; + const WCHAR* wallTile = L"0"; + + const WCHAR* option = style.c_str(); + if (_wcsicmp(option, L"CENTER") == 0) + { + wallStyle = L"0"; + } + else if (_wcsicmp(option, L"TILE") == 0) + { + wallStyle = L"0"; + wallTile = L"1"; + } + else if (_wcsicmp(option, L"STRETCH") == 0) + { + wallStyle = L"2"; + } + else if (GetOSPlatform() >= OSPLATFORM_7) + { + if (_wcsicmp(option, L"FIT") == 0) + { + wallStyle = L"6"; + } + else if (_wcsicmp(option, L"FILL") == 0) + { + wallStyle = L"10"; + } + } + + if (wallStyle) + { + RegSetValueEx(hKey, L"WallpaperStyle", 0, REG_SZ, (const BYTE*)wallStyle, sizeof(WCHAR) * 2); + RegSetValueEx(hKey, L"TileWallpaper", 0, REG_SZ, (const BYTE*)wallTile, sizeof(WCHAR) * 2); + } + else + { + Log(LOG_ERROR, L"!SetWallpaper: Invalid style"); + } + + RegCloseKey(hKey); + } + } + }; + + if (!wallpaper.empty()) + { + if (GetOSPlatform() < OSPLATFORM_7) + { + // Gotta convert to .bmp for pre-7 + Bitmap* bitmap = Bitmap::FromFile(wallpaper.c_str()); + if (bitmap && bitmap->GetLastStatus() == Ok) + { + std::wstring file = Rainmeter->GetSettingsPath(); + file += L"Wallpaper.bmp"; + + const CLSID jpegClsid = { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } }; + if (bitmap->Save(file.c_str(), &jpegClsid) == Ok) + { + setWallpaperStyle(); + SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)file.c_str(), SPIF_UPDATEINIFILE); + } + } + } + else + { + setWallpaperStyle(); + SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)wallpaper.c_str(), SPIF_UPDATEINIFILE); + } + } +} + /* ** CopyFiles ** diff --git a/Library/System.h b/Library/System.h index c239ea57..82b97382 100644 --- a/Library/System.h +++ b/Library/System.h @@ -82,6 +82,7 @@ public: static void ResetWorkingDirectory(); static void SetClipboardText(const std::wstring& text); + static void SetWallpaper(const std::wstring& wallpaper, const std::wstring& style); static bool CopyFiles(const std::wstring& strFrom, const std::wstring& strTo, bool bMove = false); static bool RemoveFile(const std::wstring& file);