繁体   English   中英

图片框控件无法正常使用C#.NET(Visual Studio)

[英]picturebox Control is not working C# .NET (Visual Studio)

我正在做一个钢琴键盘,它也带有一个Music Staff,这样,当您按键盘上的一个键时,根据音符的持续时间(钩针,半短音等)会弹出Music Staff。

我有一个窗体(称为Form1),其中有一个面板(Panel1),其中包含由按钮组成的键盘键。 我还有一个音乐谱夹,它由一个可容纳谱杆(或梯级不知道它叫什么)的PictureBox和一个可保存音高(基本上是映射音符时演奏的音符)的Test Text Box组成。

问题是这样的。 我需要在谱号(pictureBox1)上显示音符,以便在Form1中充当主类,但是每当我编写时

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

当我只用这个 OR panel1 (例如)替换每个pictureBox1时,它基本上不起作用

this.Controls.Add(this.mn); 

它会在FORM1(灰色区域)或键盘本身上显示“音符”(请参阅​​下方查看键盘)。 问题在于,它实际上并没有添加到PictureBox中,而是添加到了Form1 / Panel中

您是否知道如何解决此问题并使音乐笔记实际上成为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编码(将所有内容都从一种方法连接到另一种方法进行发布)问题出在私有无效的onMouseUp(对象发送者,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的DEBUGGER [Piano.Form1]

http://postimg.org/image/vdu0x1gv1/

注意:很长的帖子,很抱歉,但是我不知道该怎么解释。

我认为该程序只是让我感到困惑。

感谢那些看过这个问题的人。

要在图片框中播放声音,请执行以下操作

 [DllImport("winmm.dll")]
        private static extern bool PlaySound(string lpszName, int hModule, int dwFlags);

PlaySound("your sound loc ", 1, 0x0011);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM