突然來的想法,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("輸入播放循環次數"); } } } }