用C#第2部分创作钢琴

时间:2017-12-26 11:42:14

标签: c# piano

关于我之前发布的问题,我正在创建一个c#piano,它将显示一个键盘,当点击音乐键时,它将显示音符形状并输出音乐声。但是,我遇到了另一个问题。当我单击音乐键时,它会输出所需的图像,但是当我再次以另一个持续时间点击另一个时,相同的图像保持不变。有人可以帮我这个吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace NewPiano
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SoundPlayer sp = new SoundPlayer();
        long count;
        private Control mn;
        Stopwatch watch = new Stopwatch();

        private void Form1_Load(object sender, System.EventArgs e)
        {

            MusKey mk;
            BlackMusKey bmk;

            int[] whitePitch = { 1, 3, 5, 6, 8, 10, 12, 13, 15, 17, 18, 20, 22, 24 };
            int[] blackPitch = new int[] { 2, 4, 7, 9, 11, 14, 16, 19, 21, 23 };
            int[] xPos = new int[] { 10, 30, 70, 90, 110, 150, 170, 210, 230, 250 };

            for (int k = 0; k < 7; k++)
            {
                int iNote = whitePitch[k];
                int xpos = k * 40;
                mk = new MusKey(iNote, xpos + 150, 120);
                mk.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
                mk.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
                this.panel1.Controls.Add(mk);
            }

            int xOffs = 20;

            for (int k = 0; k < 5; k++)
            {
                int iNote = blackPitch[k];
                int xpos = xPos[k] * 2;
                bmk = new BlackMusKey(iNote, xpos + 150, 120);
                bmk.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
                bmk.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
                this.panel1.Controls.Add(bmk);
                this.panel1.Controls[this.panel1.Controls.Count - 1].BringToFront();
            }

        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            watch.Reset();

            foreach (MusKey mk in panel1.Controls.OfType<MusKey>())
            {
                if (sender == mk)
                {
                    if (e.Button == MouseButtons.Left)
                    {

                        watch.Start();
                        count = 0;
                        sp.SoundLocation = @"C:/Users/Kim/Desktop/Notes-Sound files/mapped/" + mk.musicNote + ".wav"; //change this to the location of your "mapped" folder

                        sp.Load();
                        sp.Play();
                    }
                }
            }

        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            foreach (MusKey mk in panel1.Controls.OfType<MusKey>())
            {
                if (sender == mk)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        watch.Stop();
                        count = watch.ElapsedMilliseconds;
                    }

                    sp.Stop();

                    int duration = 0;
                    string bNoteShape = null;
                    int pitch = 0;

                    if (count >=2024)
                    {
                        bNoteShape = "SemiBreve";

                        duration = (16 + 20) / 2;

                        pitch = 1;
                    }

                    else if ((count >= 1024) && (count < 2024))
                    {
                        bNoteShape = "DotMinim";

                        duration = (11 + 15) / 2; //average

                        pitch = 2;

                    }
                    else if ((count >= 768) && (count < 1024))
                    {
                        bNoteShape = "minim";

                        duration = (6 + 10) / 2; //average

                        pitch = 3;
                    }
                    else if ((count >= 512) && (count < 768))
                    {

                        bNoteShape = "Crotchet";
                        duration = (3 + 5) / 2; //average
                        pitch = 4;
                    }
                    else if ((count >= 256 && count < 512))
                    {
                        bNoteShape = "Quaver";

                        duration = 2; //average
                        pitch = 5;
                    }
                    else if ((count == 0 || count < 256))
                    {
                        bNoteShape = "SemiQuaver";
                        duration = 1;
                        pitch = 6;
                    }


                    MusicNote mn = new MusicNote(count, pitch, bNoteShape, duration);
                    panel1.Controls.Add(mn.drawNote());
                    watch.Restart();



                }
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Graphics graphicsObj;

            graphicsObj = this.panel1.CreateGraphics();

            Pen myPen = new Pen(System.Drawing.Color.Black, 1);

            for (int k = 0; k < 5; k++)
            {
                int ypos = (k * 15) + 20;
                graphicsObj.DrawLine(myPen, 20, ypos, 550, ypos);
            }
        }

}
}

音乐笔类:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewPiano
{
    class MusicNote
    {

        public long noteID;
        public int pitch = 0;
        public string shape = "";
        public int duration = 0;

        public MusicNote() { }

        public MusicNote(long count, int mpitch, string shape, int duration)
        {
            this.noteID = count;
            this.shape = shape;
            this.duration = duration;
            this.pitch = mpitch;
        }

        public PictureBox drawNote()
        {
            PictureBox noteDrawing = new PictureBox();
            noteDrawing.Size = new System.Drawing.Size(40, 40);
            noteDrawing.Location = new System.Drawing.Point(10, 10);

            noteDrawing.ImageLocation = @"C:/Users/Kim/Desktop/Notes-Images/" + shape + ".bmp";

            return noteDrawing;
        }

        /*public void PlayAllNotesOnClick()
        {
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.SoundLocation = "C:/Users/Kim/Desktop/Notes-Sound files/mapped/" + this.pitch + ".wav";

            player.Play();
            System.Threading.Thread.Sleep(duration + 100);
            player.Stop();
        }
        */

        public void onDrag(int pitch)
        {
            if (pitch > 150 && pitch < 155)
            {
                this.pitch = 1;
            }
            if (pitch > 100 && pitch < 154)
            {
                this.pitch = 2;
            }
            if (pitch > 75 && pitch < 99)
            {
                this.pitch = 5;
            }
            if (pitch > 50 && pitch < 74)
            {
                this.pitch = 6;
            }
            //this.pitch = pitch;
        }

    }
}

1 个答案:

答案 0 :(得分:1)

每次您的MouseUp事件触发时,您都会向Panel1.Controls集合中添加一个新的PictureBox。显示第一个,但后面的所有都不显示。要么它们不适合Panel1,要么它们位于第一个下面。

请尝试以下选项:

  1. 每次在Panel1中添加一个新的PictureNote图片框时, 删除现有的。
  2. 不是创建一个全新的PictureBox,而是重用现有的 一个只是改变它的画面路径。