Home » Windows API

Beep

說明:
Beep 函式可透過PC喇叭發出指定的聲音


引用函式庫:
Kernel32


適用系統:
Windows NT 3.1 或 Windows 95以上


函式原型:

Declare Function Beep Lib "kernel32" Alias "Beep" ( _
	ByVal dwFreq As Long, _
    ByVal dwDuration As Long _
    ) As Long



參數型態及說明:
dwFreqLong 指定聲音的頻率,參數值介於37~32767(0x25~0x7FFF)
dwDurationLong 聲音持續的時間,單位為毫秒

回傳值:
Long 呼叫成功的話,將會回傳非零的數值;若呼叫失敗的話,則回傳 0

.NET Framework API:


VB範例:

檢視原始碼 Visual Basic
1
2
3
4
5
6
7
8
9
10
'VB的宣告
Private Declare Function Beep Lib "kernel32" ( _
	ByVal dwFreq As Long, _
	ByVal dwDuration As Long _
	) As Long
 
Dim rtvl As Long
 
'發出聲音,持續100毫秒
rtvl = Beep(1000, 100)

RB範例:

檢視原始碼 REALBasic
1
2
3
4
5
6
7
8
9
10
'RB的宣告
Declare Function Beep Lib "kernel32" ( _
	dwFreq As Integer, _
	dwDuration As Integer _
	) As Boolean
 
Dim rtvl As Boolean
 
'發出聲音,持續100毫秒
rtvl = Beep(1000, 100)

VB.NET範例:

1
2
3
4
5
6
7
8
9
10
11
12
'VB.NET的宣告
<DllImport("kernel32.dll")> _
Shared Function Beep( _
	ByVal dwFreq As UInteger, _
	ByVal dwDuration As UInteger _
	) As Boolean
End Function
 
Dim rtvl As Boolean
 
'發出聲音,持續100毫秒
rtvl = Beep(1000, 100)

C#範例:

1
2
3
4
5
6
7
8
9
10
11
//C#的宣告
[DllImport("kernel32")]
public static extern bool Beep(
	uint dwFreq, 
	uint dwDuration
	);
 
bool rtvl;
 
//發出聲音,持續100毫秒
rtvl = Beep(1000, 100);

註釋:
在Windows Me/98/95中會忽略Beep 函式中的兩個參數,因此聲音的頻率都不會變。另外,若有興趣的話,可用Beep 函式來彈出音樂喔!

發表迴響