在这方面似乎有无数个问题,但我找不到一个能起作用的问题。所以,我想是时候回答问题1000,001了。
我有一个带有PictureBox
和Panel
的自定义控件。Panel
是具有透明背景的PictureBox
的子代。这允许我在加载在PictureBox
中的任何图像之上进行tp绘图。
绘图部分工作,但擦除部分不工作。如果我使用Invalidate()
,我只会得到一串闪烁,而这一行根本就没有显示出来。
如果最终目标不明显,它应该像任何像样的绘图应用程序一样工作,在其中单击一个点,拖动,线移动与鼠标,直到你放手。
代码:
private void drawLine(Point pt) {
// Erase the last line
if (m_lastPoints != null) {
m_graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
m_graphics.DrawLine(m_transPen, m_lastPoints[0], m_lastPoints[1]);
}
// Set the last points
m_lastPoints = new Point[] { m_mouseStartPoint, pt };
m_graphics.DrawLine(new Pen(m_color), m_mouseStartPoint, pt);
}
m_transPen
被定义为new Pen(Color.FromArgb(0, 0, 0, 0));
其结果是:
现在,如果我把它改为:
m_graphics.DrawLine(Pens.White, m_lastPoints[0], m_lastPoints[1]);
我明白了,这表明了它应该做什么,只是用白线代替,它们应该是透明的。
发布于 2015-02-09 08:05:54
不用抹去那句老台词!只需使Panel
失效并绘制新的,最好是在Paint
事件中。
但是要想让它工作,Panel
就不能覆盖 PictureBox
。一定是内部的 it!将其放在load或构造函数事件中:
yourPanel.Parent = yourPictureBox;
yourPanel.Size = yourPictureBox.Size;
yourPanel.Location = Point.Empty;
(我知道你已经说对了,但也许下一个人只看答案;-)
为了避免闪烁,使用double-buffered Panel
..:
class DrawPanel : Panel
{
public DrawPanel()
{
DoubleBuffered = true;
}
}
..or,更好的是,Picturebox
或Label
(与Autosize=false
一起使用);两者都打开了DoubleBuffered
属性,并且支持比Panels
更好的绘图。
实际上是,如果您只想在加载的Image
之上绘制一些东西,那么甚至不需要单独的Panel
。只需使用PictureBox
本身就行了!它有三个独立的层:BackgroundImage
、Image
和Control surface
。
下面是绘制光标控制线的最小代码:
pictureBox1.MouseDown += pictureBox1_MouseDown;
pictureBox1.MouseMove += pictureBox1_MouseMove;
pictureBox1.MouseUp += pictureBox1_MouseUp;
pictureBox1.Paint += pictureBox1_Paint;
// class level
Point mDown = Point.Empty;
Point mCurrent = Point.Empty;
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (mDown != Point.Empty) e.Graphics.DrawLine(Pens.White, mDown, mCurrent);
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
mDown = Point.Empty;
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
mCurrent = e.Location;
pictureBox1.Invalidate();
}
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mDown = e.Location;
}
当您释放鼠标按钮时,该行将消失。
要使其永久化,您需要将其两点存储在绘制它们所需的数据列表中,并在Paint
事件中处理该列表。
这个列表可能还应该包括颜色,笔的宽度,然后还有一些,所以设计一个类'drawAction‘会有帮助。
https://stackoverflow.com/questions/28403514
复制相似问题