Audiobutton

Soyez le premier à donner votre avis sur cette source.

Vue 7 675 fois - Téléchargée 427 fois

Description

C'est un contrôle qui hérite de la classe ButtonBase. Je lui ai rajouté la possibilité de jouer un fichier wav sur les événements de la souris : Enter, Leave, Click, MouseUp, MouseDown

Source / Exemple :


Imports System.Drawing.Drawing2D
Public Class AudioBtn
    Inherits System.Windows.Forms.Button

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        'We add the different handlers
        AddHandler Me.MouseEnter, AddressOf PlayMouseEnterSound
        AddHandler Me.MouseLeave, AddressOf PlayMouseLeaveSound
        AddHandler Me.Click, AddressOf PlayMouseClickSound
        AddHandler Me.MouseDown, AddressOf PlayMouseDownSound
        AddHandler Me.MouseUp, AddressOf PlayMouseUpSound

    End Sub

    'UserControl1 overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    'Friend WithEvents btnMain As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'AudioBtn
        '
        Me.Name = "AudioBtn"
        Me.Size = New System.Drawing.Size(296, 120)
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private mEnterWaveFile As String
    Private mLeaveWaveFile As String
    Private mClickWaveFile As String
    Private mDownWaveFile As String
    Private mUpWaveFile As String
    Private mSndPlayer As New SoundPlayer

    Public Property MouseEnterWaveFile() As String
        Get
            Return mEnterWaveFile
        End Get
        Set(ByVal Value As String)
            If System.IO.File.Exists(Value) = True Then
                mEnterWaveFile = Value.Trim
            End If
        End Set
    End Property

    Public Property MouseLeaveWaveFile() As String
        Get
            Return mLeaveWaveFile
        End Get
        Set(ByVal Value As String)
            If System.IO.File.Exists(Value) = True Then
                mLeaveWaveFile = Value.Trim
            End If
        End Set
    End Property

    Public Property MouseClickWaveFile() As String
        Get
            Return mClickWaveFile
        End Get
        Set(ByVal Value As String)
            If System.IO.File.Exists(Value) = True Then
                mClickWaveFile = Value.Trim
            End If
        End Set
    End Property

    Public Property MouseDownWaveFile() As String
        Get
            Return mDownWaveFile
        End Get
        Set(ByVal Value As String)
            If System.IO.File.Exists(Value) = True Then
                mDownWaveFile = Value.Trim
            End If
        End Set
    End Property

    Public Property MouseUpWaveFile() As String
        Get
            Return mUpWaveFile
        End Get
        Set(ByVal Value As String)
            If System.IO.File.Exists(Value) = True Then
                mUpWaveFile = Value.Trim
            End If
        End Set
    End Property

    Private Sub PlayMouseEnterSound(ByVal Sender As Object, ByVal e As EventArgs)
        If mEnterWaveFile <> "" Then
            Try
                mSndPlayer.Play(mEnterWaveFile)
            Catch PlayErr As Exception
                'We do nothing
            End Try
#If DEBUG Then
            Debug.Write("Enter")
#End If
        End If
    End Sub

    Private Sub PlayMouseLeaveSound(ByVal Sender As Object, ByVal e As EventArgs)
        If mLeaveWaveFile <> "" Then
            Try
                mSndPlayer.Play(MouseLeaveWaveFile)
            Catch PlayErr As Exception
                'We do nothing
            End Try
#If DEBUG Then
            Debug.Write("Leave")
#End If
        End If
    End Sub

    Private Sub PlayMouseClickSound(ByVal Sender As Object, ByVal e As EventArgs)
        If mClickWaveFile <> "" Then
            Try
                mSndPlayer.Play(mClickWaveFile)
            Catch PlayErr As Exception
                'We do nothing
            End Try
#If DEBUG Then
            Debug.Write("Click")
#End If
        End If
    End Sub

    Private Sub PlayMouseDownSound(ByVal Sender As Object, ByVal e As MouseEventArgs)
        If mClickWaveFile <> "" Then
            Try
                mSndPlayer.Play(mDownWaveFile)
            Catch PlayErr As Exception
                'We do nothing
            End Try
#If DEBUG Then
            Debug.Write("Down")
#End If
        End If
    End Sub

    Private Sub PlayMouseUpSound(ByVal Sender As Object, ByVal e As MouseEventArgs)
        If mClickWaveFile <> "" Then
            Try
                mSndPlayer.Play(mUpWaveFile)
            Catch PlayErr As Exception
                'We do nothing
            End Try
#If DEBUG Then
            Debug.Write("Up")
#End If
        End If
    End Sub

    Private Class SoundPlayer
        Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name As String, ByVal hmod As Integer, ByVal flags As Integer) As Integer
        ' name specifies the sound file when the SND_FILENAME flag is set.
        ' hmod specifies an executable file handle.
        ' hmod must be Nothing if the SND_RESOURCE flag is not set.
        ' flags specifies which flags are set. 

        ' The PlaySound documentation lists all valid flags.
        Private Enum SndModeEnum
            SND_SYNC = &H0
            SND_ASYNC = &H1
            SND_FILENAME = &H20000
            SND_RESOURCE = &H40004
        End Enum

        Private Const SND_SYNC As Integer = &H0          ' play synchronously
        Private Const SND_ASYNC As Integer = &H1         ' play asynchronously
        Private Const SND_FILENAME As Integer = &H20000 ' name is file name
        Private Const SND_RESOURCE As Integer = &H40004 ' name is resource name or atom

        Public Sub Play(ByVal filename As String)
            ' Plays a sound from filename.
            PlaySound(filename, Nothing, SndModeEnum.SND_FILENAME Or SndModeEnum.SND_ASYNC)
        End Sub
    End Class

    Protected Overrides Sub Finalize()
        RemoveHandler Me.MouseEnter, AddressOf PlayMouseEnterSound
        RemoveHandler Me.MouseLeave, AddressOf PlayMouseLeaveSound
        RemoveHandler Me.Click, AddressOf PlayMouseClickSound
        RemoveHandler Me.MouseDown, AddressOf PlayMouseDownSound
        RemoveHandler Me.MouseUp, AddressOf PlayMouseUpSound

        MyBase.Finalize()
    End Sub

End Class

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.