티스토리 뷰

Application/C#

윈폼 어플리케이션에서 Beep 음 내기

알 수 없는 사용자 2008. 1. 28. 20:56
How do I beep the computer's speaker in a Windows Form application




There is no Windows Form function to beep your computer's speaker. But you can just invoke the Win32 API MessageBeep.





using System.Runtime.InteropServices;





...





 





[DllImport("user32.dll")]





public static extern int MessageBeep(uint n);





 





private void button2_Click(object sender, System.EventArgs e)





{





MessageBeep(0x0);





}





 





Another method (suggested by msauper@sauper.com on microsoft.public.dotnet.framework.windowsforms)





 





Reference the VB.NET runtime support and just use the Beep() method.





 





The method is in:





 





Microsoft.Visual Basic.NET Runtime





 





The method is:





 





Microsoft.VisualBasic.Interaction.Beep();





 





 

이것 이외의 다른 방법은...



using System;
using System.Runtime.InteropServices;
using System.IO;

namespace Usecase
{
/// <summary>
/// 사운드
/// </summary>
public class WPlaySound
{
[DllImport("winmm.dll", EntryPoint="PlaySound",CharSet=CharSet.Auto)]
private static extern int PlaySound(String pszSound, int hmod, int falgs);
public enum SND
{
SND_SYNC ? ? ? ?= 0x0000 ?,/* play synchronously (default) */
SND_ASYNC ? ? ? ?= 0x0001 , /* play asynchronously */
SND_NODEFAULT ? ? ? ?= 0x0002 , /* silence (!default) if sound not found */
SND_MEMORY ? ? ? ?= 0x0004 , /* pszSound points to a memory file */
SND_LOOP ? ? ? ?= 0x0008 , /* loop the sound until next sndPlaySound */
SND_NOSTOP ? ? ? ?= 0x0010 , /* don't stop any currently playing sound */
SND_NOWAIT ? ? ? ?= 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS ? ? ? ?= 0x00010000 ,/* name is a registry alias */
SND_ALIAS_ID ? ? ? ?= 0x00110000, /* alias is a pre d ID */
SND_FILENAME ? ? ? ?= 0x00020000, /* name is file name */
SND_RESOURCE ? ? ? ?= 0x00040004, /* name is resource name or atom */
SND_PURGE ? ? ? ?= 0x0040, ?/* purge non-static events for task */
SND_APPLICATION ? ? ? ?= 0x0080 ?/* look for application specific association */
}

/// <summary>
/// Wave 파일 재생
/// </summary>
/// <param name="pszSound">파일 경로</param>
public static void PlaySound(String pszSound)
{
if(File.Exists(pszSound))
{
PlaySound(pszSound,0,(int) (SND.SND_ASYNC | SND.SND_FILENAME |
SND.SND_NOWAIT));
}
}

/// <summary>
/// 시스템 사운드 이벤트 HKEY_CURRENT_USERAppEventsSchemesApps.Default
/// </summary>
/// <param name="pszSound">이벤트 이름</param>
public static void PlaySoundEvent(String pszSound)
{
PlaySound(pszSound,0,(int) (SND.SND_ASYNC | SND.SND_ALIAS | SND.SND_NOWAIT));
}
}
}