C# 绘制C时屏幕闪烁#

C# 绘制C时屏幕闪烁#,c#,graphics,draw,C#,Graphics,Draw,我让用户点击屏幕上的一个点,直到他们选择第二个点,这条线将跟随光标。一旦画出第二个点,它将保持不变。我使用双缓冲区,如下所示: public void EnableDoubleBuffering() { this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); } 我将以形式_Load()调用该函数 我是这样画的: voi

我让用户点击屏幕上的一个点,直到他们选择第二个点,这条线将跟随光标。一旦画出第二个点,它将保持不变。我使用双缓冲区,如下所示:

public void EnableDoubleBuffering()
{
    this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
}
我将以形式_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);
    }
}

有一个非常轻微的闪烁发生,它是驾驶我疯狂!请帮助,谢谢。

此代码将执行您想要的操作,不会闪烁。记住,我不知道画完线后你想做什么,所以它会消失。但这会让你对如何做到这一点有一个很好的了解:

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);
    }

将点存储为表单的字段并在
pictureEdit1
OnPaint
方法中绘制线条如何?CreateGraphics的攻击!使用绘制事件。调用控件的Invalidate方法使其绘制。非常感谢,这对闪烁有帮助。最后一个问题是,一旦我使()无效,但用户停止移动鼠标。当这种情况发生时,它就不再画了。我应该使用System.Timer.Timer吗?计时器一过我就再画?最好的方法是什么?
System.Windows.Forms.Timer
@HighCore-下面我的代码看起来比您链接的示例简单。