1

我有一个 Windows Mobile 应用程序(C#,.Net framework 2),它的代码库现在也被用于在 Windows 7 和 Windows 8 上运行。我们为这个应用程序编写的控件之一是一个涂鸦控件,它允许用户捕获签名。此控件在 Windows Mobile 上运行良好,可用于很好地捕获签名。然而,在 Windows 上,OnMouseMove面板控件的覆盖方法太慢,或者很少调用,并且签名变得非常“块状”。因此,例如,如果您尝试捕获一个圆,在 Windows Mobile 上您会得到一个合理的圆,但在 Windows 上您最终会得到一个正方形,因为mousemove调用的频率不够高。这是OnMouseMove面板的覆盖方法:

protected override void OnMouseMove(MouseEventArgs e)
{
    base.OnMouseMove(e);
    if (!_captureMouseCoordinates) { return; }
    LineToDraw line = new LineToDraw();
    line.StartX = _lastMouseCoordinates.X;
    line.StartY = _lastMouseCoordinates.Y;
    line.EndX = e.X;
    line.EndY = e.Y;
    _points.Add(line);
    _graphicsHandle.DrawLine(_scribblePen, line.StartX, line.StartY, line.EndX, line.EndY);            
    // Refresh rectangle for the line drawn
    Point leftCorner = new Point();
    Size rectangleSize = new Size();
    // Case 1: line down, right
    if (line.StartX <= line.EndX && line.StartY <= line.EndY) 
    {
        leftCorner.X = line.StartX;
        leftCorner.Y = line.StartY;
    }
    // Case 2: Line up, right
    if (line.StartX <= line.EndX && line.StartY >= line.EndY) 
    {
         leftCorner.X = line.StartX;
         leftCorner.Y = line.EndY;
    }
    // Case 3: Line up, left
    if (line.StartX >= line.EndX && line.StartY >= line.EndY) 
    {
        leftCorner.X = line.EndX;
        leftCorner.Y = line.EndY;
    }
    // Case 4: Line down, left
    if (line.StartX >= line.EndX && line.StartY <= line.EndY) 
    {
        leftCorner.X = line.EndX;
        leftCorner.Y = line.StartY;
    }
    rectangleSize.Height = Math.Abs(line.EndY-line.StartY)+1;
    rectangleSize.Width = Math.Abs(line.EndX-line.StartX)+1;
    // save last mouse co-ordinates
    _lastMouseCoordinates.X = line.EndX;
    _lastMouseCoordinates.Y = line.EndY;
    Invalidate(new Rectangle(leftCorner.X,leftCorner.Y, rectangleSize.Width, rectangleSize.Height));
}

上面代码中的 _captureMouseCoordinates 布尔成员变量是在“mousedown”事件处理程序上设置的。

我注意到,如果我在此方法中注释掉无效并且仅使签名无效mouseup,那么签名会更加平滑,但是在绘制签名时您不会看到签名。有没有人知道我如何才能让这个签名控件表现得更好,以便它产生的签名更平滑,但仍然能够在绘制线条时向用户提供反馈。

4

1 回答 1

1

I don't know where _graphicsHandle is - but you should generally never save Graphics objects. Let all rendering be in Invalidate().

Create two collections:

List<Point> _currentStroke
List<List<Point>> _allStrokes

MouseMove:

_currentStroke.Add(e.Location)

MouseUp

   _allStrokes.Add(_currentStroke)

On each OnPaint

   foreach(var stroke in _allStrokes)
       {g.DrawLines(pen, stroke.ToArray());}
   if(_currentStroke.Count > 1)
       g.DrawLines(pen, _currentStroke)

(Obviously add all supporting code).

You should have no performance issues.

于 2013-02-05T17:34:50.470 回答