خطوة 7: اضف تعليمات برمجية خاص بك الى النموذج لتشغيل الأصوات الخاصة بك

أنت الآن جاهز لإضافة SoundPlayer ثاني ثم قم بإضافة الأسلوب الذي تريده ان يتصل بكل SoundPlayer.

قراءة الأصوات

  1. يبدأ عن طريق إضافة SoundPlayer ثاني لتشغيل صوت Tada Windows. سيتم تشغيل الGame لهذا الصوت عند بلوغ اللاعب التسمية إنهاء.

    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. يتم الآن تمت الإضافة كلا SoundPlayers إلى النموذج الخاص بك. إضافة **Play()**أسلوب إلى يتصل على SoundPlayer إلى تشغيل الصوت في الوقت المناسب. تريد صوت إلى تشغيل عند جدار يتجه المستخدم. لذا قم بإضافة كشف startSoundPlayer.Play();إلى الخاص بك **MoveToStart()**الأسلوب. تذكر إلى تحديث التعليق. يبحث أسلوب النهائية كما يلي.

    ''' <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. إضافة كشف finishSoundPlayer.Play();إلى إنهاء التسمية MouseEnter معالج الأحداث. تذكر إلى تحديث التعليق حيث تقوم بتغيير التعليمة البرمجية كما يلي.

    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();
    }
    

للمتابعة أو للمراجعة