📄 signaturebox.cs
字号:
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
namespace Microsoft.Sql.SqlCe.Samples.Cs.IBuySpyDelivery.IBuySpyDevice
{
/// <summary>
/// This class captures and displays the signature.
/// </summary>
public class SignatureBox : Control
{
int numPoints = 0;
Pen pen = null;
Stroke stroke = null;
ArrayList signature = null;
SolidBrush brush = null;
int penWidth = 2;
private Graphics graphics;
private Bitmap bitmap;
private bool hasCapture;
private int oldX, oldY;
private int maxWidth, maxHeight;
public short[] Signature
{
get
{
int index = 0;
short[] buf = new short[numPoints];
for (int i=0; i<signature.Count; i++)
{
short[] stroke = null;
stroke = ((Stroke)signature[i]).ToShortArray();
Array.Copy(stroke, 0, buf, index, stroke.Length);
index += stroke.Length;
}
return buf;
}
set
{
numPoints = 0;
signature.Clear();
int prevIndx = 0;
for (int i=0; i < value.Length; i += 2)
{
if ( unchecked ((short) 0xFFFF) == value[i])
{
Stroke stroke = new Stroke(50);
stroke.Fill(value, prevIndx, i);
signature.Add(stroke);
prevIndx = i + 2;
}
}
numPoints = value.Length;
}
}
public int PenWidth
{
set
{
penWidth = value;
}
}
public SignatureBox (int width, int length)
{
this.maxHeight = length;
this.maxWidth = width;
this.bitmap = new Bitmap(this.maxWidth, this.maxHeight);
this.graphics = Graphics.FromImage(this.bitmap);
this.graphics.FillRectangle(
new SolidBrush(Color.Gold), 0, 0, this.maxWidth, this.maxHeight);
pen = new Pen(Color.Blue);
brush = new SolidBrush(Color.DarkBlue);
signature = new ArrayList(50);
this.MouseDown += new MouseEventHandler(MouseDownHandler);
this.MouseMove += new MouseEventHandler(MouseMoveHandler);
this.MouseUp += new MouseEventHandler(MouseUpHandler);
}
public void Clear()
{
numPoints = 0;
signature.Clear();
this.graphics.FillRectangle(
new SolidBrush(Color.Gold), 0, 0, this.maxWidth, this.maxHeight);
Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(this.bitmap, 0, 0);
}
private void MouseMoveHandler(object sender, MouseEventArgs e)
{
int x, y;
if (this.hasCapture)
{
if (e.X < this.penWidth || e.X >= this.maxWidth - this.penWidth || e.Y < this.penWidth || e.Y >= this.maxHeight - this.penWidth)
{
return;
}
x = e.X;
y = e.Y;
numPoints += 2;
if (this.oldX != x && this.oldY != y)
{
Rectangle rectDraw;
rectDraw = new Rectangle(Math.Min(x, this.oldX), Math.Min(y, this.oldY), Math.Abs(this.oldX - x) + 1, Math.Abs(this.oldY - y) + 1);
stroke.Add(new Point(x, y));
DrawSignatureLine(this.oldX, this.oldY, x, y, ref rectDraw);
Invalidate(rectDraw);
this.oldX = x;
this.oldY = y;
}
}
}
private void MouseDownHandler(object sender, MouseEventArgs e)
{
if (e.X < this.penWidth || e.X >= this.maxWidth - this.penWidth || e.Y < this.penWidth || e.Y >= this.maxHeight - this.penWidth)
{
return;
}
this.hasCapture = true;
this.Capture = true;
this.oldX = e.X;
this.oldY = e.Y;
this.graphics.DrawLine(
this.pen, this.oldX, this.oldY, this.oldX, this.oldY);
this.Invalidate(new Rectangle(this.oldX, this.oldY, 1, 1));
numPoints += 2;
stroke = new Stroke(50);
stroke.Add(new Point(e.X, e.Y));
signature.Add(stroke);
}
private void MouseUpHandler(object sender, MouseEventArgs e)
{
if (null != stroke)
{
if (e.X >= penWidth && e.X < this.maxWidth - this.penWidth && e.Y >= this.penWidth && e.Y < this.maxHeight - this.penWidth)
{
this.oldX = e.X;
this.oldY = e.Y;
if (this.oldX != e.X && this.oldY != e.Y)
{
numPoints += 2;
stroke.Add(new Point(e.X, e.Y));
}
stroke = null;
}
}
this.hasCapture = false;
}
public delegate void RefreshDelegate();
public void DisplaySignature()
{
Rectangle rect = new Rectangle(0, 0, 0, 0);
this.graphics.FillRectangle(
new SolidBrush(Color.Gold), 0, 0, this.maxWidth, this.maxHeight);
foreach (Stroke s in signature)
{
if (s != null)
{
Point[] pa = s.ToPointArray();
int len = pa.Length;
if (len > 1)
{
Point prev = pa[0];
for (int i=1; i < len; i++)
{
DrawSignatureLine(prev.X, prev.Y, pa[i].X, pa[i].Y, ref rect);
prev = pa[i];
}
}
else if (1 == len)
{
Point pt = pa[0];
this.bitmap.SetPixel(pt.X, pt.Y, Color.DarkBlue);
DrawSignatureLine(pt.X, pt.Y, pt.X + 1, pt.Y + 1, ref rect);
}
}
}
this.Invoke(new RefreshDelegate(Refresh));
//this.Refresh();
}
private void DrawSignatureLine(int startX, int startY, int endX, int endY, ref Rectangle rectDraw)
{
int offset = 1;
/// Draw line from last hit point to current hit point
///
this.graphics.DrawLine(this.pen, startX, startY, endX, endY);
if (Math.Abs(endX - startX) > Math.Abs(endY - startY))
{
for (int i = 0; i < penWidth - 1; ++i)
{
this.graphics.DrawLine(this.pen, startX, startY + offset, endX, endY + offset);
offset = offset > 0 ? -offset : (-offset) + 1;
}
rectDraw.Y = Math.Min(endY, startY) + 1 - Math.Abs(offset);
rectDraw.Height = Math.Abs(startY - endY) + 1 + Math.Abs(offset);
}
else
{
for (int i = 0; i < penWidth - 1; ++i)
{
this.graphics.DrawLine(this.pen, startX + offset, startY, endX + offset, endY);
offset = offset > 0 ? -offset : (-offset) + 1;
}
rectDraw.X = Math.Min(endX, startX) + 1 - Math.Abs(offset);
rectDraw.Width = Math.Abs(startX - endX) + 1 + Math.Abs(offset);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -