问题中的“单击声音”实际上是一个系统范围的首选项,所以我只希望在我的应用程序有焦点时禁用它,然后在应用程序关闭/失去焦点时重新启用。
最初,我想问这个问题在这里堆叠溢出,但我还没有在测试版。所以,在谷歌搜索了答案之后,我找到了一些关于答案的信息,我想出了下面的内容,并决定在我处于测试版的时候在这里发布它。
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;
}
}
}
}然后,在主要形式中,我们在以下三个事件中使用了上面的代码:
当程序有焦点时,Form1_Activated(对象发送方,EventArgs e) { //禁用声音;}当程序失去焦点时,私有WebClickSound.Enabled (对象发送方,EventArgs e) { //启用声音;}私有空洞Form1_FormClosing(对象发送方,FormClosingEventArgs e) { //启用应用程序上的声音退出WebClickSound.Enabled = true;}
我目前看到的一个问题是,如果程序崩溃,在重新启动我的应用程序之前,它们不会有点击声音,但是他们不知道要这么做。
你们觉得怎么样?这是个好办法吗?可以作出哪些改进?
发布于 2008-08-13 23:10:16
我注意到如果您使用的是WebBrowser.Document.Write而不是WebBrowser.DocumentText,那么单击声音就不会发生。
所以,不是这样的:
webBrowser1.DocumentText = "<h1>Hello, world!</h1>";试试这个:
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");发布于 2011-01-11 09:06:31
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);
}发布于 2013-07-23 22:38:50
您可以通过将导航声音的Internet注册表值更改为“NULL”来禁用它:
Registry.SetValue("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current","","NULL");并通过将导航声音的Internet注册表值更改为"C:\Windows\Media\Cityscape\Windows导航Start.wav“来启用它:
Registry.SetValue("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current","","C:\Windows\Media\Cityscape\Windows Navigation Start.wav");https://stackoverflow.com/questions/10456
复制相似问题