Domanda

Il "suono del clic" in questione è in realtà una preferenza a livello di sistema, quindi voglio che sia disabilitato solo quando la mia applicazione è attiva e quindi riattivato quando l'applicazione chiude/perde il focus.

Inizialmente volevo porre questa domanda qui su StackOverflow, ma non ero ancora nella versione beta.Quindi, dopo aver cercato su Google la risposta e aver trovato solo poche informazioni al riguardo, ho pensato a quanto segue e ho deciso di pubblicarlo qui ora che sono nella 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;
            }
        }
    }
}

Quindi nel modulo principale utilizziamo il codice sopra in questi 3 eventi:

  • Attivato
  • Disattivato
  • Chiusura del modulo

    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;
    }
    

L'unico problema che vedo attualmente è che se il programma si arresta in modo anomalo, non sentiranno il suono del clic finché non riavvieranno la mia applicazione, ma non saprebbero farlo.

Che cosa ne pensate?È una buona soluzione?Quali miglioramenti si possono apportare?

È stato utile?

Soluzione

Ho notato che se usi WebBrowser.Document.Write anziché WebBrowser.DocumentText, il suono del clic non si verifica.

Quindi invece di questo:

webBrowser1.DocumentText = "<h1>Hello, world!</h1>";

prova questo:

webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");

Altri suggerimenti

const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21;
const int SET_FEATURE_ON_PROCESS = 0x00000002;

[DllImport("urlmon.dll")]
[PreserveSig]
[return: MarshalAs(UnmanagedType.Error)]
static extern int CoInternetSetFeatureEnabled(int FeatureEntry,
                                              [MarshalAs(UnmanagedType.U4)] int dwFlags,
                                              bool fEnable);

static void DisableClickSounds()
{
    CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS,
                                SET_FEATURE_ON_PROCESS,
                                true);
}

Puoi disabilitarlo modificando il valore del registro di Internet Explorer del suono di navigazione su "NULL":

Registry.SetValue("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current","","NULL");

E attivalo modificando il valore del registro di Internet Explorer dell'audio di navigazione in "C:\Windows\Media\Cityscape\Windows Navigation Start.wav":

Registry.SetValue("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current","","C:\Windows\Media\Cityscape\Windows Navigation Start.wav");

Sicuramente sembra un trucco, ma avendo fatto qualche ricerca su questo molto tempo fa e non avendo trovato altre soluzioni, probabilmente la soluzione migliore.

Meglio ancora sarebbe progettare la tua applicazione in modo che non richieda molti fastidiosi ricaricamenti di pagina.ad esempio, se stai aggiornando un iframe per verificare la presenza di aggiornamenti sul server, utilizza invece XMLHttpRequest.(Puoi dire che avevo a che fare con questo problema prima che fosse coniato il termine "AJAX"?)

Se desideri utilizzare la sostituzione del registro di Windows, utilizza questo:

// backup value
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current");
string BACKUP_keyValue = (string)key.GetValue(null);

// write nothing
key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current", true);
key.SetValue(null, "",  RegistryValueKind.ExpandString);

// do navigation ...

// write backup key
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current", true);
key.SetValue(null, BACKUP_keyValue,  RegistryValueKind.ExpandString);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top