我检查了我的触摸屏笔记本电脑,触摸事件可以通过MouseDown事件处理,触摸通过MouseUp和 touchmove 通过表单的MouseMove事件处理。
注意:我的机器同时支持触摸和鼠标。我不确定仅触摸设备或机器。
以下代码允许您通过触摸/鼠标交互在表单上绘制。
public partial class Form1 : Form
{
Image signature;
bool clicked = false;
Point previousPoint;
public Form1()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Paint += Form1_Paint;
this.MouseDown += Form1_MouseDown;
this.MouseUp += Form1_MouseUp;
this.MouseMove += Form1_MouseMove;
this.MouseLeave += Form1_MouseLeave;
this.FormClosing += Form1_FormClosing;
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Dispose signature after closing the form to avoid memory leak
signature.Dispose();
}
void Form1_Paint(object sender, PaintEventArgs e)
{
if (signature != null)
e.Graphics.DrawImage(signature, 0, 0);
}
void Form1_MouseDown(object sender, MouseEventArgs e)
{
clicked = true;
previousPoint = e.Location;
}
void Form1_MouseLeave(object sender, EventArgs e)
{
clicked = false;
}
void Form1_MouseUp(object sender, MouseEventArgs e)
{
clicked = false;
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (clicked)
{
if (signature == null)
signature = new Bitmap(this.Width, this.Height);
using (Graphics g = Graphics.FromImage(signature))
{
g.DrawLine(Pens.Black, previousPoint, e.Location);
previousPoint = e.Location;
this.Invalidate();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
签名绘制在图像上。因此,您可以根据需要将图像保存在数据库中。