图片框背景图像不刷新

时间:2013-12-04 23:30:04

标签: c# winforms

我在面板1中有一些按钮,并且根据MouseDown和MouseUp事件之间的持续时间(刻度数),图片显示在panel2中。按下的按钮确定图片的位置。第一次按下每个按钮时,程序工作正常。 第二次按下按钮时会出现问题,因为图像不会改变。例如。按下button1,计算4个滴答,显示图片A.              再次按下button1,计算7个刻度并显示图片B而不是图片A,但问题是图片A停留在那里! 我认为这与OnPaint方法有关,但经过几次尝试我无法解决问题,欢迎提出任何建议...... Tks

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

namespace SimplePiano
{
    class MusKey : Panel
    {
            protected int duration;
            public int musicNote;
            public TextBox txt1 = new TextBox(); //To test if musicNote refers to the correct pitch integer.
            public TextBox txt2 = new TextBox(); //To test the number of ticks.
            protected Timer timer = new Timer();

            public MusKey(int iNote, int x, int y): base()
            {
                musicNote = iNote;
                this.Location = new Point(x, y);
                this.Size = new Size(50, 200);
                this.BackColor = Color.White;
                this.BorderStyle = BorderStyle.FixedSingle;
                this.Visible = true;

                this.MouseDown += new MouseEventHandler(this.MusKey_MouseDown);
                this.MouseUp += new MouseEventHandler(this.MusKey_MouseUp);
            }

            protected void MusKey_MouseDown(object sender, EventArgs e)
            {
                duration = 0;
                timer.Interval = 100;
                timer.Tick += new EventHandler(timer1_Tick);
                txt1.Text = Convert.ToString(musicNote)+" down"; //To test if musicNote refers to the correct pitch integer.
                timer.Tick += new EventHandler(timer1_Tick);
                timer.Enabled = true;
                timer.Start();
                duration = 0;
            }

            protected void MusKey_MouseUp(object sender, EventArgs e)
            {
                timer.Stop();
                txt1.Text = Convert.ToString(musicNote)+ " up"; //To test if musicNote refers to the correct pitch integer.
                txt2.Text = Convert.ToString(duration);         //To test the number of ticks.
                timer.Enabled = false;
                string bNoteShape = "";

                if (duration < 5) bNoteShape = "Crotchet.png";
                if (duration > 5) bNoteShape = "minim.png";
                MusicNote musNote = new MusicNote(this.musicNote, bNoteShape);

                Form1.Ms.Controls.Add(musNote);

            }

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

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

namespace SimplePiano
{
    public class MusicNote: PictureBox
    {
        public string path = "";
        public int pitch; //The no. of the music key (e.g. the sound freuency).
        public string noteShape; //Mapped to a note shape(e.g. Crotchet, Minim, Quaver etc.)
        public int noteDuration;

        public MusicNote(int iPitch, string iNoteShape):base()
        {
            pitch = iPitch;
            noteShape = iNoteShape;
            Location = new Point((pitch*40)-40, 100);
            Size = new Size(40, 40);
            //Bitmap bmp = new Bitmap(noteShape + ".png");
            BackgroundImage = Image.FromFile(noteShape);
            this.BackColor = Color.Transparent;
            Image = Image;
            this.Visible = true;
            this.BringToFront();
        }

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

1 个答案:

答案 0 :(得分:1)

说实话,这是猜测,但希望你能从中得到一些指示:

MusicNote musNote = null;
protected void MusKey_MouseUp(object sender, EventArgs e)
{
    timer.Stop();
    txt1.Text = Convert.ToString(musicNote)+ " up"; //To test if musicNote refers to the correct pitch integer.
    txt2.Text = Convert.ToString(duration);         //To test the number of ticks.
    timer.Enabled = false;
    string bNoteShape = "";

    if (duration < 5) bNoteShape = "Crotchet.png";
    if (duration > 5) bNoteShape = "minim.png";

    //Remove the previous musNote Picture box before adding another one:
    if (musNote != null) Form1.Ms.Controls.Remove(musNote);
    musNote = new MusicNote(this.musicNote, bNoteShape);

    Form1.Ms.Controls.Add(musNote);

    //maybe a red herring, but just encase make sure picture box is on top:
    musNote.BringToFront()
}

我不明白为什么要将Image分配给Image?

public MusicNote(int iPitch, string iNoteShape):base()
{
    pitch = iPitch;
    noteShape = iNoteShape;
    Location = new Point((pitch*40)-40, 100);
    Size = new Size(40, 40);
    //Bitmap bmp = new Bitmap(noteShape + ".png");
    BackgroundImage = Image.FromFile(noteShape);
    this.BackColor = Color.Transparent;
    Image = Image;   //<- why assign Image to Image?
    this.Visible = true;
    this.BringToFront(); // <- try this line in the MusKey class instead, again red herring
}