QuietMessageBox
During the development of Drug Lord I ran across an annyoing feature of the Windows API MessageBox function; a noise is made whenever the message box pops up. While this is fine for normal alert-type messages, it is distracting when used frequently, such as occurs in the game. I was surprised to learn that there wasn't a flag that could be passed to the function that would shut off the beeping.
So, I decided to develop my own quiet version of MessageBox, and because I'm nice, I'm sharing this knowledge with everyone else. It's written in C, but can easily be converted to other languages.
int QuietMessageBox(HWND hwnd, char *msg, char *caption, UINT type) {
int result;
char buf[MAX_PATH];
char *entry;
DWORD keytype;
int size;
HKEY hkey;
int code;
code = (type & 0xF0) >> 4;
if (code == 1)
entry = "SystemHand";
else if (code == 2)
entry = "SystemQuestion";
else if (code == 3)
entry = "SystemExclamation";
else if (code == 4)
entry = "SystemAsterisk";
else
entry = ".Default";
wsprintf(buf, "AppEvents\\Schemes\\Apps\\.Default\\%s\\.Current", entry);
if (RegOpenKeyEx(HKEY_CURRENT_USER, buf, 0, KEY_READ | KEY_WRITE,
&hkey) == ERROR_SUCCESS) {
RegQueryValueEx(hkey, NULL, NULL, &keytype, buf, &size);
RegSetValueEx(hkey, NULL, 0, keytype, "", 1);
result = MessageBox(hwnd, msg, caption, type);
RegSetValueEx(hkey, NULL, 0, keytype, buf, size);
RegCloseKey(hkey);
} else {
result = MessageBox(hwnd, msg, caption, type);
}
return result;
}
What it does is temporarily assigns the event sound to be nothing. It then quietly pops up the MessageBox. After the user has dismissed the box, it restores the previous settings so that other applications won't be affected. There are a couple of gotchas here, however. First, if the message box is being displayed and the user manually changes the sound setting via the control panel or whatnot, the old sound will be restored when the box is dismissed. Second, if another application's message box pops up, it will also be quiet. I don't see either of these being too serious; they are both highly unlikely to occur, particularily the first. As for the second, it is doubtful the user would even notice the presence or absence of sound.
I suppose it would be possible to hook into the MessageBox function and restore the sound settings the moment the box was displayed, to ensure neither of the gotchas would occur, but I will leave that for someone else to do.
|