El 'clic de sonido' en cuestión es en realidad una preferencia de todo el sistema, por lo que solo quiero que se deshabilite cuando mi aplicación tenga foco y luego volver a habilitar cuando la aplicación se cierre/pierde focoCómo deshabilitar WebBrowser 'Click Sound' en su aplicación solamente
Originalmente, quería hacer esta pregunta aquí en stackoverflow, pero aún no estaba en la versión beta. Entonces, después de buscar en Google la respuesta y encontrar solo un poco de información sobre ella, se me ocurrió lo siguiente y decidí publicarlo aquí ahora que estoy en la versión 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(@"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;
}
}
}
}
Luego, en la forma principal que utiliza el código anterior en estos 3 eventos:
- Activado
- desactivado
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; }
Th El único problema que veo actualmente es que si el programa falla, no tendrán el sonido de clic hasta que vuelvan a ejecutar mi aplicación, pero no sabrían hacer eso.
¿Qué piensan? ¿Es esta una buena solución? ¿Qué mejoras se pueden hacer?
Tuve un problema con esta línea: isEnabled = value; Acabo de comentárselo, pero quiero saber qué fue lo que se pretendía que fuera – Cristo