<p>The 'click sound' in question is actually a system wide preference, so I only want it to be disabled when my application has focus and then re-enable when the application closes/loses focus.</p>

<p>Originally, I wanted to ask this question here on stackoverflow, but I was not yet in the beta. So, after googling for the answer and finding only a little bit of information on it I came up with the following and decided to post it here now that I'm in the beta.</p>

<pre><code>using System;
using Microsoft.Win32;

namespace HowTo
{
    class WebClickSound
    {
        /// &lt;summary&gt;
        /// Enables or disables the web browser navigating click sound.
        /// &lt;/summary&gt;
        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 &amp;&amp; keyValue != "\"\"";
            }
            set
            {
                string keyValue;

                if (value)
                {
                    keyValue = "%SystemRoot%\\Media\\";
                    if (Environment.OSVersion.Version.Major == 5 &amp;&amp; Environment.OSVersion.Version.Minor &gt; 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></pre>

<p>Then in the main form we use the above code in these 3 events:  </p>

<ul>
<li>Activated  </li>
<li>Deactivated  </li>
<li><p>FormClosing</p>

<pre><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></pre></li>
</ul>

<p>The one problem I see currently is if the program crashes they won't have the click sound until they re-launch my application, but they wouldn't know to do that.</p>

<p>What do you guys think? Is this a good solution? What improvements can be made?</p>

[{'answer_id': 10463, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 5, 'selected': True, 'text': '<p>I\'ve noticed that if you use WebBrowser.Document.Write rather than WebBrowser.DocumentText then the click sound doesn\'t happen.</p>\n\n<p>So instead of this:</p>\n\n<pre><code>webBrowser1.DocumentText = "&lt;h1&gt;Hello, world!&lt;/h1&gt;";\n</code></pre>\n\n<p>try this:</p>\n\n<pre><code>webBrowser1.Document.OpenNew(true);\nwebBrowser1.Document.Write("&lt;h1&gt;Hello, world!&lt;/h1&gt;");\n</code></pre>\n'}, {'answer_id': 10465, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 0, 'selected': False, 'text': '<p>Definitely feels like a hack, but having done some research on this a long time ago and not finding any other solutions, probably your best bet.</p>\n\n<p>Better yet would be designing your application so it doesn\'t require many annoying page reloads.. for example, if you\'re refreshing an iframe to check for updates on the server, use XMLHttpRequest instead.  (Can you tell that I was dealing with this problem back in the days before the term "AJAX" was coined?)</p>\n'}, {'answer_id': 4588327, 'author': 'DjCzermino', 'author_id': 561764, 'author_profile': 'https://Stackoverflow.com/users/561764', 'pm_score': 0, 'selected': False, 'text': '<p>If you want to use replacing Windows Registry, use this:</p>\n\n<pre><code>// backup value\nRegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current");\nstring BACKUP_keyValue = (string)key.GetValue(null);\n\n// write nothing\nkey = Registry.CurrentUser.OpenSubKey(@"AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current", true);\nkey.SetValue(null, "",  RegistryValueKind.ExpandString);\n\n// do navigation ...\n\n// write backup key\nRegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current", true);\nkey.SetValue(null, BACKUP_keyValue,  RegistryValueKind.ExpandString);\n</code></pre>\n'}, {'answer_id': 4655871, 'author': 'John black', 'author_id': 566868, 'author_profile': 'https://Stackoverflow.com/users/566868', 'pm_score': 5, 'selected': False, 'text': '<pre><code>const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21;\nconst int SET_FEATURE_ON_PROCESS = 0x00000002;\n\n[DllImport("urlmon.dll")]\n[PreserveSig]\n[return: MarshalAs(UnmanagedType.Error)]\nstatic extern int CoInternetSetFeatureEnabled(int FeatureEntry,\n                                              [MarshalAs(UnmanagedType.U4)] int dwFlags,\n                                              bool fEnable);\n\nstatic void DisableClickSounds()\n{\n    CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS,\n                                SET_FEATURE_ON_PROCESS,\n                                true);\n}\n</code></pre>\n'}, {'answer_id': 17822223, 'author': 'user2612427', 'author_id': 2612427, 'author_profile': 'https://Stackoverflow.com/users/2612427', 'pm_score': 1, 'selected': False, 'text': '<p>You disable it by changing Internet Explorer registry value of navigating sound to "NULL":</p>\n\n<pre><code>Registry.SetValue("HKEY_CURRENT_USER\\\\AppEvents\\\\Schemes\\\\Apps\\\\Explorer\\\\Navigating\\\\.Current","","NULL");\n</code></pre>\n\n<p>And enable it by changing Internet Explorer registry value of navigating sound to "C:\\Windows\\Media\\Cityscape\\Windows Navigation Start.wav":</p>\n\n<pre><code>Registry.SetValue("HKEY_CURRENT_USER\\\\AppEvents\\\\Schemes\\\\Apps\\\\Explorer\\\\Navigating\\\\.Current","","C:\\Windows\\Media\\Cityscape\\Windows Navigation Start.wav");\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/10456', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1147/']