2017-12-26 17 views
-2

내가 게시 한 이전 질문과 관련하여 음악 키를 클릭하면 음악 노트 모양이 표시되고 음악 사운드가 출력되는 키보드가 표시되는 C# 피아노를 만들고 있습니다. 그러나 또 다른 문제가 발생했습니다. 음악 키를 클릭하면 필요한 이미지가 출력되지만, 다른 이미지를 다시 클릭하면 동일한 이미지가 유지되고 변경되지 않습니다. 누구든지 이걸 좀 도와 주실 래요?C# 파트 2로 피아노 만들기

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

때마다 당신의 MouseUp 이벤트가 발생합니다, 당신은 당신의 Panel1.Controls 컬렉션에 새로운 PictureBox를 추가하고 있습니다. 첫 번째 메시지가 표시되지만 이후의 메시지는 표시되지 않습니다. 그들은 Panel1 내부에 들어 가지 않거나 첫 번째 패널 아래로갑니다.

시도 다음 옵션을

  1. 당신이에 Panel1 내부 MusicNote의 새로운 그림 상자를 추가 할 때마다, 기존 하나를 제거합니다.
  2. 완전히 새로운 PictureBox를 만드는 대신 기존 을 재사용하고 그림 경로 만 변경하십시오.