code_junkie

HowTo Disable WebBrowser 'Click Sound' in your app only

Nov 10th, 2011
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. using System;
  2. using Microsoft.Win32;
  3.  
  4. namespace HowTo
  5. {
  6. class WebClickSound
  7. {
  8. /// <summary>
  9. /// Enables or disables the web browser navigating click sound.
  10. /// </summary>
  11. public static bool Enabled
  12. {
  13. get
  14. {
  15. RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEventsSchemesAppsExplorerNavigating.Current");
  16. string keyValue = (string)key.GetValue(null);
  17. return String.IsNullOrEmpty(keyValue) == false && keyValue != """";
  18. }
  19. set
  20. {
  21. string keyValue;
  22.  
  23. if (value)
  24. {
  25. keyValue = "%SystemRoot%\Media\";
  26. if (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor > 0)
  27. {
  28. // XP
  29. keyValue += "Windows XP Start.wav";
  30. }
  31. else if (Environment.OSVersion.Version.Major == 6)
  32. {
  33. // Vista
  34. keyValue += "Windows Navigation Start.wav";
  35. }
  36. else
  37. {
  38. // Don't know the file name so I won't be able to re-enable it
  39. return;
  40. }
  41. }
  42. else
  43. {
  44. keyValue = """";
  45. }
  46.  
  47. // Open and set the key that points to the file
  48. RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEventsSchemesAppsExplorerNavigating.Current", true);
  49. key.SetValue(null, keyValue, RegistryValueKind.ExpandString);
  50. isEnabled = value;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. private void Form1_Activated(object sender, EventArgs e)
  57. {
  58. // Disable the sound when the program has focus
  59. WebClickSound.Enabled = false;
  60. }
  61.  
  62.  
  63. private void Form1_Deactivate(object sender, EventArgs e)
  64. {
  65. // Enable the sound when the program is out of focus
  66. WebClickSound.Enabled = true;
  67. }
  68.  
  69.  
  70. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  71. {
  72. // Enable the sound on app exit
  73. WebClickSound.Enabled = true;
  74. }
  75.  
  76. webBrowser1.DocumentText = "<h1>Hello, world!</h1>";
  77.  
  78. webBrowser1.Document.OpenNew(true);
  79. webBrowser1.Document.Write("<h1>Hello, world!</h1>");
  80.  
  81. const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21;
  82. const int SET_FEATURE_ON_PROCESS = 0x00000002;
  83.  
  84. [DllImport("urlmon.dll")]
  85. [PreserveSig]
  86. [return: MarshalAs(UnmanagedType.Error)]
  87. static extern int CoInternetSetFeatureEnabled(
  88. int FeatureEntry,
  89. [MarshalAs(UnmanagedType.U4)] int dwFlags,
  90. bool fEnable);
  91.  
  92. static void DisableClickSounds()
  93. {
  94. CoInternetSetFeatureEnabled(
  95. FEATURE_DISABLE_NAVIGATION_SOUNDS,
  96. SET_FEATURE_ON_PROCESS,
  97. true);
  98. }
  99.  
  100. // backup value
  101. RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEventsSchemesAppsExplorerNavigating.Current");
  102. string BACKUP_keyValue = (string)key.GetValue(null);
  103.  
  104. // write nothing
  105. key = Registry.CurrentUser.OpenSubKey(@"AppEventsSchemesAppsExplorerNavigating.Current", true);
  106. key.SetValue(null, "", RegistryValueKind.ExpandString);
  107.  
  108. // do navigation ...
  109.  
  110. // write backup key
  111. RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEventsSchemesAppsExplorerNavigating.Current", true);
  112. key.SetValue(null, BACKUP_keyValue, RegistryValueKind.ExpandString);
Add Comment
Please, Sign In to add comment