【问题标题】:How to disable Click Sound using a WebBrowser element in C Sharp?如何使用 C Sharp 中的 WebBrowser 元素禁用 Click Sound?
【发布时间】:2009-11-12 09:07:40
【问题描述】:

我有一个事件被触发时这样做:

htmlView.DocumentText = contentArea.Text;

问题是这个事件被触发了很多,我得到那个烦人的 IE “点击噪音”。我已经看到了一个解决方案 HowTo Disable WebBrowser 'Click Sound' in your app only 和另一个,但我对 C Sharp 很陌生(只使用了几个小时),我不确定是什么:


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

...意思是。我把那个代码放在哪里?抱歉,我是 C Sharp 和 Visual Studio 的菜鸟,希望新手能够理解的帮助。

谢谢!

【问题讨论】:

    标签: c# audio


    【解决方案1】:

    您可以将它放在一个静态类中,或者将CoInternetSetFeatureEnabled 方法公开,或者在必要时添加一个额外的桥接方法,在将参数从更可用的形式转换后调用它。例如,在第一种情况下,它将是:

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

    【讨论】:

    • 嘿,我在我的代码中使用了这个方法。它给出了一个错误提示“尝试访问该方法失败”
    【解决方案2】:

    谢谢Konamiman!

    这是我的最终代码:


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

    【讨论】:

    • 很高兴能帮上忙。还要注意一点:如果你不直接从 UnmanagedCode 类外部调用他的 CoInternetSetFeatureEnabled 方法,最好将其标记为私有。
    猜你喜欢
    • 2020-02-20
    • 1970-01-01
    • 2018-06-08
    • 2014-06-22
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多