1、桌面图标隐藏,本地电脑重启也生效。代码执行结果:之前隐藏,执行后显示,之前显示,执行后隐藏。
//桌面图标隐藏
#include <iostream>
#include <ShlObj.h>
void FindDesktopCom_Interface(REFIID riid, void** ppv)
{
long hwnd = NULL;
VARIANT vtLoc;
vtLoc.vt = VT_I4;
vtLoc.lVal = long(CSIDL_DESKTOP);
VARIANT vtEmpty;
vtEmpty.vt = VT_EMPTY;
IDispatch* pdisp = nullptr;
IServiceProvider* pProv = nullptr;
IShellBrowser* pBrowser = nullptr;
IShellView* pView = nullptr;
IFolderView2* p_View = nullptr;
HRESULT hr = CoInitialize(nullptr);
IShellWindows* pShellWindows = nullptr;
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_IShellWindows, (void**)&pShellWindows);
if (SUCCEEDED(hr))
{
hr = pShellWindows->FindWindowSW(&vtLoc, &vtEmpty, SWC_DESKTOP, &hwnd, SWFO_NEEDDISPATCH, &pdisp);
if (SUCCEEDED(hr))
{
hr = pdisp->QueryInterface(IID_IServiceProvider, (void**)&pProv);
if (SUCCEEDED(hr))
{
hr = pProv->QueryService(SID_STopLevelBrowser, IID_PPV_ARGS(&pBrowser));
if (SUCCEEDED(hr))
{
hr = pBrowser->QueryActiveShellView(&pView);
if (SUCCEEDED(hr))
{
hr = pView->QueryInterface(riid, ppv);
if (SUCCEEDED(hr))
{
}
}
}
}
}
}
}
}
int main()
{
IFolderView2* spView = nullptr;
FindDesktopCom_Interface(IID_PPV_ARGS(&spView));
DWORD flags = 0;
HRESULT hr;
if (spView)
{
hr = spView->GetCurrentFolderFlags(&flags);
DWORD res = flags & FWF_NOICONS;
hr = spView->SetCurrentFolderFlags(FWF_NOICONS, flags ^ FWF_NOICONS);
Sleep(3000);
hr = spView->GetCurrentFolderFlags(&flags);
hr = spView->SetCurrentFolderFlags(FWF_NOICONS, flags ^ FWF_NOICONS);
}
return 0;
}
2、隐藏任务栏,本地重启也生效。DWORD newByteValue = 0x03;// 0x02 显示;0x03 隐藏;
//隐藏任务栏
#include <iostream>
#include <windows.h>
#include <TlHelp32.h>
// 修改注册表二进制值的特定位
bool ModifyRegValueBit(HKEY hKey, DWORD bitPosition, DWORD newValue) {
LONG result;
DWORD type = REG_BINARY;
BYTE data[1024] = {0};
DWORD size = sizeof(data);
// 打开注册表键
result = RegOpenKeyEx(hKey, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StuckRects3", 0, KEY_ALL_ACCESS, &hKey);
if (result != ERROR_SUCCESS) {
std::cerr << "RegOpenKeyEx failed: " << result << std::endl;
return false;
}
// 查询注册表值
result = RegQueryValueEx(hKey, L"Settings", 0, &type, (LPBYTE)&data, &size);
if (result != ERROR_SUCCESS) {
std::cerr << "RegQueryValueEx failed: " << result << std::endl;
RegCloseKey(hKey);
return false;
}
//result = RegGetValueA(hKey, NULL, "Settings", RRF_RT_REG_SZ, &type, data, &size);
//if (result != ERROR_SUCCESS) {
// std::cerr << "无法查询注册表值" << result << std::endl;
// RegCloseKey(hKey);
// return false;
//}
// 确保请求的字节索引在数据范围内
if (bitPosition >= size) {
std::cerr << "Index out of bounds" << std::endl;
RegCloseKey(hKey);
return false;
}
data[bitPosition] = newValue;
// 设置注册表值
result = RegSetValueEx(hKey, L"Settings", 0, type, (const BYTE*)&data, size);
if (result != ERROR_SUCCESS) {
std::cerr << "RegSetValueEx failed: " << result << std::endl;
RegCloseKey(hKey);
return false;
}
// 关闭注册表键
RegCloseKey(hKey);
return true;
}
DWORD FindProcessId(const wchar_t* processName) {
DWORD processId = 0;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot) {
PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(processEntry);
if (Process32First(snapshot, &processEntry)) {
do {
if (!wcscmp(processEntry.szExeFile, processName)) {
processId = processEntry.th32ProcessID;
break;
}
} while (Process32Next(snapshot, &processEntry));
}
CloseHandle(snapshot);
}
return processId;
}
BOOL EndProcessById(DWORD processId) {
HANDLE process = OpenProcess(PROCESS_TERMINATE, FALSE, processId);
if (process == NULL) {
return FALSE;
}
BOOL result = TerminateProcess(process, 1);
CloseHandle(process);
return result;
}
int main() {
HKEY hKey = HKEY_CURRENT_USER;
DWORD bitPosition = 8; // 修改的第几位,从0开始计数
DWORD newByteValue = 0x02;// 0x02 显示;0x03 隐藏;
if (ModifyRegValueBit(hKey, bitPosition, newByteValue)) {
std::cout << "Reg Value Modified Successfully!" << std::endl;
DWORD explorerPid = FindProcessId(L"explorer.exe");
if (explorerPid != 0) {
EndProcessById(explorerPid);
// 等待资源管理器退出
Sleep(1000);
// 重新启动资源管理器
WinExec("explorer.exe", SW_SHOWNORMAL);
}
}
else {
std::cerr << "Failed to Modify Reg Value!" << std::endl;
}
return 0;
}
3、桌面更换壁纸
//桌面更换壁纸
#include <windows.h>
#include <shlobj.h>
#include <iostream>
bool SetWallpaper(const char* path) {
if (SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, (void*)path, SPIF_SENDWININICHANGE)) {
// 更新注册表,保存壁纸路径
HKEY hKey;
LPCSTR subKey = "Control Panel\\Desktop";
LPCSTR valueName = "Wallpaper";
if (RegOpenKeyExA(HKEY_CURRENT_USER, subKey, 0, KEY_SET_VALUE, &hKey) == ERROR_SUCCESS) {
if (RegSetValueExA(hKey, valueName, 0, REG_SZ, (const BYTE*)path, strlen(path)) != ERROR_SUCCESS) {
std::cerr << "注册表更新失败" << std::endl;
}
RegCloseKey(hKey);
}
return true;
}
else {
std::cerr << "Failed to set wallpaper." << std::endl;
return false;
}
}
int main() {
const char* wallpaperPath = "E:\\aa.jpg"; // 修改为壁纸图片的完整路径
if (SetWallpaper(wallpaperPath)) {
std::cout << "Wallpaper set successfully." << std::endl;
}
return 0;
}