C++实现修改windows窗体背景颜色小工具。
主要思路是:
示例如下:
程序入口点代码
```c++
// 01_backgroundcolorregchangeTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <tchar.h>
using namespace std;
TCHAR g_tszRegCareValue[2][16] = {_T("255 255 255"), _T("199 237 204")}; //注册表默认值 保护色值
DWORD g_szdwColors[2] = {RGB(255, 255, 255), RGB(199, 237, 204)}; //颜色 白色和 豆沙绿
int g_iElements[1] = {COLOR_WINDOW}; //修改颜色的类型 这里是窗口
void CreateReg(HKEY Root,LPCTSTR szSubKey,LPCTSTR valueName,LPCTSTR RegValueBuffer) //修改注册表键值
{
HKEY key;
//long Ret=RegCreateKeyEx(Root, (LPCTSTR)szSubKey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&key,NULL);
//cout<<Ret<<endl;
//打开注册表某一路径
long Ret = RegOpenKeyEx(Root, szSubKey, 0, KEY_ALL_ACCESS, &key);
cout<<Ret<<endl;
//使用Cstring和char[]会乱码,这里写入键值必须使用LPCTSTR类型
//修改打开注册表路径下某个键值
Ret=RegSetValueEx(key, valueName, 0, REG_SZ, (CONST BYTE)RegValueBuffer, (lstrlen(RegValueBuffer) + 1)sizeof(TCHAR));
cout<<Ret<<endl;
if(Ret!=ERROR_SUCCESS)
{
cout<<"error!"<<endl;
}
RegCloseKey(key);
}
//刷新窗口 当即生效
void UpdateWindowColor(const DWORD *pdwInColor)
{
SetSysColors(1, &g_iElements[0], pdwInColor);
}
//设置windows窗口背景色
void SetWindowBackgroundColor()
{
//设置windows窗口背景色对应注册表的路径
LPCTSTR baseKey = _T("Control Panel\Colors");
LPCTSTR subKey = _T("Window");
//判断是否修改背景颜色
DWORD dwCurrentColors[1];
dwCurrentColors[0] = GetSysColor(g_iElements[0]);
if (0x00ffffff == dwCurrentColors[0])
{
CreateReg(HKEY_CURRENT_USER, baseKey, subKey, g_tszRegCareValue[1]);
//刷新窗口立即生效
UpdateWindowColor(&g_szdwColors[1]);
} else {
CreateReg(HKEY_CURRENT_USER, baseKey, subKey, g_tszRegCareValue[0]);
//刷新窗口立即生效
UpdateWindowColor(&g_szdwColors[0]);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
SetWindowBackgroundColor();
return 0;
}
注意立即刷新windows窗体,经过查找使用SetSysColors函数
```c++
//刷新窗口 当即生效
void UpdateWindowColor(const DWORD *pdwInColor)
{
SetSysColors(1, &g_iElements[0], pdwInColor);
}
修改windows注册表函数
```c++
//修改windows注册表函数
void CreateReg(HKEY Root,LPCTSTR szSubKey,LPCTSTR valueName,LPCTSTR RegValueBuffer) //修改注册表键值
{
HKEY key;
//long Ret=RegCreateKeyEx(Root, (LPCTSTR)szSubKey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&key,NULL);
//cout<<Ret<<endl;
//打开注册表某一路径
long Ret = RegOpenKeyEx(Root, szSubKey, 0, KEY_ALL_ACCESS, &key);
cout<<Ret<<endl;
//使用Cstring和char[]会乱码,这里写入键值必须使用LPCTSTR类型
//修改打开注册表路径下某个键值
Ret=RegSetValueEx(key, valueName, 0, REG_SZ, (CONST BYTE)RegValueBuffer, (lstrlen(RegValueBuffer) + 1)sizeof(TCHAR));
cout<<Ret<<endl;
if(Ret!=ERROR_SUCCESS)
{
cout<<"error!"<<endl;
}
RegCloseKey(key);
}
参考意见和网页地址:
原文:https://blog.51cto.com/zhaoanan/2537381