如何仅在应用程序中禁用WebBrowser"单击声音"
时间:2020-03-05 18:39:30 来源:igfitidea点击:
有问题的"点击声音"实际上是系统范围的首选项,因此我只希望在应用程序具有焦点时禁用它,然后在应用程序关闭/失去焦点时重新启用它。
本来我想在stackoverflow上问这个问题,但是我还没有进入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; } } } }
然后在主要形式中,在以下3个事件中使用以上代码:
- 活性
- 已停用
- 表单关闭
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 = "<h1>Hello, world!</h1>";
试试这个:
webBrowser1.Document.OpenNew(true); webBrowser1.Document.Write("<h1>Hello, world!</h1>");
回答
绝对感觉像是黑客,但是很久以前对此做了一些研究,没有找到任何其他解决方案,这也许是最好的选择。
最好是设计应用程序,这样就不需要进行很多烦人的页面重装。例如,如果要刷新iframe来检查服务器上的更新,请改用XMLHttpRequest。 (我们能告诉我在创造" AJAX"一词之前的日子里我正在处理此问题吗?)