【发布时间】:2015-09-28 16:35:28
【问题描述】:
我有一个主要的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 在
OnMouseClickevent 里面,我打电话给Invalidate()OnMouseMoveevent。
标签: c# winforms picturebox