Tuesday, March 08, 2005

Beep, MessageBeep

Here is three very simple methods to Beep.

  • Most simple way.
    Console.WriteLine("\a");
  • Second one is WinAPI MessageBeep function using PInvoke.
    using System;
    using System.Runtime.InteropServices;
    
    public class _Main
    {
        #region Win32
        [DllImport("user32.dll")]
        static extern void MessageBeep(uint uType); 
        
        const uint MB_OK                = 0x00000000;
        
        const uint MB_ICONHAND          = 0x00000010;
        const uint MB_ICONQUESTION      = 0x00000020;
        const uint MB_ICONEXCLAMATION   = 0x00000030;
        const uint MB_ICONASTERISK      = 0x00000040;
        
        #endregion
    
        public static void Main()
        {
            MessageBeep(MB_ICONEXCLAMATION);
        }
    }
    
  • Third is PInvoke too. This is WinAPI Beep function.
    using System;
    using System.Runtime.InteropServices;
    
    public class _Main
    {
        #region Win32
        
        [DllImport("Kernel32.dll")]
    static extern bool Beep( uint dwFreq, uint dwDuration ); #endregion public static void Main() { Beep(150, 150); } }

MSDN:

MessageBeep function

The MessageBeep function plays a waveform sound. The waveform sound for each sound type is identified by an entry in the registry.

Parameters

uType:Sound type, as identified by an entry in the registry.

Beep function

The Beep function generates simple tones on the speaker. The function is synchronous; it does not return control to its caller until the sound finishes.

Parameters

dwFreq:Frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF). dwDuration:Duration of the sound, in milliseconds.

Return Values:

If the function succeeds, the return value is true. If the function fails, the return value is false.

1 Comments:

Blogger gnash said...

System.Media.SystemSounds.Beep.Play();

or

System.Console.Beep(250, 500);

or

System.Console.Beep(250, 500);

3:53 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home