"Sunetul de clic" în cauză este, de fapt, o preferință la nivel de sistem, așadar vreau doar să fie dezactivat atunci când aplicația mea se concentrează și apoi reactivează atunci când aplicația închide/pierde focalizarea.
Inițial, am vrut să pun această întrebare aici pe stackoverflow, dar nu am fost încă în beta. Deci, după ce am răspuns la răspuns și am găsit doar câteva informații despre el, am venit cu următorul text și am decis să îl post pe care îl am aici acum, când sunt în beta.
using System;
using Microsoft.Win32;
namespace HowTo
{
class WebClickSound
{
///
/// Enables or disables the web browser navigating click sound.
///
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;
}
}
}
}
Apoi, în forma principală vom folosi codul de mai sus în aceste 3 evenimente:
- 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;
}
Singura problemă pe care o văd în prezent este că în cazul în care programul se blochează, nu va avea sunetul de clic până când nu-și relansează aplicația, dar nu ar ști să facă acest lucru.
Voi ce credeți? Este o soluție bună? Ce îmbunătățiri pot fi făcute?