앱에서만 WebBrowser '클릭 사운드'를 비활성화하는 방법
문제
문제의 '클릭 소리'는 실제로 시스템 전반에 걸친 기본 설정이므로 내 응용 프로그램에 포커스가 있을 때만 비활성화하고 응용 프로그램이 포커스를 닫거나 잃을 때 다시 활성화하고 싶습니다.
원래는 여기 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가지 이벤트에 사용합니다.
- 활성화됨
- 비활성화됨
양식닫기
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; }
현재 내가 보는 한 가지 문제는 프로그램이 충돌하면 내 응용 프로그램을 다시 시작할 때까지 클릭 소리가 들리지 않지만 그렇게 하는 방법을 모른다는 것입니다.
여러분은 어떻게 생각하시나요?이것이 좋은 해결책입니까?어떤 개선이 이루어질 수 있습니까?
해결책
WebBrowser.DocumentText 대신 WebBrowser.Document.Write를 사용하면 클릭 소리가 발생하지 않는 것으로 나타났습니다.
따라서 이 대신:
webBrowser1.DocumentText = "<h1>Hello, world!</h1>";
이 시도:
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");
다른 팁
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);
}
사운드 탐색의 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);