C#のWindowsアプリケーションでビープ音、システムサウンド音を鳴らすコードと実行結果を紹介します。
ビープやシステムサウンド音を鳴らすには以下の方法があります。
Windowsアプリケーションを新規作成しフォームにボタンを5つ配置します。
以下のコードを記述します。
using Microsoft.VisualBasic;
using System.Runtime.InteropServices;
namespace BeepDemo
{
public partial class FormMain : Form
{
public enum beepType : uint
{
/// <summary>
/// A simple windows beep
/// </summary>
SimpleBeep = 0xFFFFFFFF,
/// <summary>
/// A standard windows OK beep
/// </summary>
OK = 0x00,
/// <summary>
/// A standard windows Question beep
/// </summary>
Question = 0x20,
/// <summary>
/// A standard windows Exclamation beep
/// </summary>
Exclamation = 0x30,
/// <summary>
/// A standard windows Asterisk beep
/// </summary>
Asterisk = 0x40,
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool MessageBeep(beepType uType);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool Beep(uint dwFreq, uint dwDuration);
public FormMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Console.Beep();
}
private void button2_Click(object sender, EventArgs e)
{
Interaction.Beep();
}
private void button3_Click(object sender, EventArgs e)
{
MessageBeep(0);
}
private void button4_Click(object sender, EventArgs e)
{
Beep(700, 40);
}
private void button5_Click(object sender, EventArgs e)
{
System.Media.SystemSounds.Beep.Play();
//System.Media.SystemSounds.Asterisk.Play();
//System.Media.SystemSounds.Exclamation.Play();
//System.Media.SystemSounds.Hand.Play();
//System.Media.SystemSounds.Question.Play();
}
}
}
button1のクリックイベントでは、Console.Beep();
メソッドを呼び出し、ビープ音を鳴らします。
private void button1_Click(object sender, EventArgs e)
{
Console.Beep();
}
button2のクリックイベントでは、Interaction.Beep();
メソッドを呼び出し、Windowsのシステムサウンドを鳴らします。
Windows APIを呼び出すため、DllImport属性を利用して、MessageBeep 関数をインポートしています。
private void button2_Click(object sender, EventArgs e)
{
Interaction.Beep();
}
button3のクリックイベントでは、MessageBeep(0);
Windows APIを呼び出し、Windowsのシステムサウンドを鳴らします。
private void button3_Click(object sender, EventArgs e)
{
MessageBeep(0);
}
button4のクリックイベントでは、Beep(0);
Windows APIを呼び出し、ビープ音を鳴らします。
Windows APIを呼び出すため、DllImport属性を利用して、Beep 関数をインポートしています。
1つ目の引数に、鳴らす音の周波数 (ヘルツ単位)を与えます。2つ目の引数に、サウンドの再生時間 (ミリ秒単位)を与えます。
下記コードでは、700ヘルツのビープ音を40ミリ秒再生します。
private void button4_Click(object sender, EventArgs e)
{
Beep(700,40);
}
button5のクリックイベントでは、System.Media.SystemSounds
クラスのメソッドを利用してシステムサウンド恩を再生します。
private void button5_Click(object sender, EventArgs e)
{
System.Media.SystemSounds.Beep.Play();
//System.Media.SystemSounds.Asterisk.Play();
//System.Media.SystemSounds.Exclamation.Play();
//System.Media.SystemSounds.Hand.Play();
//System.Media.SystemSounds.Question.Play();
}
プロジェクトを実行します。
下図のウィンドウが表示されます。
Windowsのシステムサウンドを再生する場合は、System.Media.SystemSounds名前空間のクラスを利用できます。
システムサウンドは以下のメソッドで再生できます。
メソッド | 動作 |
---|---|
System.Media.SystemSounds.Asterisk.Play(); | メッセージ(情報)のサウンド再生 |
System.Media.SystemSounds.Beep.Play(); | 一般の警告音の再生 |
System.Media.SystemSounds.Exclamation.Play(); | メッセージ(警告)のサウンド再生 |
System.Media.SystemSounds.Hand.Play(); | システムエラーのサウンド再生 |
System.Media.SystemSounds.Question.Play(); | メッセージ(問い合わせ)のサウンド再生 |