Skip to content

Commit a36c61c

Browse files
committed
Added SysColor
1 parent 8b2020e commit a36c61c

File tree

3 files changed

+316
-0
lines changed

3 files changed

+316
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ Edit shortcut properties from the command line.
125125
## [Sleep](Sleep.cpp)
126126
Sleep in seconds.
127127

128+
## [SysColors](SysColors.cpp)
129+
Edit system colors. See [GetSysColor](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsyscolor).
130+
128131
## [TimeStamp](TimeStamp.cpp)
129132
Copies stdin to stdout prepending each line with a timestamp.
130133

SysColors.cpp

+303
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
#define WIN32_LEAN_AND_MEAN
2+
#include <Windows.h>
3+
#include <tchar.h>
4+
5+
#define and &&
6+
#define or ||
7+
8+
#include <cstdlib>
9+
#include <cstdio>
10+
#include <fstream>
11+
#include <string>
12+
#include <sstream>
13+
14+
#ifdef UNICODE
15+
#define tstring wstring
16+
#define tifstream wifstream
17+
#define tistringstream wistringstream
18+
#else
19+
#define tstring string
20+
#define tifstream ifstream
21+
#define tistringstream istringstream
22+
#endif
23+
24+
#define STRINGIZE2(s) TEXT(#s)
25+
#define STRINGIZE(s) STRINGIZE2(s)
26+
27+
// https://www.temblast.com/dbplot/color5.htm
28+
29+
const TCHAR* ColorName(int i)
30+
{
31+
switch (i)
32+
{
33+
#define x(v) case v: return TEXT(#v)
34+
x(COLOR_SCROLLBAR);
35+
x(COLOR_BACKGROUND);
36+
x(COLOR_ACTIVECAPTION);
37+
x(COLOR_INACTIVECAPTION);
38+
x(COLOR_MENU);
39+
x(COLOR_WINDOW);
40+
x(COLOR_WINDOWFRAME);
41+
x(COLOR_MENUTEXT);
42+
x(COLOR_WINDOWTEXT);
43+
x(COLOR_CAPTIONTEXT);
44+
x(COLOR_ACTIVEBORDER);
45+
x(COLOR_INACTIVEBORDER);
46+
x(COLOR_APPWORKSPACE);
47+
x(COLOR_HIGHLIGHT);
48+
x(COLOR_HIGHLIGHTTEXT);
49+
x(COLOR_BTNFACE);
50+
x(COLOR_BTNSHADOW);
51+
x(COLOR_GRAYTEXT);
52+
x(COLOR_BTNTEXT);
53+
x(COLOR_INACTIVECAPTIONTEXT);
54+
x(COLOR_BTNHIGHLIGHT);
55+
56+
#if(WINVER >= 0x0400)
57+
x(COLOR_3DDKSHADOW);
58+
x(COLOR_3DLIGHT);
59+
x(COLOR_INFOTEXT);
60+
x(COLOR_INFOBK);
61+
#endif /* WINVER >= 0x0400 */
62+
63+
#if(WINVER >= 0x0500)
64+
x(COLOR_HOTLIGHT);
65+
x(COLOR_GRADIENTACTIVECAPTION);
66+
x(COLOR_GRADIENTINACTIVECAPTION);
67+
#if(WINVER >= 0x0501)
68+
x(COLOR_MENUHILIGHT);
69+
x(COLOR_MENUBAR);
70+
#endif /* WINVER >= 0x0501 */
71+
#endif /* WINVER >= 0x0500 */
72+
#undef x
73+
case 25: return TEXT("COLOR_UNUSED");
74+
default: return TEXT("Unknown");
75+
}
76+
}
77+
78+
const TCHAR* RegColorName(int i)
79+
{
80+
switch (i)
81+
{
82+
#define x(v, s) case v: return TEXT(s)
83+
x(COLOR_SCROLLBAR, "Scrollbar");
84+
x(COLOR_BACKGROUND , "Background");
85+
x(COLOR_ACTIVECAPTION, "ActiveTitle");
86+
x(COLOR_INACTIVECAPTION , "InactiveTitle");
87+
x(COLOR_MENU, "Menu");
88+
x(COLOR_WINDOW, "Window");
89+
x(COLOR_WINDOWFRAME, "WindowFrame");
90+
x(COLOR_MENUTEXT, "MenuText");
91+
x(COLOR_WINDOWTEXT, "WindowText");
92+
x(COLOR_CAPTIONTEXT, "TitleText");
93+
x(COLOR_ACTIVEBORDER, "ActiveBorder");
94+
x(COLOR_INACTIVEBORDER, "InactiveBorder");
95+
x(COLOR_APPWORKSPACE, "AppWorkspace");
96+
x(COLOR_HIGHLIGHT, "Hilight");
97+
x(COLOR_HIGHLIGHTTEXT, "HilightText");
98+
x(COLOR_BTNFACE, "ButtonFace");
99+
x(COLOR_BTNSHADOW, "ButtonShadow");
100+
x(COLOR_GRAYTEXT, "GrayText");
101+
x(COLOR_BTNTEXT, "ButtonText");
102+
x(COLOR_INACTIVECAPTIONTEXT, "InactiveTitleText");
103+
x(COLOR_BTNHIGHLIGHT, "ButtonHilight");
104+
105+
#if(WINVER >= 0x0400)
106+
x(COLOR_3DDKSHADOW, "ButtonDkShadow");
107+
x(COLOR_3DLIGHT, "ButtonLight");
108+
x(COLOR_INFOTEXT, "InfoText");
109+
x(COLOR_INFOBK, "InfoWindow");
110+
#endif /* WINVER >= 0x0400 */
111+
x(25, "ButtonAlternateFace");
112+
113+
#if(WINVER >= 0x0500)
114+
x(COLOR_HOTLIGHT, "HotTrackingColor");
115+
x(COLOR_GRADIENTACTIVECAPTION, "GradientActiveTitle");
116+
x(COLOR_GRADIENTINACTIVECAPTION, "GradientInactiveTitle");
117+
#if(WINVER >= 0x0501)
118+
x(COLOR_MENUHILIGHT, "MenuHilight");
119+
x(COLOR_MENUBAR, "MenuBar");
120+
#endif /* WINVER >= 0x0501 */
121+
#endif /* WINVER >= 0x0500 */
122+
#undef x
123+
default: return TEXT("Unknown");
124+
}
125+
}
126+
127+
void InitSysColours(_In_ int cElements,
128+
_Out_writes_(cElements) INT* lpaElements)
129+
{
130+
for (int i = 0; i < cElements; ++i)
131+
lpaElements[i] = i;
132+
}
133+
134+
void LoadSysColors(
135+
_In_ int cElements,
136+
_In_reads_(cElements) CONST INT* lpaElements,
137+
_Out_writes_(cElements) COLORREF* lpaRgbValues)
138+
{
139+
for (int i = 0; i < cElements; ++i)
140+
lpaRgbValues[i] = GetSysColor(lpaElements[i]);
141+
}
142+
143+
void PrintSysColors(
144+
_In_ int cElements,
145+
_In_reads_(cElements) CONST INT* lpaElements,
146+
_In_reads_(cElements) CONST COLORREF* lpaRgbValues)
147+
{
148+
for (int i = 0; i < cElements; ++i)
149+
_tprintf(_T("%s=%u %u %u\n"), ColorName(lpaElements[i]), GetRValue(lpaRgbValues[i]), GetGValue(lpaRgbValues[i]), GetBValue(lpaRgbValues[i]));
150+
}
151+
152+
COLORREF ParseColor(LPCTSTR sColor)
153+
{
154+
std::tistringstream sstre(sColor);
155+
int r, g, b;
156+
sstre >> r;
157+
sstre >> g;
158+
sstre >> b;
159+
if (r >= 0 and r <= 255 and g >= 0 and g <= 255 and b >= 0 and b <= 255)
160+
return RGB(r, g, b);
161+
else
162+
{
163+
_ftprintf(stderr, _T("Invalid color: %s\n"), sColor);
164+
return 0;
165+
}
166+
}
167+
168+
void LoadRegSysColors(
169+
_In_ int cElements,
170+
_In_reads_(cElements) CONST INT* lpaElements,
171+
_Out_writes_(cElements) COLORREF* lpaRgbValues)
172+
{
173+
_ftprintf(stderr, _T("TODO LoadRegSysColors\n"));
174+
HKEY hKey = NULL;
175+
RegOpenKey(HKEY_CURRENT_USER, _T("Control Panel\\Colors"), &hKey);
176+
if (hKey == NULL)
177+
{
178+
_ftprintf(stderr, _T("Error opening key\n"));
179+
return;
180+
}
181+
182+
DWORD dwIndex = 0;
183+
while (true)
184+
{
185+
TCHAR valueName[100];
186+
DWORD dataType;
187+
TCHAR data[100];
188+
189+
DWORD valNameLen = ARRAYSIZE(valueName);
190+
DWORD dataSize = ARRAYSIZE(data) * sizeof(data[0]);
191+
192+
if (RegEnumValue(hKey,
193+
dwIndex,
194+
valueName,
195+
&valNameLen,
196+
NULL,
197+
&dataType,
198+
(BYTE*) &data,
199+
&dataSize) == ERROR_NO_MORE_ITEMS)
200+
break;
201+
202+
int i;
203+
for (i = 0; i < cElements; ++i)
204+
if (_tcsicmp(valueName, RegColorName(lpaElements[i])) == 0)
205+
{
206+
lpaRgbValues[i] = ParseColor(data);
207+
break;
208+
}
209+
if (i >= cElements)
210+
_ftprintf(stderr, _T("Unknown value: %s\n"), valueName);
211+
212+
dwIndex++;
213+
}
214+
215+
RegCloseKey(hKey);
216+
}
217+
218+
void SaveRegSysColors(
219+
_In_ int cElements,
220+
_In_reads_(cElements) CONST INT* lpaElements,
221+
_In_reads_(cElements) CONST COLORREF* lpaRgbValues)
222+
{
223+
_ftprintf(stderr, _T("TODO SaveRegSysColors\n"));
224+
}
225+
226+
void LoadFileSysColors(
227+
_In_ LPCTSTR sFileName,
228+
_In_ int cElements,
229+
_In_reads_(cElements) CONST INT* lpaElements,
230+
_Out_writes_(cElements) COLORREF* lpaRgbValues)
231+
{
232+
std::tifstream f(sFileName);
233+
std::tstring line;
234+
while (std::getline(f, line))
235+
{
236+
if (line.empty() or line[0] == _T('#'))
237+
continue;
238+
239+
const size_t e = line.find('=');
240+
if (e == std::string::npos)
241+
{
242+
_ftprintf(stderr, _T("Unknown line: %s\n"), line.c_str());
243+
continue;
244+
}
245+
246+
const std::tstring name = line.substr(0, e);
247+
int i;
248+
for (i = 0; i < cElements; ++i)
249+
if (name == ColorName(lpaElements[i]))
250+
{
251+
const std::tstring color = line.substr(e + 1);
252+
lpaRgbValues[i] = ParseColor(color.c_str());
253+
break;
254+
}
255+
if (i >= cElements)
256+
_ftprintf(stderr, _T("Unknown line: %s\n"), line.c_str());
257+
}
258+
}
259+
260+
#define COLOR_MAX (COLOR_MENUBAR + 1)
261+
262+
int _tmain(const int argc, const TCHAR* const argv[])
263+
{
264+
_tprintf(_T("# Sys Colours\n"));
265+
bool bLoadReg = false;
266+
bool bSaveReg = false;
267+
LPCTSTR sFileName = nullptr;
268+
269+
for (int argi = 1; argi < argc; ++argi)
270+
{
271+
LPCTSTR arg = argv[argi];
272+
if (_tcsicmp(arg, _T("/loadreg")) == 0)
273+
bLoadReg = true;
274+
else if (_tcsicmp(arg, _T("/savereg")) == 0)
275+
bSaveReg = true;
276+
else if (sFileName == nullptr)
277+
sFileName = arg;
278+
else
279+
_ftprintf(stderr, _T("Unknown argument: %s\n"), arg);
280+
}
281+
282+
INT index[COLOR_MAX] = {};
283+
COLORREF colours[COLOR_MAX] = {};
284+
285+
InitSysColours(COLOR_MAX, index);
286+
287+
LoadSysColors(COLOR_MAX, index, colours);
288+
289+
if (bLoadReg)
290+
LoadRegSysColors(COLOR_MAX, index, colours);
291+
292+
if (sFileName)
293+
LoadFileSysColors(sFileName, COLOR_MAX, index, colours);
294+
295+
PrintSysColors(COLOR_MAX, index, colours);
296+
297+
SetSysColors(COLOR_MAX, index, colours);
298+
299+
if (bSaveReg)
300+
SaveRegSysColors(COLOR_MAX, index, colours);
301+
302+
return EXIT_SUCCESS;
303+
}

SysColors.props

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<ItemDefinitionGroup>
3+
<Link>
4+
<AdditionalDependencies>user32.lib;Advapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
5+
</Link>
6+
</ItemDefinitionGroup>
7+
<PropertyGroup>
8+
<CharacterSet>Unicode</CharacterSet>
9+
</PropertyGroup>
10+
</Project>

0 commit comments

Comments
 (0)