[英]How to disable Click Sound using a WebBrowser element in C Sharp?
[英]HowTo Disable WebBrowser 'Click Sound' in your app only
有問題的“點擊聲音”實際上是系統范圍的偏好,因此我只希望在我的應用程序具有焦點時禁用它,然后在應用程序關閉/失去焦點時重新啟用它。
最初,我想在stackoverflow上問這個問題,但我還沒有進入測試階段。 因此,在谷歌搜索答案並找到一點信息之后,我想出了以下內容並決定在此處發布,因為我已經進入測試版。
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; }
我目前看到的一個問題是,如果程序崩潰,他們將無法獲得點擊聲,直到他們重新啟動我的應用程序,但他們不知道這樣做。
你們有什么感想? 這是一個好的解決方案嗎? 可以做些什么改進?
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);
}
我注意到如果您使用WebBrowser.Document.Write而不是WebBrowser.DocumentText,則不會發生咔嗒聲。
所以不是這樣的:
webBrowser1.DocumentText = "<h1>Hello, world!</h1>";
嘗試這個:
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");
您通過將導航聲音的Internet Explorer注冊表值更改為“NULL”來禁用它:
Registry.SetValue("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current","","NULL");
並通過將導航聲音的Internet Explorer注冊表值更改為“C:\\ Windows \\ Media \\ Cityscape \\ Windows Navigation Start.wav”來啟用它:
Registry.SetValue("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\Explorer\\Navigating\\.Current","","C:\Windows\Media\Cityscape\Windows Navigation Start.wav");
絕對感覺像是一個黑客,但很久以前就已經做了一些研究而沒有找到任何其他解決方案,可能是你最好的選擇。
更好的是設計你的應用程序,因此它不需要很多惱人的頁面重新加載..例如,如果你刷新iframe以檢查服務器上的更新,請改用XMLHttpRequest。 (在“AJAX”這個詞被創造出來之前的幾天,你能說出我正在處理這個問題嗎?)
如果要使用替換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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.