C#でビープ音を鳴らすVBではBeep関数により、簡単にビープ音(一般の警告音)を鳴らすことが出来ます。C#で同じようにビープ音を鳴らすには、Microsoft.VisualBasic名前空間内InteractionクラスのBeepメソッドを使うか、あるいはWin32 APIのMessageBeep関数を使います。 .NET Framework 2.0以降では、Console.Beepメソッドや、SystemSounds.Beep.Playメソッドで鳴らすこともできます。 Win32 APIのMessageBeep関数を使用した方法次にWin32 APIのMessageBeep関数でビープ音を鳴らす例を示します。なおMessageBeep関数について詳しくは、「システムサウンドを再生する」で説明しています。 //ビープ音を鳴らす [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern int MessageBeep(uint n); public static void Beep() { MessageBeep(0); } .NET Framework 2.0以降で、Console.BeepメソッドやSystemSounds.Beep.Playメソッドを使用した方法.NET Framework 2.0以降で、Console.BeepメソッドやSystemSounds.Beep.Playメソッドを使う例は、以下のとおりです。なおSystemSoundsクラスについて詳しくは、「システムサウンドを再生する」で説明しています。 '800Hzの周波数で200ミリ秒間再生する Console.Beep() '10000の周波数で500ミリ秒間再生する Console.Beep(10000, 500) //800Hzの周波数で200ミリ秒間再生する Console.Beep(); //10000の周波数で500ミリ秒間再生する Console.Beep(10000, 500); //SystemSoundsクラスで一般の警告音を鳴らす System.Media.SystemSounds.Beep.Play(); Console.Beepメソッドは他の方法と少し違います。Console.BeepメソッドはWin32 APIのBeep関数を呼び出すため、PC本体(マザーボード)のスピーカーからビープ音を鳴らします。なお、Beep関数はWindows Vista x64およびWindows XP 64-Bit Editionではサポートされていません。
|
|
Copyright(C) DOBON!. All rights reserved.
|