有问题的"点击声音"实际上是系统范围的偏好,因此我只希望在我的应用程序具有焦点时禁用它,然后在应用程序关闭/失去焦点时重新启用它.
最初,我想在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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在主窗体中我们在这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;
}
Run Code Online (Sandbox Code Playgroud)我目前看到的一个问题是,如果程序崩溃,他们将无法获得点击声,直到他们重新启动我的应用程序,但他们不知道这样做.
你们有什么感想?这是一个好的解决方案吗?可以做些什么改进?
Joh*_*ack 39
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);
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*ton 12
我注意到如果您使用WebBrowser.Document.Write而不是WebBrowser.DocumentText,则不会发生咔嗒声.
所以不是这样的:
webBrowser1.DocumentText = "<h1>Hello, world!</h1>";
Run Code Online (Sandbox Code Playgroud)
试试这个:
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14476 次 |
最近记录: |