如何在应用程序中禁用WebBrowser“单击声音”

有问题的“咔嗒声”实际上是系统范围的偏好,因此我只希望在我的应用程序具有焦点时禁用它,然后在应用程序关闭/失去焦点时重新启用它。

最初,我想在stackoverflow上问这个问题,但我还没有进入测试阶段。 因此,在谷歌搜索答案并找到一点信息之后,我想出了以下内容并决定在此处发布,因为我处于测试阶段。

using System; using Microsoft.Win32; namespace HowTo { class WebClickSound { ///  /// Enables or disables the web browser navigating click sound. ///  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; } } } } 

然后在主窗体中我们在这3个事件中使用上面的代码:

  • 活性
  • 停用
  • 的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; } 

我目前看到的一个问题是,如果程序崩溃,他们将无法获得点击声,直到他们重新启动我的应用程序,但他们不知道这样做。

你们有什么感想? 这是一个好的解决方案吗? 可以做出哪些改进?

我注意到,如果您使用WebBrowser.Document.Write而不是WebBrowser.DocumentText,则不会发生咔嗒声。

所以不是这样的:

 webBrowser1.DocumentText = "

Hello, world!

";

试试这个:

 webBrowser1.Document.OpenNew(true); webBrowser1.Document.Write("

Hello, world!

");
 const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21; const int SET_FEATURE_ON_PROCESS = 0x00000002; [DllImport("urlmon.dll")] [PreserveSig] [return: MarshalAs(UnmanagedType.Error)] static extern int CoInternetSetFeatureEnabled(int FeatureEntry, [MarshalAs(UnmanagedType.U4)] int dwFlags, bool fEnable); static void DisableClickSounds() { CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_PROCESS, true); } 

您通过将导航声音的Internet Explorer注册表值更改为“NULL”来禁用它:

 Registry.SetValue("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current","","NULL"); 

并通过将导航声音的Internet Explorer注册表值更改为“C:\ Windows \ Media \ Cityscape \ Windows Navigation Start.wav”来启用它:

 Registry.SetValue("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current","","C:\Windows\Media\Cityscape\Windows Navigation Start.wav"); 

绝对感觉像是一个黑客,但很久以前就已经做过一些研究而没有找到任何其他解决方案,可能是你最好的选择。

更好的是设计你的应用程序,因此它不需要很多烦人的页面重新加载..例如,如果你要刷新iframe来检查服务器上的更新,请改用XMLHttpRequest。 (在“AJAX”这个词被创造出来之前的几天,你能说出我正在处理这个问题吗?)

如果要使用替换Windows注册表,请使用以下命令:

 // backup value RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current"); string BACKUP_keyValue = (string)key.GetValue(null); // write nothing key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current", true); key.SetValue(null, "", RegistryValueKind.ExpandString); // do navigation ... // write backup key RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current", true); key.SetValue(null, BACKUP_keyValue, RegistryValueKind.ExpandString);