如何在Windows上的C语言中发出蜂鸣声? [英] How to make a Beep sound in C on Windows?

查看:889
本文介绍了如何在Windows上的C语言中发出蜂鸣声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个包含蜂鸣声的程序.我使用32位Windows Vista.我正在使用在GNU编译器上运行的Code :: Blocks IDE.我的示例代码是-

I am trying to make a program which includes a beep noise. I work on a 32 bit Windows Vista. I am using the Code::Blocks IDE which runs on a GNU compiler. My sample code is -

#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发出哔声.我试过了,但是没有用.我检查了扬声器和声卡.一切都很完美,但我没有听到任何提示音.甚至我在示例代码中显示的方法也不起作用.

On the Internet I read that we could also use \a in printf to make a beep. I tried that but it is not working. I checked my speakers and sound card. Everything is perfect but I hear no beep. Even the method I displayed in my sample code does not work.

推荐答案

C标准建议编写'\a'到标准输出会产生声音或可见的警报信号,但是如果重定向标准输出,它将不起作用.同样,某些较新的计算机缺少Windows Beep()和某些终端所依赖的PC提示音.要使Windows PC在桌面应用程序中播放警报声音,可以调用Windows特定的

The C standard 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 function, which plays a sound "asynchronously" (in the background while your program continues to run). The user can configure which sound is associated with each of these four values in the Sound control panel.

#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中定义的,因此,如果这会导致链接错误,请确保要链接到相应的导入库.在MinGW GCC(代码:: Blocks中的编译器)中,将-lUser32添加到传递给链接器的库列表中.

MessageBeep() is defined in User32.dll, so if this gives you link errors, make sure you're linking to the corresponding import library. In MinGW GCC (the compiler in Code::Blocks), add -lUser32 to the list of libraries passed to the linker.

这篇关于如何在Windows上的C语言中发出蜂鸣声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆