簡體   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