C# 如何仅在您的应用程序中禁用 WebBrowser 'Click Sound'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HowTo Disable WebBrowser 'Click Sound' in your app only
提问by sieben
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.
本来,我想在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;
}
}
}
}
Then in the main form we use the above code in these 3 events:
然后在主窗体中我们在这 3 个事件中使用上面的代码:
- 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; }
- 活性
- 停用
关闭表单
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?
你们有什么感想?这是一个很好的解决方案吗?可以进行哪些改进?
采纳答案by Matt Hamilton
I've noticed that if you use WebBrowser.Document.Write rather than WebBrowser.DocumentText then the click sound doesn't happen.
我注意到,如果您使用 WebBrowser.Document.Write 而不是 WebBrowser.DocumentText 则不会发生点击声音。
So instead of this:
所以而不是这个:
webBrowser1.DocumentText = "<h1>Hello, world!</h1>";
try this:
尝试这个:
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");
回答by pix0r
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?)
更好的是设计您的应用程序,使其不需要许多烦人的页面重新加载。例如,如果您正在刷新 iframe 以检查服务器上的更新,请改用 XMLHttpRequest。(你能说在“AJAX”这个词被创造出来之前的几天里我正在处理这个问题吗?)
回答by DjCzermino
If you want to use replacing Windows Registry, use this:
如果要使用替换 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);
回答by John black
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);
}
回答by user2612427
You disable it by changing Internet Explorer registry value of navigating sound to "NULL":
您可以通过将导航声音的 Internet Explorer 注册表值更改为“NULL”来禁用它:
Registry.SetValue("HKEY_CURRENT_USER\AppEvents\Schemes\Apps\Explorer\Navigating\.Current","","NULL");
And enable it by changing Internet Explorer registry value of navigating sound to "C:\Windows\Media\Cityscape\Windows Navigation Start.wav":
并通过将导航声音的 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");