如何在 Windows 上用 C 发出哔声?
How to make a Beep sound in C on Windows?
我正在尝试制作一个包含嘟嘟声的程序。我在 32 位 Windows Vista 上工作。我正在使用在 GNU 编译器上运行的 Code::Blocks IDE。我的示例代码是 -
#include <stdio.h>
#include <windows.h>
#include <dos.h>
int main(void)
{
Beep(750, 300);
printf("\n \n \t This is a dummy program for Beep.");
getch();
return 0;
}
在网上看到说我们也可以在printf中使用\a来发出哔哔声。我试过了,但没有用。我检查了我的扬声器和声卡。一切都很完美,但我没有听到哔哔声。即使我在示例代码中显示的方法也不起作用。
C 标准 recommends that writing '\a'
to standard output produce an audible or visible alert signal, but it will not work if standard output is redirected. Likewise, some newer computers lack the PC beeper on which Windows Beep()
and some terminals rely. To cause a Windows PC to play an alert sound in a desktop application, you can call the Windows-specific MessageBeep
函数,它播放声音 "asynchronously"(在您的程序继续 运行 时在后台播放)。用户可以在声音控制面板中配置与这四个值中的每一个关联的声音。
#include <windows.h>
/* Include one of these in a function */
MessageBeep(MB_OK); /* play Windows default beep */
MessageBeep(MB_ICONINFORMATION); /* play asterisk sound */
MessageBeep(MB_ICONQUESTION); /* play question sound */
MessageBeep(MB_ICONWARNING); /* play warning sound */
MessageBeep()
在 User32.dll
中定义,因此如果这给您带来 link 错误,请确保您正在 linking 到相应的导入库。在 MinGW GCC(Code::Blocks 中的编译器)中,将 -lUser32
添加到传递给 linker 的库列表中。
要使 windows.h 中的 Beep()
功能真正发挥作用,您的 PC 中必须装有 "PC speaker" 蜂鸣器,如功能文档中所述。因此,您需要一台相当旧的 PC,并且 Windows XP 或更早版本,因为 Windows Vista 显然已放弃对该功能的支持。
在较新的 Windows 版本中,调用 Beep()
会使用您的声卡在扬声器中发出哔哔声。如果您没有听到任何蜂鸣声,则可能与程序无关,但可能与您的特定计算机硬件有关。
MessageBeep(-1);
来自 MSDN 文档:
MessageBeep function
Plays a waveform sound. The waveform sound for each sound type is
identified by an entry in the registry.
BOOL WINAPI MessageBeep( _In_ UINT uType );
... ...
Value for uType
: 0xFFFFFFFF
Meaning: A simple beep. If the
sound card is not available, the sound is generated using the speaker.
此外,令我惊讶的是,我已经对此进行了测试。至少 Windows 7 32 位(当然 Windows Vista 也是)对旧的 8253 I/O 端口和键盘端口进行某种模拟,可用于 ring 3 个进程,所以旧的sound()
和 nosound()
的实现应该有效。不幸的是,我没有任何 32 位机器可用的 ATM,所以我无法确认这一点。
您可以使用 \a
。至少它在我的电脑上有效。
Beep 在 Windows 中再次起作用,因为 windows 7。格式为:
Beep(frequency, duration) where frequency is the pitch in hertz, and duration is the length in milliseconds
见https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx
这个适用于 Windows 7,用 Visual Studio 2017 编译。没问题。
printf("\n Bad request - check status code parameter\a");
我正在尝试制作一个包含嘟嘟声的程序。我在 32 位 Windows Vista 上工作。我正在使用在 GNU 编译器上运行的 Code::Blocks IDE。我的示例代码是 -
#include <stdio.h>
#include <windows.h>
#include <dos.h>
int main(void)
{
Beep(750, 300);
printf("\n \n \t This is a dummy program for Beep.");
getch();
return 0;
}
在网上看到说我们也可以在printf中使用\a来发出哔哔声。我试过了,但没有用。我检查了我的扬声器和声卡。一切都很完美,但我没有听到哔哔声。即使我在示例代码中显示的方法也不起作用。
C 标准 recommends that writing '\a'
to standard output produce an audible or visible alert signal, but it will not work if standard output is redirected. Likewise, some newer computers lack the PC beeper on which Windows Beep()
and some terminals rely. To cause a Windows PC to play an alert sound in a desktop application, you can call the Windows-specific MessageBeep
函数,它播放声音 "asynchronously"(在您的程序继续 运行 时在后台播放)。用户可以在声音控制面板中配置与这四个值中的每一个关联的声音。
#include <windows.h>
/* Include one of these in a function */
MessageBeep(MB_OK); /* play Windows default beep */
MessageBeep(MB_ICONINFORMATION); /* play asterisk sound */
MessageBeep(MB_ICONQUESTION); /* play question sound */
MessageBeep(MB_ICONWARNING); /* play warning sound */
MessageBeep()
在 User32.dll
中定义,因此如果这给您带来 link 错误,请确保您正在 linking 到相应的导入库。在 MinGW GCC(Code::Blocks 中的编译器)中,将 -lUser32
添加到传递给 linker 的库列表中。
要使 windows.h 中的 Beep()
功能真正发挥作用,您的 PC 中必须装有 "PC speaker" 蜂鸣器,如功能文档中所述。因此,您需要一台相当旧的 PC,并且 Windows XP 或更早版本,因为 Windows Vista 显然已放弃对该功能的支持。
在较新的 Windows 版本中,调用 Beep()
会使用您的声卡在扬声器中发出哔哔声。如果您没有听到任何蜂鸣声,则可能与程序无关,但可能与您的特定计算机硬件有关。
MessageBeep(-1);
来自 MSDN 文档:
MessageBeep function
Plays a waveform sound. The waveform sound for each sound type is identified by an entry in the registry.
BOOL WINAPI MessageBeep( _In_ UINT uType );
... ...Value for
uType
:0xFFFFFFFF
Meaning: A simple beep. If the sound card is not available, the sound is generated using the speaker.
此外,令我惊讶的是,我已经对此进行了测试。至少 Windows 7 32 位(当然 Windows Vista 也是)对旧的 8253 I/O 端口和键盘端口进行某种模拟,可用于 ring 3 个进程,所以旧的sound()
和 nosound()
的实现应该有效。不幸的是,我没有任何 32 位机器可用的 ATM,所以我无法确认这一点。
您可以使用 \a
。至少它在我的电脑上有效。
Beep 在 Windows 中再次起作用,因为 windows 7。格式为:
Beep(frequency, duration) where frequency is the pitch in hertz, and duration is the length in milliseconds
见https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx
这个适用于 Windows 7,用 Visual Studio 2017 编译。没问题。
printf("\n Bad request - check status code parameter\a");