Der 'Klick-Sound' in Frage ist eigentlich eine systemweite Einstellung, so dass ich nur deaktiviert werden möchte, wenn meine Anwendung den Fokus hat und dann wieder aktivieren, wenn die Anwendung geschlossen wird/verliert den Fokus.HowTo Deaktivieren WebBrowser 'Klick Sound' nur in Ihrer App
Ursprünglich wollte ich diese Frage hier auf Stackoverflow stellen, aber ich war noch nicht in der Beta. Also, nachdem ich nach der Antwort gegoogelt und nur ein paar Informationen darüber gefunden hatte, kam ich auf das folgende und entschied, es hier zu posten, jetzt wo ich in der Beta bin.
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;
}
}
}
}
dann in der Hauptform verwenden wir den obigen Code in diesen 3 Veranstaltungen
- Aktiviert Deaktiviert
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 Das einzige Problem, das ich derzeit sehe, ist, wenn das Programm abstürzt, werden sie nicht den Klick-Sound haben, bis sie meine Anwendung neu starten, aber sie würden das nicht tun.
Was denkst du? Ist das eine gute Lösung? Welche Verbesserungen können vorgenommen werden?
Ich hatte ein Problem mit dieser Zeile: isEnabled = Wert; Ich habe es gerade kommentiert, aber ich möchte wissen, was es sein sollte – Cristo