#include <windows.h> #include <iostream> typedef struct PersonalizationColor { int r; int g; int b; } PersonalizationColor; static const unsigned char ColorSet_Version3[25][2][3] = { // Table for Accent & Background color in Windows 8 {{37,37,37},{244,179,0}}, {{37,37,37},{120,186,0}}, {{37,37,37},{38,115,236}}, {{37,37,37},{174,17,61}}, {{46,23,0},{99,47,0}}, {{78,0,0},{176,30,0}}, {{78,0,56},{193,0,79}}, {{45,0,78},{114,0,172}}, {{31,0,104},{70,23,180}}, {{0,30,78},{0,106,193}}, {{0,77,96},{0,130,135}}, {{0,74,0},{25,153,0}}, {{21,153,42},{0,193,63}}, {{229,108,25},{255,152,29}}, {{184,27,27},{255,46,18}}, {{184,27,108},{255,29,119}}, {{105,27,184},{170,64,255}}, {{27,88,184},{31,174,255}}, {{86,156,227},{86,197,255}}, {{0,170,170},{0,216,204}}, {{131,186,31},{145,209,0}}, {{211,157,9},{225,183,0}}, {{224,100,183},{255,118,188}}, {{105,105,105},{0,164,164}}, {{105,105,105},{255,125,35}} }; typedef struct windows_version { int major; // CurrentVersion int minor; // CurrentVersion int build_number; // CurrentBuildNumber char name[256]; // ProductName char service_pack[256]; // CSDVersion(if exist) char edition[128]; // EditionID(if exist) } windows_version; int get_winver(windows_version* ptr) { HKEY temp; unsigned char CurrentVersion[8]; long long long_temp=8; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows NT\\CurrentVersion",0,KEY_READ,&temp)==ERROR_SUCCESS) { int a=0; if(RegQueryValueEx(temp,"CurrentVersion",0,nullptr,CurrentVersion,(LPDWORD)&long_temp)!=ERROR_SUCCESS) return 0; double current_version=strtod((char*)CurrentVersion,nullptr); ptr->major=(int)floor(current_version); ptr->minor=(int)floor(current_version*10.0)-ptr->major*10; long_temp=8; memset(CurrentVersion,0,8); if((a=RegQueryValueEx(temp,"CurrentBuildNumber",0,nullptr,CurrentVersion,(LPDWORD)&long_temp))!=ERROR_SUCCESS) return 0; ptr->build_number=atoi((char*)CurrentVersion); long_temp=256; if(RegQueryValueEx(temp,"ProductName",0,nullptr,(LPBYTE)ptr->name,(LPDWORD)&long_temp)!=ERROR_SUCCESS) return 0; int* from_lt=(int*)&long_temp+4; long_temp=256; *from_lt=RegQueryValueEx(temp,"CSDVersion",0,nullptr,(LPBYTE)ptr->service_pack,(LPDWORD)&long_temp); if(*from_lt==ERROR_FILE_NOT_FOUND) memset(ptr->service_pack,0,256); else if(*from_lt!=ERROR_SUCCESS) return 0; long_temp=128; *from_lt=RegQueryValueEx(temp,"EditionID",0,nullptr,(LPBYTE)ptr->edition,(LPDWORD)&long_temp); if(*from_lt==ERROR_FILE_NOT_FOUND) memset(ptr->edition,0,128); else if(*from_lt!=ERROR_SUCCESS) return 0; } else return 0; RegCloseKey(temp); return 1; } bool Win8GetAccentColor(PersonalizationColor* col) { windows_version win; get_winver(&win); if(win.major>=6 && (win.minor>=2 && win.minor<=3)) { HKEY temp; int r=0; if((r=RegOpenKeyExA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Accent",0,KEY_READ,&temp))==ERROR_SUCCESS) { auto accent_color=0UL; auto buf=4UL; if(win.minor==2) { if((r=RegQueryValueExA(temp,"ColorSet_Version3",nullptr,nullptr,(LPBYTE)&accent_color,&buf))==ERROR_SUCCESS) { col->r=ColorSet_Version3[accent_color][1][0]; col->g=ColorSet_Version3[accent_color][1][1]; col->b=ColorSet_Version3[accent_color][1][2]; RegCloseKey(temp); return true; } else { RegCloseKey(temp); return false; } } else { if((r=RegQueryValueExA(temp,"AccentColor",nullptr,nullptr,(LPBYTE)&accent_color,&buf))==ERROR_SUCCESS) { col->r=GetRValue(accent_color); col->g=GetGValue(accent_color); col->b=GetBValue(accent_color); RegCloseKey(temp); return true; } else { RegCloseKey(temp); return false; } } } else return false; } else return false; } bool Win8GetBackgroundColor(PersonalizationColor* col) { windows_version win; get_winver(&win); if(win.major>=6 && (win.minor>=2 && win.minor<=3)) { HKEY temp; int r=0; if((r=RegOpenKeyExA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Accent",0,KEY_READ,&temp))==ERROR_SUCCESS) { auto accent_color=0UL; auto buf=4UL; if(win.minor==2) { if((r=RegQueryValueExA(temp,"ColorSet_Version3",nullptr,nullptr,(LPBYTE)&accent_color,&buf))==ERROR_SUCCESS) { col->r=ColorSet_Version3[accent_color][0][0]; col->g=ColorSet_Version3[accent_color][0][1]; col->b=ColorSet_Version3[accent_color][0][2]; RegCloseKey(temp); return 3; } else { RegCloseKey(temp); return true; } } else { if((r=RegQueryValueExA(temp,"StartColor",nullptr,nullptr,(LPBYTE)&accent_color,&buf))==ERROR_SUCCESS) { col->r=GetRValue(accent_color); col->g=GetGValue(accent_color); col->b=GetBValue(accent_color); RegCloseKey(temp); return true; } else { RegCloseKey(temp); return false; } } } else { return false; } } else return false; } int main() { PersonalizationColor accent; PersonalizationColor background; if(!Win8GetAccentColor(&accent)) { std::cerr << "Cannot get accent color!" << endl; return 1; } if(!Win8GetBackgroundColor(&background)) { std::cerr << "Cannot get background color!" << endl; return 1; } std::cout << "Accent: " << accent.r << "," << accent.g << "," << accent.g << endl << "Background: " << background.r << "," << background.g << "," << background.b << endl; return 0; }
Note1: It also work in Windows 8.1 without any edits
Note2: It can be ported to C with some editing.
Note3: I give a bonus code. You can use get_winver to get windows version(which i think is better than GetVersionEx)
No comments:
Post a Comment