【发布时间】:2019-10-10 14:59:10
【问题描述】:
在 ControlPaint.DrawReversibleLine 和 DrawReversibleFrame 之后擦除旧行时遇到问题。我确实擦除了,但屏幕(用户控件和图片框)在 Mouse_Move 时保留了旧行。如何彻底清除旧线?
我在 stackoverflow 上搜索过这些东西,所有答案都相似,但对我的情况不起作用。
//a picturebox1 on usercontrol.
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//...other code
ControlPaint.DrawReversibleLine(HorizontalSelectionPT1, HorizontalSelectionPT2, Color.Red); //erase old Horizontal line
ControlPaint.DrawReversibleLine(VerticalSelectionPT1, VerticalSelectionPT2, Color.Red); //erase old vertical line
Point point = PointToScreen(base.Location);
HorizontalSelectionPT1 = new Point(point.X, Cursor.Position.Y);
HorizontalSelectionPT2 = new Point(point.X + base.Width, Cursor.Position.Y);
VerticalSelectionPT1 = new Point(Cursor.Position.X, point.Y);
VerticalSelectionPT2 = new Point(Cursor.Position.X, point.Y + base.Height);
ControlPaint.DrawReversibleLine(HorizontalSelectionPT1, HorizontalSelectionPT2, Color.Red); //Draw new line
ControlPaint.DrawReversibleLine(VerticalSelectionPT1, VerticalSelectionPT2, Color.Red); //Draw new line
//draw selected frame
ControlPaint.DrawReversibleFrame(pbCanvas.RectangleToScreen(rectangle_0), Color.Black, FrameStyle.Dashed); //erase old frame
rectangle_0.Width = e.X - rectangle_0.X;
rectangle_0.Height = e.Y - rectangle_0.Y;
ControlPaint.DrawReversibleFrame(pbCanvas.RectangleToScreen(rectangle_0), Color.Black, FrameStyle.Dashed); //Draw new frame
//...other codes
}
【问题讨论】:
-
您需要以与最初绘制相反的顺序“撤消”先前的输出。所以 DrawReversibleFrame() 必须是第一个。当控件重新绘制自身时,您必须不撤消它,使用 Paint 事件来了解这一点。
-
以上代码中所有可反转的线条和边框都是用Mouse_Move绘制的。
-
该评论不太可能帮助我们帮助您。请避免使用 ControlPaint。而是使用 Paint 事件来绘制线条,在 MouseMove 事件处理程序中调用 Invalidate() 以强制重绘。
-
您的解决方案工作时有一点点闪烁。我在 Paint 事件中将 ControlPaint 用于可逆线和帧。
-
顺便说一句,我的颜色是红色,但为什么线条是“蓝色”?
标签: c# image paint picturebox