C#使用蜂鸣器发音

突然来的想法,c#控制蜂鸣器发音,查了查写了个简单的demo

界面:

textbox;

button;

添加引用:

using System.Runtime.InteropServices;

代码:

using System.Runtime.InteropServices;

namespace beep
{

    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern int MessageBeep(uint uType);
        uint beep = 0x00000040;

        //发出不同类型的声音的参数如下:  
        //Ok = 0x00000000,  
        //Error = 0x00000010,  
        //Question = 0x00000020,  
        //Warning = 0x00000030,  
        //Information = 0x00000040  

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBeep(beep);     //系统声音发音
            try
            {
            int a=Convert.ToInt32(textBox1.Text);
            int b = 300;
            
                for (int i = 1; i < a; i++)
                {
                    Console.Beep(b, 100);//主板蜂鸣器发音,b是振动的Hz频率;100代表持续时间,单位是“毫秒”
                    b += 100;
                }
            }
            catch (Exception ex) { MessageBox.Show("输入播放循环次数"); }
        }
    }
}

 

posted on 2015-04-03 14:17  geekjames  阅读(2990)  评论(0编辑  收藏  举报

导航