改进面板OnMouseMove方法用于签名控件的性能 [英] Improve the performance of a panel OnMouseMove method for a signature control

查看:76
本文介绍了改进面板OnMouseMove方法用于签名控件的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Windows Mobile application (C#, .Net framework 2), the codebase of which is also now being used to run on Windows 7 and Windows 8. One of the controls that we wrote for this application is a scribble control that allows the user to capture a signature. This control works ok on Windows Mobile and can be used to capture signatures fairly well. On Windows, however, the OnMouseMove override method of a panel control is too slow, or called to infrequently, and the signature becomes very "blocky". So, for example, if you try capture a circle, on Windows Mobile you get a reasonable circle, but on Windows you end up getting a square because mousemove is not called frequently enough. Here is the OnMouseMove override method of the panel:

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'上设置的'事件处理程序。

The _captureMouseCoordinates boolean member variable in the code above is being set on the 'mousedown' event handler.

我注意到,如果我注释掉此方法中的无效,并且仅对 mouseup 无效,则签名为平滑得多,但是在绘制签名时您看不到签名。有谁知道我如何才能使此签名控件更好地执行,以使它产生的签名更平滑,同时在绘制线条时仍能向用户提供反馈。

I notice that if I comment out the invalidate in this method and only invalidate on mouseup that the signature then is much smoother, but then you don't get to see the signature while it is being drawn. Does anyone have any ideas how I could go about getting this signature control to perform better so that the signatures produced by it are smoother and yet still be able to give feedback to the user as the lines are being drawn.

推荐答案

我不知道_graphicsHandle在哪里-但通常不应保存Graphics对象。让所有呈现都在Invalidate()中。

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

创建两个集合:

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

MouseMove:

MouseMove:

_currentStroke.Add(e.Location)

MouseUp

   _allStrokes.Add(_currentStroke)

在每个OnPaint上

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).

您应该没有性能问题。

这篇关于改进面板OnMouseMove方法用于签名控件的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆