SRL Archive - Senior Scripts and Snippets

CBAS Style Changer


Smartzkid

2008-07-04 17:24:09

This script writes default 'Look and Feel' settings for swing (java gui). By default, it changes to the XP 'theme', but it isn't hard to change it to other styles. If you cant figure it out, feel free to hit me up on MSN.

Credits to Benland for findLibrary(), which I tore up a little bit to suit my needs. Many thanks.

Note that it's probably got a few spare bits left over in findLibrary(), but other than that, it should work pretty well.

And yes, this is definitely a Rube Goldberg; it's probably faster to set the preferences by hand than to compile & link this script, but it was nice practice for me, and hopefully one of you will learn something from it, too.

#include <iostream>
#include <fstream>
#include <windows.h>
#include "Main.h"

int writeFile(char* filename, char* data, std::_Ios_Openmode mode){
std::ofstream file;
file.open (filename, mode);
file << data;
file.close();
return 0;
}

char* findLibrary() {
HKEY jrekey;
HKEY curjrekey;
char value[512];
unsigned long size;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\JavaSoft\\Java Runtime Environment", 0, KEY_QUERY_VALUE, &jrekey);
size = 512;
memset(value, ' ', 512);
RegQueryValueEx(jrekey, "CurrentVersion", 0, 0, (unsigned char*) value, &size);
RegOpenKeyEx(jrekey, value, 0, KEY_QUERY_VALUE, &curjrekey);
size = 512;
RegQueryValueEx(curjrekey, "JavaHome", 0, 0, (unsigned char*) value, &size);
return value;
}

int scriptmain () {
char buff[512];
memset(buff, 0, 512);
strcat(buff, findLibrary());
strcat(buff, "\\lib\\swing.properties");
writeFile(buff, "swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel", std::ios::out | std::ios::binary);
jout << "Preferences written to: " << buff << "\n";
return 0;
}