您的位置:首页 > 编程语言 > VB

VB实现给控件添加系统声音

2012-04-22 10:32 267 查看
作品展中组员做了一个小游戏,功能都实现了但还有很多方面要维护,其中添加声音就是改进的一方面。通过上网搜集找到了些代码,很简单也很有意思,和大家分享一下。

(1)添加“咚”的声音

在窗体上添加一个command按钮

private sub command1_click()

beep

end sub

(2)其他的系统声音

使用API: MessageBeep

声明: Declare Function MessageBeep Lib "user32" Alias "MessageBeep" (ByVal wType As Long) As Long

把下面的东西复制到一个新的空白的form中,不要任何东西,不要有代码、控件。

Private Const MB_ICONASTERISK = &H40&

Private Const MB_ICONEXCLAMATION = &H30&

Private Const MB_ICONHAND = &H10&

Private Const MB_ICONINFORMATION = MB_ICONASTERISK

Private Const MB_OK = &H0&

Private Declare Function MessageBeep Lib "user32" (ByVal wType As Long) As Long

Dim WithEvents Command1 As CommandButton

Dim WithEvents Command2 As CommandButton

Dim WithEvents Command3 As CommandButton

Dim WithEvents Command4 As CommandButton

Dim WithEvents Command5 As CommandButton

Dim WithEvents Command6 As CommandButton

Private Sub Command1_Click()

MessageBeep 0

End Sub

Private Sub Command2_Click()

MessageBeep MB_ICONASTERISK

End Sub

Private Sub Command3_Click()

MessageBeep MB_ICONEXCLAMATION

End Sub

Private Sub Command4_Click()

MessageBeep MB_ICONHAND

End Sub

Private Sub Command5_Click()

MessageBeep MB_ICONINFORMATION

End Sub

Private Sub Command6_Click()

MessageBeep MB_OK

End Sub

Private Sub Form_Load()

MessageBeep 0

Set Command1 = Me.Controls.Add("VB.CommandButton", "Command1")

Command1.Visible = True

Command1.Caption = "0"

Command1.Left = 120

Command1.Top = 240

Command1.Height = 255

Command1.Width = 1215

Set Command2 = Me.Controls.Add("VB.CommandButton", "Command2")

Command2.Visible = True

Command2.Caption = "MB_ICONASTERISK"

Command2.Left = 120

Command2.Top = 600

Command2.Height = 255

Command2.Width = 1935

Set Command3 = Me.Controls.Add("VB.CommandButton", "Command3")

Command3.Visible = True

Command3.Caption = "MB_ICONEXCLAMATION"

Command3.Left = 120

Command3.Top = 960

Command3.Height = 255

Command3.Width = 1935

Set Command4 = Me.Controls.Add("VB.CommandButton", "Command4")

Command4.Visible = True

Command4.Caption = "MB_ICONHAND"

Command4.Left = 120

Command4.Top = 1320

Command4.Height = 255

Command4.Width = 1935

Set Command5 = Me.Controls.Add("VB.CommandButton", "Command5")

Command5.Visible = True

Command5.Caption = "MB_ICONINFORMATION"

Command5.Left = 120

Command5.Top = 1680

Command5.Height = 255

Command5.Width = 1935

Set Command6 = Me.Controls.Add("VB.CommandButton", "Command6")

Command6.Visible = True

Command6.Caption = "MB_OK"

Command6.Left = 120

Command6.Top = 2040

Command6.Height = 255

Command6.Width = 1935

End Sub
运行时出现六个按钮 点击就会出现声音
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: