恕我直言,它不是好主意放置图片框 - 它不透明。所以我建议你手动绘制笔记。例如:
创建基调类:
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