0
我有一个主要的PictureBox
它添加到它,其他图片框;我通过父母的孩子,并将其添加到父如下:添加多个图片框到一个主要的图片框,并绘制它们
public class VectorLayer : PictureBox
{
Point start, end;
Pen pen;
public VectorLayer(Control parent)
{
pen = new Pen(Color.FromArgb(255, 0, 0, 255), 8);
pen.StartCap = LineCap.ArrowAnchor;
pen.EndCap = LineCap.RoundAnchor;
parent.Controls.Add(this);
BackColor = Color.Transparent;
Location = new Point(0, 0);
}
public void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(pen, end, start);
}
public void OnMouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
}
public void OnMouseMove(object sender, MouseEventArgs e)
{
end = e.Location;
Invalidate();
}
public void OnMouseUp(object sender, MouseEventArgs e)
{
end = e.Location;
Invalidate();
}
}
和我处理来自主PictureBox
里面那些On Events
,现在在主PictureBox
我在操控性上Paint
事件如下:
private void PicBox_Paint(object sender, PaintEventArgs e)
{
//current layer is now an instance of `VectorLayer` which is a child of this main picturebox
if (currentLayer != null)
{
currentLayer.OnPaint(this, e);
}
e.Graphics.Flush();
e.Graphics.Save();
}
但是当我画什么都不出现,当我做Alt+Tab
从它失去焦点,我看到我的载体,当我尝试重新绘制和失去焦点没有任何反应..
为什么这种奇怪的行为,我该如何解决它?
'currentLayer'在哪里设置? –
@PatrikEckebrecht在OnMouseClick事件中,并且我调用Invalidate()OnMouseMove事件。 – Abanoub