恕我直言,它不是好主意放置圖片框 - 它不透明。所以我建議你手動繪製筆記。例如:
創建基調類:
public class Note
{
public Point Location { get; set; }
public virtual void Draw(Graphics g)
{
}
}
創建筆記,可以說簡單,困難和圖片(其中提請圖片):
public class SimpleNote:Note
{
public SimpleNote(Point position)
{
Location = position;
}
public override void Draw(Graphics g)
{
g.FillEllipse(Brushes.Red, Location.X, Location.Y, 5, 5);
g.DrawLine(new Pen(Color.Red), Location.X+5, Location.Y, Location.X+5, Location.Y-15);
}
}
public class DifficultNote:Note
{
public DifficultNote(Point position)
{
Location = position;
}
public override void Draw(Graphics g)
{
SimpleNote left = new SimpleNote(Location);
SimpleNote right = new SimpleNote(new Point(Location.X + 20, Location.Y));
left.Draw(g);
right.Draw(g);
g.DrawLine(new Pen(Color.Red), Location.X+5, Location.Y - 15, Location.X+25, Location.Y-15);
}
}
public class PictureNote:Note
{
private Image _image;
public PictureNote(Image image, Point position)
{
Location = new Point(position.X - image.Width/2, position.Y - image.Height/2);
_image = image;
}
public override void Draw(Graphics g)
{
g.DrawImage(_image, Location);
}
}
在職員類中添加註釋列表並在OnPai中調用繪圖方法NT方法:
public List<Note> noteList = new List<Note>();
protected override void OnPaint(PaintEventArgs e)
{
int yPos = kOffset + staffIndex * kStaffSpacing;
for (int bars = 0; bars < 5; bars++)
{
e.Graphics.DrawLine(Pens.Black, 0, yPos, kStaffInPixels, yPos);
yPos += kBarSpacing;
}
foreach (var note in noteList)
{
note.Draw(e.Graphics);
}
}
,並在您的工作人員面板創建鼠標單擊事件處理程序,例如像這樣:
private void staff1_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
staff1.noteList.Add(new SimpleNote(new Point(e.X, e.Y)));
if(e.Button == MouseButtons.Right)
staff1.noteList.Add(new DifficultNote(new Point(e.X, e.Y)));
if(e.Button == MouseButtons.Middle)
staff1.noteList.Add(new PictureNote(new Bitmap("c:\\note.png"), new Point(e.X, e.Y)));
staff1.Invalidate();
}
結果(第一個是從畫面,第二個困難,三是簡單):
詩代碼不是最好的,我只是說明我的想法。
從該事件被你叫addPictureBox()? – 2011-12-28 14:35:34
我從MouseUp事件調用addPictureBox() – Clayton 2011-12-28 14:38:25
你知道如何使用調試器嗎?這對你自己來說會更容易,而且如果你在求助於stackoverflow之前先嚐試調試過這些東西,那麼速度會更快。當你不知道如何做某事,或者發生什麼事情是不合邏輯的時候,Stackoverflow就是用來做這件事的。你的問題不是那些;你的問題只是你需要做一些調試。 – 2011-12-28 14:42:20