2008-08-13 43 views
27

問題中的'點擊聲音'實際上是系統範圍的首選項,所以我只希望在應用程序有焦點時禁用它,然後在應用程序關閉時重新啓用/失去重點。如何在您的應用中禁用WebBrowser'點擊聲音'

最初,我想在stackoverflow上問這個問題,但我還沒有在測試中。所以,在Google上搜索答案並找到一點信息之後,我想出了以下內容,現在決定在這裏發佈我正在測試的內容。

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

然後,在主要形式,我們使用在這些3個事件上面的代碼:

  • 活化
  • 停用
  • 的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; 
    } 
    

釷我目前看到的一個問題是,如果程序崩潰,他們將不會有點擊聲音,直到他們重新啓動我的應用程序,但他們不知道這樣做。

你們認爲什麼?這是一個好的解決方案嗎?可以做什麼改進?

+0

我對這一行有一個問題:isEnabled = value; 我剛剛評論過它,但我想知道它的目的是什麼 – Cristo 2013-03-31 20:57:20

回答

12

我注意到,如果您使用WebBrowser.Document.Write而不是WebBrowser.DocumentText,那麼點擊聲音不會發生。

因此,不是這樣的:

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

試試這個:

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

您建議的解決方案可以防止控制發出咔噠聲,但另一方面,這種方法會導致控件出現嚴重的焦點問題。在我的情況下,webbrowser控件在事件循環結束時回退焦點。據我所知,沒有解決方法。 在我的情況下,我需要回到DocumentText解決方案 - 但仍然在尋找禁用令人討厭的聲音。任何其他想法? – 2009-05-31 09:28:56

+2

剛剛發現,詹姆斯在這個線程:http://stackoverflow.com/questions/393166/how-to-disable-click-sound-in-webbrowser-control有一個很好的解決方案 - 至少對於IE7和IE8。我可以使用DocumentText屬性,沒有焦點問題,最重要的是,我沒有點擊聲音。 – 2009-05-31 10:23:03

0

肯定感覺就像一個黑客攻擊,但已經做了一些這方面的研究很長一段時間以前,沒有發現任何其他的解決方案,可能是你最好的選擇。

更重要的是將設計應用程序,以便它不需要很多煩人的頁面重新加載。例如,如果你耳目一新的iframe來檢查服務器上的更新,使用XMLHttpRequest代替。 (?你能告訴我是回來處理這個問題在未來的日子術語「AJAX」被創造出來之前)

0

如果你想使用替代Windows註冊表,使用此:

// 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); 
39
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); 
} 
+2

爲InterOp方式+1,這實際上工作! – CodeWrite 2011-07-02 09:32:24

1

您可以通過更改Internet Explorer註冊表值,禁用它的導航聲音的爲「NULL」:

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

而且通過改變導航聲音的Internet Explorer的註冊表值,使之爲「C:\ WINDOWS \媒體\市容\ Windows下ñ avigation Start.wav「:

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