Pregunta

El 'sonido de" clic " en la pregunta en realidad es un amplio sistema de preferencia, así que sólo quiero que esté desactivada cuando mi solicitud ha de enfoque y, a continuación, volver a habilitar cuando la aplicación se cierra/pierde el foco.

Originalmente, quería hacer esta pregunta en stackoverflow, pero yo todavía no estaba en la beta.Así que, después de googlear para la respuesta y sólo encontrar un poco de información sobre ella, se me ocurrió la siguiente y se decidió a publicar aquí ahora que estoy en la 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 el formulario principal utilizamos el código de arriba 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;
    }
    

El problema que veo actualmente es que si el programa se bloquea no tiene el sonido de "clic" hasta que se re-lanzamiento de mi aplicación, pero no sé hacerlo.

¿Ustedes qué piensan?Es esta una buena solución?¿Qué mejoras se pueden hacer?

¿Fue útil?

Solución

Me he dado cuenta que si utiliza el Navegador web.Documento.La escritura en lugar de WebBrowser.DocumentText, a continuación, haga clic en el sonido que no suceda.

Así que en lugar de esto:

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

intente esto:

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

Otros consejos

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

Deshabilitar cambiando de registro de Internet Explorer valor de navegación de sonido a "NULL":

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

Y activar la opción de cambiar de registro de Internet Explorer valor de navegación de sonido "C:\Windows\Media\Cityscape\Windows Navegación Inicio.wav":

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

Definitivamente se siente como un hack, pero después de haber hecho algunas investigaciones sobre esta hace mucho tiempo y no encontrar otras soluciones, probablemente su mejor apuesta.

Mejor aún sería el diseño de su aplicación por lo que no requiere de muchos molestos página se vuelve a cargar..por ejemplo, si usted es la actualización de un iframe para comprobar si hay actualizaciones en el servidor, el uso de XMLHttpRequest en su lugar.(Puedes decir que yo estaba lidiando con este problema en los días antes de que el término "AJAX" fue acuñado?)

Si desea utilizar la sustitución del Registro de Windows, utilice esto:

// 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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top