C#MouseMove事件太慢了

时间:2013-09-27 15:00:11

标签: c# mousemove

当我快速移动鼠标时,它无法捕获坐标。我确实阅读了大约20个MS的东西,所以我认为我可以捕获旧的和新的X和Y但是如何?

2 个答案:

答案 0 :(得分:1)

保存旧点并使用每个调用在旧光标位置和新光标位置之间绘制一条线。像这样的东西

Point oldPoint = Point.Empty;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    Point newPoint = e.Location;

    if (oldPoint != Point.Empty)
        DrawLine(oldPoint, newPoint);

    //save away as oldpoint
    oldPoint = newPoint;
}

void DrawLine(Point from, Point to)
{
    //calculate the length of distance between from and to
    int xdiff = to.X - from.X;
    int ydiff = to.Y - from.Y;
    double length = Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
    //if the length is zero exit here (or else divide by zero happens below)
    if (length == 0)
        return;

    //nx are the normalized xdiff and ydiff used for travelling along the line in
    //equal units below
    double nx = xdiff / length;
    double ny = ydiff / length;

    //draw ellipses along the line every 4 pixels (lowering it from 4 increases the draw
    //smoothness but at the cost of performance)
    for (int i = 0; i <= length; i += 4)
    {
        //the point along the line to draw an ellipse
        double px = from.X + nx * i;
        double py = from.Y + ny * i;

        //where px and py are the center of the ellipse, this draws a 10x10 ellipse
        //with that center
        g.FillEllipse(Brushes.Blue, (float)px - 5, (float)py - 5, 10, 10); 
    }
}

答案 1 :(得分:0)

在mousemove事件中保存旧坐标。在下一次鼠标移动事件中与新的比较? (我不确定你的问题)

    private System.Drawing.Point _OldPoint { get; set; }

    void MouseMoveEvent(object sender, MouseEventArgs e)
    {
        if (_OldPoint != null)
        {
            var diffX = e.Location.X - _OldPoint.X;
            var diffY = e.Location.Y - _OldPoint.Y;
        }
        _OldPoint = e.Location;
    }