繁体   English   中英

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

[英]HowTo Disable WebBrowser 'Click Sound' in your app only

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

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

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

然后在主窗体中我们在这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; } 

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

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

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

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

所以不是这样的:

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

尝试这个:

webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");

您通过将导航声音的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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM