Condividi tramite


Passaggio 7: aggiungere codice al form per riprodurre suoni

Si è ora pronti ad aggiungere un secondo controllo SoundPlayer, quindi ad aggiungere un metodo per chiamare i singoli controlli SoundPlayer.

Per riprodurre suoni

  1. Iniziare aggiungendo un secondo controllo SoundPlayer per riprodurre il suono Tada di Windows.Il gioco riprodurrà questo suono quando il giocatore raggiunge l'etichetta Fine.

    Public Class Form1
    
        ' This SoundPlayer plays a sound whenever the player hits a wall.
        Dim startSoundPlayer = New System.Media.SoundPlayer("C:\Windows\Media\chord.wav")
    
        ' This SoundPlayer plays a sound when the player finishes the game.
        Dim finishSoundPlayer = New System.Media.SoundPlayer("C:\Windows\Media\tada.wav")
    
        Public Sub New()
            ' This call is required by Windows Forms Designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            MoveToStart()
        End Sub
    
    public partial class Form1 : Form
    {
        // This SoundPlayer plays a sound whenever the player hits a wall.
        System.Media.SoundPlayer startSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\chord.wav");
    
        // This SoundPlayer plays a sound when the player finishes the game.
        System.Media.SoundPlayer finishSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\tada.wav");
    
        public Form1()
        {
            InitializeComponent();
            MoveToStart();
        }
    
  2. Entrambi i controlli SoundPlayer sono stati ora aggiunti al form.Aggiungere un metodo Play() per chiamare SoundPlayer per riprodurre il suono al momento giusto.Si desidera che venga riprodotto un suono quando l'utente colpisce un muro.Aggiungere quindi l'istruzione startSoundPlayer.Play(); al metodo MoveToStart().Ricordarsi di aggiornare il commento.L'aspetto finale del metodo sarà simile al seguente.

    ''' <summary>
    ''' Play a sound, then move the mouse pointer to a point 10 pixels down and to 
    ''' the right of the starting point in the upper-left corner of the maze.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub MoveToStart()
        startSoundPlayer.Play()
        Dim startingPoint = Panel1.Location
        startingPoint.Offset(10, 10)
        Cursor.Position = PointToScreen(startingPoint)
    End Sub
    
    /// <summary>
    /// Play a sound, then move the mouse pointer to a point 10 pixels down and to 
    /// the right of the starting point in the upper-left corner of the maze.
    /// </summary>
    private void MoveToStart()
    {
        startSoundPlayer.Play();
        Point startingPoint = panel1.Location;
        startingPoint.Offset(10, 10);
        Cursor.Position = PointToScreen(startingPoint);
    }
    
  3. Aggiungere l'istruzione finishSoundPlayer.Play(); al gestore dell'evento MouseEnter dell'etichetta Fine.Ricordarsi di aggiornare il commento, perché si modifica il codice, nel modo seguente.

    Private Sub finishLabel_MouseEnter() Handles finishLabel.MouseEnter
        ' Play a sound, show a congratulatory MessageBox, then close the form.
        finishSoundPlayer.Play()
        MessageBox.Show("Congratulations!")
        Close()
    End Sub
    
    private void finishLabel_MouseEnter(object sender, EventArgs e)
    {
        // Play a sound, show a congratulatory MessageBox, then close the form.
        finishSoundPlayer.Play();
        MessageBox.Show("Congratulations!");
        Close();
    }
    

Per continuare o rivedere l'esercitazione