⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 media.vb

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 VB
字号:
Imports System.ComponentModel
Imports System.io
Imports System.Runtime.InteropServices

Namespace Media

    Friend Class NativeMethods

        <DllImport("coredll.dll")> _
        Friend Shared Sub MessageBeep(ByVal type As MB)
        End Sub

        <DllImport("coredll.dll", EntryPoint:="PlaySoundW")> _
        Friend Shared Function PlaySound(ByVal lpszName As String, ByVal hModule As IntPtr, ByVal dwFlags As SND) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        Friend Enum MB
            ICONHAND = &H10
            ICONQUESTION = &H20
            ICONEXCLAMATION = &H30
            ICONASTERISK = &H40
        End Enum

        <Flags()> _
        Friend Enum SND
            SYNC = 0
            ASYNC = 1
            NODEFAULT = 2
            [LOOP] = 8
            NOSTOP = &H10
            NOWAIT = &H2000
            FILENAME = &H20000
        End Enum

    End Class

    Public NotInheritable Class SoundPlayer
        Inherits Component

        Private m_soundLocation As String

        Public Event SoundLocationChanged As EventHandler

        Public Sub New()
            m_soundLocation = ""
        End Sub

        Public Sub New(ByVal soundLocation As String)
            m_soundLocation = ""
            m_soundLocation = soundLocation
        End Sub

        Private Sub OnSoundLocationChanged(ByVal e As EventArgs)
            RaiseEvent SoundLocationChanged(Me, e)
        End Sub

        Public Sub Play()
            NativeMethods.PlaySound(Me.soundLocation, IntPtr.Zero, (NativeMethods.SND.FILENAME Or NativeMethods.SND.ASYNC))
        End Sub

        Public Sub PlayLooping()
            NativeMethods.PlaySound(Me.soundLocation, IntPtr.Zero, (NativeMethods.SND.FILENAME Or NativeMethods.SND.LOOP Or NativeMethods.SND.ASYNC))
        End Sub

        Public Sub PlaySync()
            NativeMethods.PlaySound(Me.soundLocation, IntPtr.Zero, NativeMethods.SND.FILENAME)
        End Sub

        Public Sub [Stop]()
            NativeMethods.PlaySound(Nothing, IntPtr.Zero, NativeMethods.SND.NODEFAULT)
        End Sub

        Public Property SoundLocation() As String
            Get
                Return m_soundLocation
            End Get
            Set(ByVal value As String)
                If File.Exists(value) Then
                    m_soundLocation = value
                End If
                RaiseEvent SoundLocationChanged(Me, EventArgs.Empty)
            End Set
        End Property

    End Class

    Public NotInheritable Class SystemSound

        Private soundType As NativeMethods.MB

        Friend Sub New(ByVal soundType As NativeMethods.MB)
            Me.soundType = soundType
        End Sub

        Public Sub Play()
            NativeMethods.MessageBeep(Me.soundType)
        End Sub

    End Class

    Public Class SystemSounds

        Public Shared ReadOnly Property Asterisk() As SystemSound
            Get
                Return New SystemSound(NativeMethods.MB.ICONASTERISK)
            End Get
        End Property

        Public Shared ReadOnly Property Beep() As SystemSound
            Get
                Return New SystemSound(DirectCast(0, NativeMethods.MB))
            End Get
        End Property

        Public Shared ReadOnly Property Exclamation() As SystemSound
            Get
                Return New SystemSound(NativeMethods.MB.ICONEXCLAMATION)
            End Get
        End Property

        Public Shared ReadOnly Property Hand() As SystemSound
            Get
                Return New SystemSound(NativeMethods.MB.ICONHAND)
            End Get
        End Property

        Public Shared ReadOnly Property Question() As SystemSound
            Get
                Return New SystemSound(NativeMethods.MB.ICONQUESTION)
            End Get
        End Property

    End Class

End Namespace

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -