picturebox控件在C#.NET(Visual Studio)中不工作
我正在做一个钢琴键盘,它也有一个音乐棒,这样当你按下键盘上的一个键时,音乐棒上会根据持续时间弹出一个音符(交叉、半短音符等) 我有一个表单(称为Form1),其中有一个面板(Panel1),其中包含由按钮组成的键盘键。我还有一个音乐棒,它由一个装着音乐棒的图片盒和一个装着音高的测试文本框组成(基本上是在映射时播放的音符) 问题是这个。我需要在教职员(pictureBox1)上显示音乐笔记,所以在Form1中,它作为主课堂,但每当我写作时picturebox控件在C#.NET(Visual Studio)中不工作,c#,winforms,visual-studio-2012,controls,picturebox,C#,Winforms,Visual Studio 2012,Controls,Picturebox,我正在做一个钢琴键盘,它也有一个音乐棒,这样当你按下键盘上的一个键时,音乐棒上会根据持续时间弹出一个音符(交叉、半短音符等) 我有一个表单(称为Form1),其中有一个面板(Panel1),其中包含由按钮组成的键盘键。我还有一个音乐棒,它由一个装着音乐棒的图片盒和一个装着音高的测试文本框组成(基本上是在映射时播放的音符) 问题是这个。我需要在教职员(pictureBox1)上显示音乐笔记,所以在Form1中,它作为主课堂,但每当我写作时 mn = new MusicNote (nPos,nPit
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
它基本上不起作用,但当我将每个图片box1替换为这个或面板1(例如)
它在FORM1(灰色空间)或键盘上显示音符(请参阅下面的部分以查看键盘)。问题是,这样做实际上并没有添加到PictureBox,而是添加到Form1/面板
你知道如何解决这个问题并使音符成为PictureBox1的一部分吗?因为我还需要一些方法来处理这个pictureBox,比如当我点击其中一个音符时,它们实际上会播放那个持续时间的声音(在这个过程中,我仍然需要弄清楚如何将控件从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编码(在所有方法从一种方法连接到另一种方法时过账)
问题在于mouseup上的私有void(objectsender,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
}
}
}
}
使用系统;
使用系统线程;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用系统诊断;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统、媒体;
使用系统集合;
名称空间钢琴
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
//音符、钢琴键以及围绕键盘和音符的所有变量的对象
怀特基作品;
布莱克基bk;
公众国际音高;
公共int持续时间=0;
字符串nShape=“”;
公共常数int xOff=35;
整数计数=0;
int nPos=0;
公共字符串rootFolder=@“.\..\\bin\\Debug\\images\\”;
MusicNote mn;
MusicStaff ms;
SoundPlayer sp=新的SoundPlayer();
秒表sw=新秒表();
//---------------黑白键创建按钮和位置------------------
public int[]wKeys={1,3,5,6,8,10,12,13,15,17,18,20,22,24,25};//白键注释数字(音高)
public int[]bKeys={0,2,0,4,0,0,0,7,0,9,0,11,0,0,0,14,0,16,0,0,0,19,0,21,0,23,0,0};//黑键注释数字(音高)
int[]wPos={35,70,105,140,175,210,245,280,315,350,385,420,455,490,525};//面板上白键的位置
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};//面板中黑键的位置
私有void Form1_加载(对象发送方,System.EventArgs e)
{
对于(int i=0;i<15;i++)
{
白键wk=新白键(wKeys[i],wPos[i]-35,0);//在x位置和y=0位置创建一个具有[i]音高的新白键
wk.MouseDown+=onMouseDown;//播放键并启动计时器
wk.MouseUp+=onMouseUp;//保存时间和形状等数据
this.panel1.Controls.Add(wk);//赋予它控制权(播放和编辑)
}
对于(int i=0;i<25;i++)//黑色键也是一样,但是我们使用25个键,而那些表示为0的键是白色键应该放置的位置
{
如果(b键[i]!=0)
{
//同上,但适用于从WhiteKey继承的黑键
bk=新的黑键(bKeys[i],bPos[i]-35,0);
bk.MouseDown+=onMouseDown;
bk.MouseUp+=onMouseUp;
此.panel1.Controls.Add(bk);
this.panel1.Controls[this.panel1.Controls.Count-1].BringToFront();//使黑键显示在白键上
}
}
}
//方法,显示执行MouseDown事件时发生的情况
MouseDown上的私有void(对象发送器,MouseEventArgs e)
{
wk=sender as WhiteKey;//获取WhiteKey控件
音高=wk.pitch;//指定音高
sp.SoundLocation=@.\\..\\bin\\Debug\\sound\\mapped\\“+pitch+”.wav;//查找按下的音符
sp.Play();//播放它
sw.Reset();//重置秒表
sw.Start();//开始时间
}
私有void timeTick(对象发送方,事件参数e)
{
持续时间++;
}
MouseUp上的私有void(对象发送器、MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
sw.Stop();//停止时间
持续时间=(int)sw.ElapsedMilliseconds/100;
textBox.Text+=(字符串)(音高+“,”;//添加用于文本框的音高
//将确定音符图像
if(持续时间>=0&&duration<2)
{
nShape=“Quaver”;
}
否则如果(持续时间>=2和持续时间<5)
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
}
}
}
}
[DllImport("winmm.dll")]
private static extern bool PlaySound(string lpszName, int hModule, int dwFlags);