日期:2014-05-18  浏览次数:20814 次

在线等C#windows程序设计
C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Maze
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            //This SoundPlayer plays a sound whenever the player hits a wall.
            System.Media.SoundPlayer startSoundPlayer = new System.Media.SoundPlayer(@"E:\KwDownload\song\Jimi Hendrix-Voodoo Chile Blues.mp3");
            
            // This SoundPlayer plays a sound when the player finishes the game.
            System.Media.SoundPlayer finishSoundPlayer = new System.Media.SoundPlayer(@"E:\KwDownload\song\Jimi Hendrix-Red House.mp3");
            
            InitializeComponent();
            MoveToStart();
        }

        private void finishLabel_MouseEnter(object sender, EventArgs e)
        {
            //Show a congratulatory Messagebox,then close the form
            
            MessageBox.Show("Congratulations!");
            Close();
        }

        /// <summary>
        /// Move the pointer to a point 10 pixels down and to the right
        /// of the starting point in the upper-left corner of the maze.
        /// 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()
        {
          [color=#FF0000]  startSoundPlayer.Play();[/color]
            Point startingPoint = panel1.Location;
            startingPoint.Offset(10,10);
            Cursor.Position = PointToScreen(startingPoint);
        }

        private void wall_MouseEnter(object sender, EventArgs e)
        {
            // When the mouse pointer hits a wall or enters the panel,
            // call the MoveToStart() method.

            MoveToStart();
        }

       
    }
}



startSoundPlayer.Play();显示上下文不存在,如何解决

------解决方案--------------------
startSoundPlayer是构造函数中的局部变量,你MoveToStart方法中使用不了
把startSoundPlayer放到全局
------解决方案--------------------
把定义移到form1()上面。定义成全局的