tags:

views:

190

answers:

2

I have an event when fired does this:

htmlView.DocumentText = contentArea.Text;

The problem is this event is fired a lot and I get that annoying IE "click noise". I've seen a solution http://stackoverflow.com/questions/10456/howto-disable-webbrowser-click-sound-in-your-app-only and the other one, but I'm pretty new to C Sharp (only been using it for a couple of hours), and am unsure what:


private const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21;
private const int SET_FEATURE_ON_THREAD = 0x00000001;
private const int SET_FEATURE_ON_PROCESS = 0x00000002;
private const int SET_FEATURE_IN_REGISTRY = 0x00000004;
private const int SET_FEATURE_ON_THREAD_LOCALMACHINE = 0x00000008;
private const int SET_FEATURE_ON_THREAD_INTRANET = 0x00000010;
private const int SET_FEATURE_ON_THREAD_TRUSTED = 0x00000020;
private const int SET_FEATURE_ON_THREAD_INTERNET = 0x00000040;
private const int SET_FEATURE_ON_THREAD_RESTRICTED = 0x00000080;

...

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


...means. Where do I put that code? Sorry, I'm a noob with C Sharp and Visual Studio and would appreciate help that a neophyte could understand.

Thanks!

+1  A: 

You can put it inside a static class and either make the CoInternetSetFeatureEnabled method public, or add an additional bridge method that calls it after converting the parameters from a more usable form, if necessary. For example, in the first case it would be:

public static class UnmanagedCode
{
    private const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21;
    //etc...

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

Thank you Konamiman!

Here is my final code:


public static class UnmanagedCode
    {
        private const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21;
        private const int SET_FEATURE_ON_THREAD = 0x00000001;
        private const int SET_FEATURE_ON_PROCESS = 0x00000002;
        private const int SET_FEATURE_IN_REGISTRY = 0x00000004;
        private const int SET_FEATURE_ON_THREAD_LOCALMACHINE = 0x00000008;
        private const int SET_FEATURE_ON_THREAD_INTRANET = 0x00000010;
        private const int SET_FEATURE_ON_THREAD_TRUSTED = 0x00000020;
        private const int SET_FEATURE_ON_THREAD_INTERNET = 0x00000040;
        private const int SET_FEATURE_ON_THREAD_RESTRICTED = 0x00000080;

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

        public static int disableSound()
        {
            UnmanagedCode.CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_THREAD, true);
            UnmanagedCode.CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_PROCESS, true);
            UnmanagedCode.CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_IN_REGISTRY, true);
            UnmanagedCode.CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_THREAD_LOCALMACHINE, true);
            UnmanagedCode.CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_THREAD_INTRANET, true);
            UnmanagedCode.CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_THREAD_TRUSTED, true);
            UnmanagedCode.CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_THREAD_INTERNET, true);
            UnmanagedCode.CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_THREAD_RESTRICTED, true);

            return 1;
        }
    }
 static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            UnmanagedCode.disableSound();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }


Breck
Glad to have been of help. Just one more note: if you don't directly call he CoInternetSetFeatureEnabled method from outside the UnmanagedCode class, it is better to mark it as private.
Konamiman