0

我正在编写一个程序,以便单击按钮并根据您将鼠标按住的时间打印链接的注释。我的问题是第一次点击工作正常,但是当我第二次坚持时它没有更新,这让我发疯。任何帮助都感激不尽。下面找到我正在使用的以下代码。谢谢

对于 Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NoteShape
{
    public partial class Form1 : Form
    {
        public int duration = 0;
        MusicNote mn;

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            duration++;
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                timer1.Enabled = true;
                duration = 0;
            }
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                timer1.Enabled = false;
            }
            duration = duration % 20;

            string bNoteShape = "";

            if (duration >= 12)
            {
                bNoteShape = "SemiBreve";
            }

            if ((duration >= 6) && (duration <= 11))
            {
                bNoteShape = "Minim";
            }

            if ((duration >= 3) && (duration <= 5))
            {
                bNoteShape = "Crotchet";
            }

            if ((duration >= 1) && (duration <= 2))
            {
                bNoteShape = "Quaver";
            }

            if (duration == 0)
            {
                bNoteShape = "SemiQuaver";
            }


            mn = new MusicNote(1, bNoteShape);
            MessageBox.Show(bNoteShape);
            this.Controls.Add(this.mn);

        }
    }
}

对于相应的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace NoteShape
{
    class MusicNote: PictureBox
    {
        public string path = "ImagesName\\";
        public int pitch;
        public string noteShape;

        public MusicNote(int iPitch, string iNoteShape) : base()
        {
            pitch = iPitch;
            noteShape = iNoteShape;

            Location = new Point(150, 50);
            Size = new Size(40, 40);
            Bitmap bmp1 = new Bitmap(path + noteShape + ".bmp");
            Image = bmp1;
            BackColor = Color.Transparent;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
}
4

2 回答 2

0

您正在创建一个新笔记,但每次都将其放置在相同的位置。

您应该编辑现有/单击的注释或放置新注释。

于 2012-12-21T13:35:29.753 回答
0

我刚刚在 Visual Studio 2010 中“按原样”尝试了您的代码,没有任何问题。我唯一改变的是我评论了位图,因为我没有它们,但我选择了背景颜色黑色来查看它。

 //Bitmap bmp1 = new Bitmap(path + noteShape + ".bmp");
 //Image = bmp1;
 BackColor = Color.Black;

另外,我将 timer1 设置为 500 毫秒的间隔,因为我不知道你的间隔是多少。

我可以一遍又一遍地做,然后弹出消息框,里面有正确的东西!您可以再试一次,或者更新您的代码,因为现在完全没有问题。

编辑

您需要将其添加到您的 button1_MouseUp

this.Controls.Add(mn);
mn.BringToFront();

画框画在前一个画框的后面。

于 2012-12-21T14:50:18.083 回答