每当触发我的C#程序中使用的消息框时,我的计算机都会发出非常烦人的哔哔声。如何使用C#代码禁用此蜂鸣声。
我使用的代码非常简单。
MessageBox.show("text");
这就是我解决这个小问题的方法。
将这个简单的类添加到您的项目中。它现在是我个人图书馆的一部分,但您可以直接在项目中添加该类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace MyCSharpLibrary
{
public class Volume
{
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr h, out uint dwVolume);
[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr h, uint dwVolume);
private static uint _savedVolumeLevel;
private static Boolean VolumeLevelSaved = false;
// ----------------------------------------------------------------------------
public static void On()
{
if (VolumeLevelSaved)
{
waveOutSetVolume(IntPtr.Zero, _savedVolumeLevel);
}
}
// ----------------------------------------------------------------------------
public static void Off()
{
waveOutGetVolume(IntPtr.Zero, out _savedVolumeLevel);
VolumeLevelSaved = true;
waveOutSetVolume(IntPtr.Zero, 0);
}
}
}
现在在调用MessageBox和Volume.On()之后调用Volume.Off()
Volume.Off();
MessageBox.Show("\n Information \n", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Volume.On();
我更喜欢这种方法,因为我不必对Windows进行修改,我可以为MessageBoxes选择任何我想要的图标。
谢谢你,再见
在您尝试之前,这听起来很奇怪。打开命令提示符,键入:
net stop beep
我做了一个快速的谷歌,并找到了4个其他方式:
尝试使用VisualBasic MsgBox类而不是MessageBox。我无法解释原因,但在我的计算机上,VisualBasic不会发出哔哔声。
http://msdn.microsoft.com/en-us/library/sfw6660x%28VS.85%29.aspx
所以代替:
MessageBox.Show("text")
做:
MsgBox("text")
您必须从Microsoft.VisualBasic导入MsgBox函数,而不是从System.Windows.Forms导入MessageBox。但那应该不是问题;它是.NET API的一部分。
编辑:好的,现在我可以解释一下。缺少声音是因为默认情况下,MessageBox的VB版本未修饰(即,它不是信息,问题等)。我假设毕竟没有真正需要使用VB MsgBox类 - 可能如果你使常规的旧MessageBox未修饰,它也不会发出任何声音。 :)
您应该将它留给最终用户来决定他想要的声音。他可以为系统事件配置/禁用声音,例如控制面板/声音和音频设备/声音/节目事件中的消息框。
停止蜂鸣声服务需要管理权限,而不是通常只为一个应用程序执行的操作。
如果您确实要使用自定义对话框来替换MessageBox,请记住实现CTL-C(将消息框内容复制到剪贴板)。
尽管这个问题很老,但我遇到了同样的问题但能够以一种非常简单的方式解决它。
MessageBox.Show("Your text here", "Information", MessageBoxButtons.OK, MessageBoxIcon.None);
通过将MessageBoxIcon
设置为None
,系统不会产生声音。但是,盒子上没有图标。像this之类的东西。
根据您使用MessageBox(图标等)的程度,您可以随时进行自定义对话。特别是如果它是一个你想要显示的简单信息,这将不困难或耗时。