Como desativar o WebBrowser 'Click Sound' somente no seu aplicativo

O "som de clique" em questão é, na verdade, uma preferência de todo o sistema, portanto, só quero que ele seja desativado quando meu aplicativo tiver foco e, em seguida, reativado quando o aplicativo fechar / perder o foco.

Originalmente, eu queria fazer essa pergunta aqui no stackoverflow, mas eu ainda não estava na versão beta. Então, depois de googling para a resposta e encontrar apenas um pouco de informação sobre isso eu vim com o seguinte e decidi postar aqui agora que estou na versão beta.

<code>using System;
using Microsoft.Win32;

namespace HowTo
{
    class WebClickSound
    {
        /// <summary>
        /// Enables or disables the web browser navigating click sound.
        /// </summary>
        public static bool Enabled
        {
            get
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current");
                string keyValue = (string)key.GetValue(null);
                return String.IsNullOrEmpty(keyValue) == false && keyValue != "\"\"";
            }
            set
            {
                string keyValue;

                if (value)
                {
                    keyValue = "%SystemRoot%\\Media\\";
                    if (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor > 0)
                    {
                        // XP
                        keyValue += "Windows XP Start.wav";
                    }
                    else if (Environment.OSVersion.Version.Major == 6)
                    {
                        // Vista
                        keyValue += "Windows Navigation Start.wav";
                    }
                    else
                    {
                        // Don't know the file name so I won't be able to re-enable it
                        return;
                    }
                }
                else
                {
                    keyValue = "\"\"";
                }

                // Open and set the key that points to the file
                RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current", true);
                key.SetValue(null, keyValue,  RegistryValueKind.ExpandString);
                isEnabled = value;
            }
        }
    }
}
</code>

Então, no formulário principal, usamos o código acima nesses três eventos:

ativadoDesativado

FormClosing

<code>private void Form1_Activated(object sender, EventArgs e)
{
    // Disable the sound when the program has focus
    WebClickSound.Enabled = false;
}

private void Form1_Deactivate(object sender, EventArgs e)
{
    // Enable the sound when the program is out of focus
    WebClickSound.Enabled = true;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // Enable the sound on app exit
    WebClickSound.Enabled = true;
}
</code>

O único problema que vejo atualmente é que, se o programa travar, ele não terá o som de clique até que ele reinicie o aplicativo, mas eles não saberiam fazer isso.

O que é que vocês acham? Esta é uma boa solução? Quais melhorias podem ser feitas?

questionAnswers(5)

yourAnswerToTheQuestion