ボタンをクリックすると、マウスを押した時間に応じてリンクされたメモが印刷されるようにプログラムを作成しています。私の問題は、最初のクリックが正常に機能することですが、2回押し続けると更新されず、頭がおかしくなります。どんな助けでも大歓迎です。以下に、私が使用している次のコードを示します。ありがとう
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);
}
}
}