我让用户点击屏幕上的一个点,直到他们选择第二个点,该行将跟随光标。一旦第二点被绘制,它将保持不变。我正在使用双缓冲区:
public void EnableDoubleBuffering()
{
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
}
我将在Form_Load()中调用该函数;
我正在这样做:
void draw(int x1, int y1, int x2, int y2)
{
Graphics formGraphics = pictureEdit1.CreateGraphics();
Pen myPen = new Pen(Color.Red, 3);
formGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
formGraphics.DrawLine(myPen, x1, y1, x2, y2);
myPen.Dispose();
formGraphics.Dispose();
}
我从MouseMove事件中重复调用该函数;
void pictureEdit1_MouseMove(object sender, MouseEventArgs e)
{
if (click == 1 && !rightClicked)
{
pictureEdit1.Invalidate();
trail.X = e.X;
trail.Y = e.Y;
draw(p1.X, p1.Y, trail.X, trail.Y);
}
else if (click != 1)
{
draw(p1.X, p1.Y, trail.X, trail.Y);
}
}
发生了轻微的闪烁,这让我疯了!请帮助,谢谢。
答案 0 :(得分:0)
此代码可以执行您想要的操作,而不会出现任何闪烁。请记住,在绘制线后我不知道你想做什么,所以它会消失。但这应该会让你很好地了解如何做到这一点:
public partial class Form1 : Form
{
private Point _firstPoint;
private Point _secondPoint;
private bool _hasClicked;
public Form1()
{
InitializeComponent();
_hasClicked = false;
_firstPoint = new Point();
_secondPoint = new Point();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureEdit1_MouseMove(object sender, MouseEventArgs e)
{
_secondPoint.X = e.X;
_secondPoint.Y = e.Y;
pictureEdit1.Refresh();
}
private void pictureEdit1_MouseUp(object sender, MouseEventArgs e)
{
if (!_hasClicked)
{
_firstPoint.X = e.X;
_firstPoint.Y = e.Y;
}
_hasClicked = !_hasClicked;
pictureEdit1.Refresh();
}
private void pictureEdit1_Paint(object sender, PaintEventArgs e)
{
if (_hasClicked)
e.Graphics.DrawLine(Pens.Red, _firstPoint, _secondPoint);
}