📄 signaturecontrol.cs
字号:
/*
SignatureControl class
--
Collects and displays a signature. The signature is made up of
a list of line segments. Each segment contains an array of points
(x and y coordinates).
Draws to a memory bitmap to prevent flickering.
Raises the SignatureUpdate event when a new segment is added to
the signature.
*/
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using Common;
namespace PocketSignature
{
/// <summary>
/// Custom control that collects and displays a signature.
/// </summary>
public class SignatureControl : Control
{
// gdi objects
Bitmap _bmp;
Graphics _graphics;
Pen _pen = new Pen(Color.Black);
// list of line segments
ArrayList _lines = new ArrayList();
// the current line segment
ArrayList _points = new ArrayList();
Point _lastPoint = new Point(0,0);
// if drawing signature or not
bool _collectPoints = false;
// notify parent that line segment was updated
public event EventHandler SignatureUpdate;
/// <summary>
/// List of signature line segments.
/// </summary>
public ArrayList Lines
{
get { return _lines; }
}
/// <summary>
/// Return the signature flattened to a stream of bytes.
/// </summary>
public byte[] SignatureBits
{
get { return SignatureData.GetBytes(this.Width, this.Height, _lines); }
}
public SignatureControl()
{
}
protected override void OnPaint(PaintEventArgs e)
{
// blit the memory bitmap to the screen
// we draw on the memory bitmap on mousemove so there
// is nothing else to draw at this time (the memory
// bitmap already contains all of the lines)
CreateGdiObjects();
e.Graphics.DrawImage(_bmp, 0, 0);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// don't pass to base since we paint everything, avoid flashing
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
// process if currently drawing signature
if (!_collectPoints)
{
// start collecting points
_collectPoints = true;
// use current mouse click as the first point
_lastPoint.X = e.X;
_lastPoint.Y = e.Y;
// this is the first point in the list
_points.Clear();
_points.Add(_lastPoint);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
// process if drawing signature
if (_collectPoints)
{
// stop collecting points
_collectPoints = false;
// add current line to list of segments
Point[] points = new Point[_points.Count];
for (int i=0; i < _points.Count; i++)
{
Point pt = (Point)_points[i];
points[i].X = pt.X;
points[i].Y = pt.Y;
}
_lines.Add(points);
// start over with a new line
_points.Clear();
// notify container a new segment was added
RaiseSignatureUpdateEvent();
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// process if drawing signature
if (_collectPoints)
{
// add point to current line segment
_points.Add(new Point(e.X, e.Y));
// draw the new segment on the memory bitmap
_graphics.DrawLine(_pen, _lastPoint.X, _lastPoint.Y, e.X, e.Y);
// update the current position
_lastPoint.X = e.X;
_lastPoint.Y = e.Y;
// display the updated bitmap
Invalidate();
}
}
/// <summary>
/// Clear the signature.
/// </summary>
public void Clear()
{
_lines.Clear();
InitMemoryBitmap();
Invalidate();
}
/// <summary>
/// Create any GDI objects required to draw signature.
/// </summary>
private void CreateGdiObjects()
{
// only create if don't have one or the size changed
if (_bmp == null || _bmp.Width != this.Width || _bmp.Height != this.Height)
{
// memory bitmap to draw on
InitMemoryBitmap();
}
}
/// <summary>
/// Create a memory bitmap that is used to draw the signature.
/// </summary>
private void InitMemoryBitmap()
{
// load the background image
_bmp = Global.LoadImage("sign here.png");
// get graphics object now to make drawing during mousemove faster
_graphics = Graphics.FromImage(_bmp);
}
/// <summary>
/// Notify container that a line segment has been added.
/// </summary>
private void RaiseSignatureUpdateEvent()
{
if (this.SignatureUpdate != null)
SignatureUpdate(this, EventArgs.Empty);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -