这取决于你的鼠标移动的速度,有时MouseMove事件会更频繁地触发,有时不会。我认为这也取决于你的机器在这个特定的系统中加载了多少。如果你在两点之间画线,它们不会弯曲,而是笔直。相反,你应该看看Beziers和Splines。这样你就可以基于几点得到曲线。
但是你可以用你的代码做些什么。每当最后一次mousedown和当前mousedown事件之间的距离大于一个阈值(您可以凭经验获得),可以在曲线上添加新的点。下面 是被加入一个点的例子的代码:
public bool isMouseDown { get; set; }
Point lastPoint = Point.Empty;
public double treshold { get; set; }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
pictureBox1.Capture = true; // I try to capture mouse here
Graphics g = Graphics.FromHwnd(this.pictureBox1.Handle);
if (Math.Sqrt(Math.Pow(e.X - lastPoint.X, 2) + Math.Pow(e.Y - lastPoint.Y, 2)) > treshold)
{
g.FillRectangle(new SolidBrush(Color.Black), (e.X + lastPoint.X)/2, (e.Y + lastPoint.Y)/2, 1, 1);
}
g.FillRectangle(new SolidBrush(Color.Black), e.X, e.Y, 1, 1);
lastPoint = new Point(e.X, e.Y);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
lastPoint = new Point(e.X, e.Y);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
我知道我可以使用Bezier等多项式算法来做到这一点,但我不能说明这一点吗?这不可能吗? – abilash
@Vsevywniy根据我的经验,你不能影响多少鼠标移动事件将被提出的速度。也没有最小的距离,将触发mousemove。因此你会得到中断曲线。 –
当我在标准窗口中绘画使用铅笔工具绘制图形编辑器时,我获得了不间断的曲线。这是否意味着它使用Bezier等多项式算法形成? – abilash