WINCE上的手写签名,同样可应用于WINFORM.

主要应用的知识为,在鼠标点击,移动时,获取当前的点,在每一笔都为一个集合,一个集合中包含了这一笔中所有的点.通过调用DrawLines方法来画线.

代码很简单,如下所示

  
    
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace SmartDeviceProject1
{
public partial class MobileTest : Form
{

List
< Points > handTrack = new List < Points > ();
public Graphics pb_Graphics;
Bitmap pb_BMP
= new Bitmap( 240 , 268 );//签名时图片的大小
int index = 0 ; // 第几笔
bool isDrawing = false ;//用于在move时,只有在Down的时候才画.


public MobileTest()
{
InitializeComponent();
}

private void Form1_Paint( object sender, PaintEventArgs e)
{
try
{
pb_Graphics
= Graphics.FromImage((System.Drawing.Image)pb_BMP);//获取图片的Graphics
pb_Graphics.Clear(
this .BackColor);
foreach (Points item in handTrack)
{
if (item.PT.Count > 1 )
pb_Graphics.DrawLines(
new Pen(Color.Black), item.PT.ToArray());
}
e.Graphics.DrawImage(pb_BMP,
0 , 0 );//其实是在当前控件上写了一个图片
pb_Graphics.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void Form1_MouseDown( object sender, MouseEventArgs e)
{
isDrawing
= true ;
handTrack.Add(
new Points());
handTrack[index].PT.Add(
new Point(e.X, e.Y));
this .Refresh();
}

private void Form1_MouseMove( object sender, MouseEventArgs e)
{
if (isDrawing == true )
{
handTrack[index].PT.Add(
new Point(e.X, e.Y));
this .Refresh();
}
}

private void Form1_MouseUp( object sender, MouseEventArgs e)
{
index
++ ;
isDrawing
= false ;
}
//一个菜单按钮,用于清空当前的签名.

private void menuItem1_Click( object sender, EventArgs e)
{
this .handTrack.Clear();
index
= 0 ;
this .Refresh();
}


}

class Points
{
public List < Point > PT = new List < Point > ();
}
}

在Paint的时候,用了当item.PT.count>1的时候才画,这是因为,这个方法只有在集合点数大于1的时候才正确,否则会出现参数错误的报错信息.

当然也可以在0<count<=1的时候,调用DrawLine来单独画点.

代码在CINCE 5.0 ,WINDOWS XP上测试通过.

使用VS2008,SDK为3.5

你可能感兴趣的:(WinForm)