题
所讨论的“点击声音”实际上是系统范围内的首选项,因此我只希望在我的应用程序获得焦点时禁用它,然后在应用程序关闭/失去焦点时重新启用它。
本来,我想在 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个事件中使用上面的代码:
- 活性
- 已停用
表格关闭
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>");
其他提示
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);
不隶属于 StackOverflow