問題中的'點擊聲音'實際上是系統範圍的首選項,所以我只希望在應用程序有焦點時禁用它,然後在應用程序關閉時重新啓用/失去重點。如何在您的應用中禁用WebBrowser'點擊聲音'
最初,我想在stackoverflow上問這個問題,但我還沒有在測試中。所以,在Google上搜索答案並找到一點信息之後,我想出了以下內容,現在決定在這裏發佈我正在測試的內容。
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個事件上面的代碼:
- 活化
- 停用
的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; }
釷我目前看到的一個問題是,如果程序崩潰,他們將不會有點擊聲音,直到他們重新啓動我的應用程序,但他們不知道這樣做。
你們認爲什麼?這是一個好的解決方案嗎?可以做什麼改進?
我對這一行有一個問題:isEnabled = value; 我剛剛評論過它,但我想知道它的目的是什麼 – Cristo 2013-03-31 20:57:20