views:

2570

answers:

3

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.

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.

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;
            }
        }
    }
}

Then in the main form we use the above code in these 3 events:

  • Activated
  • Deactivated
  • FormClosing

    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;
    }
    

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.

What do you guys think? Is this a good solution? What improvements can be made?

+3  A: 

I've noticed that if you use WebBrowser.Document.Write rather than WebBrowser.DocumentText then the click sound doesn't happen.

So instead of this:

webBrowser1.DocumentText = "<h1>Hello, world!</h1>";

try this:

webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");
Matt Hamilton
your suggested solution prevents the control from making that click noise but on the other side this approach causes the control to have serious focus issues. In my case, the webbrowser control steals back the focus at the end of the event loop. As far as I can tell there's no workaround for that.In my case I needed to get back to the DocumentText solution - but still I'm looking for disabling that annoying sound. Any other ideas?
Stefan Koell
Just found out, that James on this thread: http://stackoverflow.com/questions/393166/how-to-disable-click-sound-in-webbrowser-control has a great solution - at least for IE7 and IE8. I can use the DocumentText property, have no focus issues and best of all, I have no click sound.
Stefan Koell
A: 

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.

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?)

pix0r
A: 

@matt,

Document.Write works perfectly for what I was doing, I don't know why no body posted that on any of the forums i came across while googling.

sieben