|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWindows plays sounds if you click somewhere, or if an error occurs. If you logoff, Windows plays a logoff sound. Did you ever want to use something similar in your application, e.g. if you'd like to play a certain sound if your application does something special? The .NET framework support no sounds at this time, so we have to use the API method [DllImport("winmm.dll", EntryPoint="PlaySound",CharSet=CharSet.Auto)]
private static extern int PlaySound(String pszSound, int hmod, int falgs);
First problem fixed... Sound eventsThe sound events are stored in the registry, this is done by user. So the settings can be found under Every sound event is based on :
With this knowledge, we can rebuild the control panel view of the system sounds. We're also able to add our own sound events to registry. All we have to do is create some registry keys at the right position. How to play sound eventsThe public enum SND
{
SND_SYNC = 0x0000 ,/* play synchronously (default) */
SND_ASYNC = 0x0001 , /* play asynchronously */
SND_NODEFAULT = 0x0002 , /* silence (!default) if sound not found */
SND_MEMORY = 0x0004 , /* pszSound points to a memory file */
SND_LOOP = 0x0008 , /* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010 , /* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS = 0x00010000 ,/* name is a registry alias */
SND_ALIAS_ID = 0x00110000, /* alias is a pre d ID */
SND_FILENAME = 0x00020000, /* name is file name */
SND_RESOURCE = 0x00040004, /* name is resource name or atom */
SND_PURGE = 0x0040, /* purge non-static events for task */
SND_APPLICATION = 0x0080 /* look for application specific association */
}
Playing a wave file located on the disk looks like this, public static void PlaySound(String pszSound)
{
if(File.Exists(pszSound))
{
PlaySound(pszSound,0,
(int) (SND.SND_ASYNC | SND.SND_FILENAME | SND.SND_NOWAIT));
}
}
Otherwise, if we play a sound event, it looks like this, public static void PlaySoundEvent(String pszSound)
{
PlaySound(pszSound,0,
(int) (SND.SND_ASYNC | SND.SND_ALIAS | SND.SND_NOWAIT));
}
How to display sound eventsThe next step is to fill a First we need to open the RegistryKey rkey = Registry.CurrentUser;
RegistryKey rkey1=rkey.OpenSubKey("AppEvents\Schemes\Apps");
string[] sections = rkey1.GetSubKeyNames();
Now we've got the names of the application sections, next we read the display string for each section stored in the default value of the key. // open reg key
for(int i=0;i<sections.Length;i++)
{
// read section name
rkey1=rkey.OpenSubKey(
@"AppEvents\Schemes\Apps\" + sections[i]);
// read section name
try
{
string sectionName = rkey1.GetValue("").ToString();
}
catch {}
}
Now we read the subkeys of the section key, these are the sound event verbs. // read subkeys
string[] subsections = rkey1.GetSubKeyNames();
for(int j=0;j<subsections.Length;j++)
{
rkey1=rkey.OpenSubKey(
@"AppEvents\EventLabels\" + subsections[j]);
try
{
string eventName = rkey1.GetValue("").ToString();
}
catch {}
}
Finish our work and close all resources allocated. rkey1.Close();
rkey.Close();
If you like, you could fill this information into a Have phun with it.
|
||||||||||||||||||||||