在嘀嗒声的问题实际上是一个系统范围preference,所以我只希望在我的应用程序具有焦点,然后重新启用应用程序时关闭/失去焦点,它被禁止
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.
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(@"AppEventsSchemesAppsExplorerNavigating.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(@"AppEventsSchemesAppsExplorerNavigating.Current", true);
key.SetValue(null, keyValue, RegistryValueKind.ExpandString);
isEnabled = value;
}
}
}
}
然后在主窗体,我们用上面的code在这3个事件:
Then in the main form we use the above code in these 3 events:
激活 停用的FormClosing 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;
}
在一个问题,目前我看到的是,如果程序崩溃,他们不会有咔嗒声,直到他们重新启动我的应用程序,但他们不知道这样做。
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?
我已经注意到,如果你使用WebBrowser.Document.Write而不是WebBrowser.DocumentText然后点击声音不会发生。
I've noticed that if you use WebBrowser.Document.Write rather than WebBrowser.DocumentText then the click sound doesn't happen.
所以:
webBrowser1.DocumentText = "<h1>Hello, world!</h1>";
试试这个:
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");