-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopUtils.cpp
More file actions
94 lines (77 loc) · 3.03 KB
/
DesktopUtils.cpp
File metadata and controls
94 lines (77 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
#include <vector>
#include <string>
#include <stdexcept>
#include "DesktopUtils.h"
// Callback function for EnumWindows
static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
// Retrieve the vector of HWNDs passed as lParam
std::vector<HWND>* workerwWindows = reinterpret_cast<std::vector<HWND>*>(lParam);
// Get the class name of the window
char className[256];
if (GetClassNameA(hwnd, className, sizeof(className))) {
// Check if the class name is "WorkerW"
if (std::string(className) == "WorkerW") {
workerwWindows->push_back(hwnd);
}
}
return TRUE; // Continue enumeration
}
// Function to create and retrieve the WorkerW layer
static HWND CreateWorkerWLayer() {
// Get the handle to the Progman window
HWND progman = FindWindowW(L"Progman", nullptr);
if (!progman) {
return nullptr;
}
// Send a message to Progman to spawn a WorkerW window
SendMessageTimeoutW(progman, 0x052C, 0, 0, SMTO_NORMAL, 1000, nullptr);
// Vector to store WorkerW windows
std::vector<HWND> workerwWindows;
// Enumerate all top-level windows
EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&workerwWindows));
// If we found WorkerW windows, return the last one
if (!workerwWindows.empty()) {
return workerwWindows.back();
}
return nullptr;
}
// Gets the location of the currently using wallpaper
wchar_t* GetCurrentWallpaper() {
const wchar_t* regKey = L"Control Panel\\Desktop";
const wchar_t* regValue = L"Wallpaper";
HKEY hKey;
// Open the registry key
LONG result = RegOpenKeyExW(HKEY_CURRENT_USER, regKey, 0, KEY_READ, &hKey);
if (result != ERROR_SUCCESS) {
throw std::runtime_error("Failed to open registry key, error code: " + std::to_string(result));
}
// Query the size of the value
DWORD valueSize = 0;
result = RegQueryValueExW(hKey, regValue, nullptr, nullptr, nullptr, &valueSize);
if (result != ERROR_SUCCESS) {
RegCloseKey(hKey);
throw std::runtime_error("Failed to query registry value size, error code: " + std::to_string(result));
}
// Allocate buffer for the value
wchar_t* valueBuffer = new wchar_t[valueSize / sizeof(wchar_t)];
result = RegQueryValueExW(hKey, regValue, nullptr, nullptr, reinterpret_cast<LPBYTE>(valueBuffer), &valueSize);
if (result != ERROR_SUCCESS) {
RegCloseKey(hKey);
delete[] valueBuffer; // Free allocated memory in case of failure
throw std::runtime_error("Failed to query registry value, error code: " + std::to_string(result));
}
// Close the registry key
RegCloseKey(hKey);
return valueBuffer;
}
// Function to set a window as the desktop background
void SetAsDesktop(HWND hwnd) {
// Call the function to create or retrieve the WorkerW layer
HWND workerw = CreateWorkerWLayer(); // Ensure this function is implemented as before
if (!workerw) {
return;
}
// Set the parent of the target window to WorkerW
SetParent(hwnd, workerw);
}