gpt4 book ai didi

c# - 如何仅在您的应用中禁用 Web 浏览器 'Click Sound'

转载 作者:IT王子 更新时间:2023-10-29 04:16:08 25 4
gpt4 key购买 nike

有问题的“点击声音”实际上是系统范围的首选项,所以我只希望在我的应用程序获得焦点时禁用它,然后在应用程序关闭/失去焦点时重新启用它。

本来我想在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;
    }

我目前看到的一个问题是,如果程序崩溃,他们在重新启动我的应用程序之前不会听到点击声音,但他们并不知道这样做。

大家怎么看?这是一个好的解决方案吗?可以做哪些改进?

最佳答案

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

关于c# - 如何仅在您的应用中禁用 Web 浏览器 'Click Sound',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com