我正在做一個鋼琴鍵盤,它也有一個音樂工作人員,因此,當你按下鍵盤上的按鍵,根據持續時間音樂筆記(crotchet ,半等等)彈出在音樂人員。picturebox控制不工作C#.NET(Visual Studio)
我有一個窗體(稱爲Form1),其中有一個面板(Panel1),其中包含由按鈕組成的鍵盤按鍵。我也有一個音樂工作人員製作的一個PictureBox的持有工作人員(或梯級不知道它叫什麼)和一個測試文本框,其中包含音高(基本上是他們被映射的音符)
問題這是。我需要做的音符顯示在Form1中被作爲主類的工作人員(pictureBox1),所以,但每當我寫
mn = new MusicNote (nPos,nPitch, duration,nShape);
pictureBox1.Controls.Add(this.mn);
pictureBox1.Controls[pictureBox1.Controls.Count - 1].BringToFront(); //Bring the notes OVER the Stave
它基本上不工作,但是當我更換每pictureBox1與只是這個 OR PANEL1(例如)
this.Controls.Add(this.mn);
它顯示的音符無論是在FORM1(灰色空間)或鍵盤上的本身(見進一步往下看鍵盤)。問題是,與此,它實際上並沒有增加到PictureBox,但對Form1 /面板
你有什麼想法如何解決這個問題,並使音樂筆記實際上是PictureBox1的一部分?因爲我還需要一些方法來處理那個pictureBox,就像當我點擊其中一個Notes時,他們實際上會用那個持續時間播放那個聲音(我仍然需要弄清楚如何將控件從Form1.cs傳遞到MusicNote.cs)編碼爲MusicNote.cs其具有與做
部分中的「加入」的圖像是這樣的下面:
public class MusicNote : PictureBox
{
public int pitch;
public int duration;
public string noteShape;
string nShape;
bool isDragging = false;
public string rootFolder = @"..\..\\bin\\Debug\\images\\";
ArrayList al = new ArrayList();
SoundPlayer sp = new SoundPlayer();
Timer t1 = new Timer();
public MusicNote(int x, int mPitch, int mDuration,string nShape)
: base()
{
pitch = mPitch;
duration = mDuration;
noteShape = nShape;
Location = new Point(x, 100);
this.Size = new Size(35, 40);
//--------- Get the Image of Note ----------
Bitmap bmp = new Bitmap(@rootFolder + nShape + ".bmp");
this.Image = bmp;
this.BackColor = Color.Transparent;
//-------Mouse Events-------
this.MouseDown += new MouseEventHandler(StartDrag);
this.MouseUp += new MouseEventHandler(StopDrag);
this.MouseMove += new MouseEventHandler(NoteDrag);
}
etc
...
...
...
}
Form1.cs的編碼(張貼所有所有從一個方法連接到另一個) 問題在於private void onMouseUp(object sender,MouseEventArgs e)這是最後一種方法
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.Collections;
namespace Piano
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Objects of Music Note, Piano Keys, and all variables surronding the Keyboard and Notes
WhiteKey wk;
BlackKey bk;
public int pitch;
public int duration = 0;
string nShape = "";
public const int xOff = 35;
int count = 0;
int nPos = 0;
public string rootFolder = @"..\..\\bin\\Debug\\images\\";
MusicNote mn;
MusicStaff ms;
SoundPlayer sp = new SoundPlayer();
Stopwatch sw = new Stopwatch();
//--------------- White and Black Keys Creation both Buttons and Position------------------
public int[] wKeys = { 1, 3, 5, 6, 8, 10, 12, 13, 15, 17, 18, 20, 22, 24, 25 }; //White Keys notes numbers (pitch)
public int[] bKeys = { 0, 2, 0, 4, 0, 0, 7, 0, 9, 0, 11, 0, 0, 14, 0, 16, 0, 0, 19, 0, 21, 0, 23, 0, 0 }; //Black Keys notes numbers (pitch)
int[] wPos = { 35, 70, 105, 140, 175, 210, 245, 280, 315, 350, 385, 420, 455, 490, 525 }; // Position of White Keys on the Panel
int[] bPos = { 0, 57, 0, 92, 0, 0, 162, 0, 197, 0, 232, 0, 0, 302, 0, 337, 0, 0, 407, 0, 442, 0, 477, 0, 1 }; //Position of the Black Keys in the Panel
private void Form1_Load(object sender, System.EventArgs e)
{
for (int i = 0; i < 15; i++)
{
WhiteKey wk = new WhiteKey(wKeys[i], wPos[i]-35,0); //create a new white Key with [i] Pitch, at that x position and at y =0 position
wk.MouseDown += onMouseDown; //Plays the Key and starts Timer
wk.MouseUp += onMouseUp; // Holds the data like Time and shape and so
this.panel1.Controls.Add(wk); //Give it control (to play and edit)
}
for (int i = 0; i < 25; i++) //same for the Black Keys but instead we use 25 keys and those denoted at 0 are where the WHITE KEYS should be placed
{
if (bKeys[i] != 0)
{
//Same as above but for Black Key which is inherits from WhiteKey
bk = new BlackKey(bKeys[i], bPos[i]-35, 0);
bk.MouseDown += onMouseDown;
bk.MouseUp += onMouseUp;
this.panel1.Controls.Add(bk);
this.panel1.Controls[this.panel1.Controls.Count - 1].BringToFront(); //Make the Black Keys show OVER the white
}
}
}
//Method showing what happens when you do a MouseDown Event
private void onMouseDown(object sender, MouseEventArgs e)
{
wk = sender as WhiteKey; //gets the WhiteKey Controls
pitch = wk.pitch; //assign pitch
sp.SoundLocation = @"..\\..\\bin\\Debug\\sound\\mapped\\" + pitch + ".wav"; //find that pressed note
sp.Play(); //play it
sw.Reset(); //Reset Stop Watch
sw.Start(); //Start Time
}
private void timeTick(object sender, EventArgs e)
{
duration++;
}
private void onMouseUp (object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
sw.Stop(); //Stop time
duration = (int)sw.ElapsedMilliseconds/100;
textBox.Text += (string)(pitch + " , "); //add the Pitch used to the textbox
// Will determine the Music Note Image
if (duration >= 0 && duration < 2)
{
nShape = "Quaver";
}
else if (duration >= 2 && duration < 5)
{
nShape = "Crotchet";
}
else if (duration >= 5 && duration < 8)
{
nShape = "minim";
}
else if (duration >= 8 && duration < 10)
{
nShape = "DotMin";
}
else if (duration >= 10)
{
nShape = "SemiBreve";
}
count++; //will help Determine the 'x' coordiante of the Music Note
nPos = xOff * count; //moves the x-coordinate by 35 pixels each note
mn = new MusicNote(nPos,pitch,duration,nShape); //Creation of a new MusicNote
pictureBox1.Controls.Add(this.mn); //PROBLEM --- Doesn't add to the PictureBox (Does Nothing)
pictureBox1.Controls[pictureBox1.Controls.Count - 1].BringToFront(); //Brought to front of stave to make sure it doesn't get hidden in background
}
}
}
}
任何想法如何將CONTROL添加到PictureBox1並使音樂筆記顯示?因爲我設法使它顯示在Form1上和Panel1的,但未能上一個PictureBox
鋼琴如何用音符看起來在Form1上
根據Visual Studio調試器[鋼琴.Form1]
http://postimg.org/image/vdu0x1gv1/
NB對不起,長職位,但我不知道究竟該如何解釋這個問題。