繁体   English   中英

C#禁用Webbrowser声音/应用程序声音

[英]C# Disable Webbrowser Sound/Application sound

我想禁用Web浏览器声音,但我认为这是不可能的,因此我看到可以在高于Win XP的系统上禁用应用程序声音,现在我只需要知道该怎么做,而我找不到它!

当前代码:

Form.ActiveForm.Hide();
        webBrowser1.ScriptErrorsSuppressed = true;
        try
        {
            webBrowser1.Navigate(args[2], null, null, "User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Xbox; Xbox One)");
        }
        catch (Exception ex)
        {
            Environment.Exit(0);
        }

我不认为有webrowser.noSound的东西,我也使用activeform.hide()来隐藏webbrowser

首先添加此名称空间:

using System.Runtime.InteropServices;

现在,您只需禁用所有音频输出即可。 试试这些代码:

[DllImport("winmm.dll")]
    public static extern int GetVolume(IntPtr p, out uint volume);

    [DllImport("winmm.dll")]
    public static extern int SetVolume(IntPtr p, uint volume);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Save the current volume
        int save;
        GetVolume(IntPtr.Zero, out save);

        this.FormClosing += delegate 
        {
            // Restore the volume 
            SetVolume(IntPtr.Zero, save);
        };

        // Now you can mute sounds
        SetVolume(IntPtr.Zero, 0);
        string url = "http://www.example.com";
        this.webBrowser1.Navigate(url);
    }

更新:

您可以阅读“此人也回答了这个问题”。

更新2:

您可以将其放在静态类中,并公开CoInternetSetFeatureEnabled方法,或者添加一个附加的bridge方法,如果需要的话,可以从更可用的形式转换参数后调用该方法。

阅读这两个类似的问题并禁用声音: Question1 Question2

更新3:

对于IE7及更高版本,您可以使用CoInternetSetFeatureEnabled:

// Constants
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;

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

......

// You can call the CoInternetSetFeatureEnabled like this:
CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_PROCESS, true);

这是来源

更新4:

如何静音Windows声音

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM