在如今这个时代,对这个功能的需求量可能已经很少了,但是这也不妨碍我们或多或少的会碰到一些稀奇古怪的需求,多接触一些总归是没错的。
本篇主要是用到了Microsoft.Ink来实现一个手写输入,也就是类似于手机输入法中的手写功能。
实现功能:
用鼠标或者手指在屏幕上写字并识别输入到文本框
开发环境:
开发工具: Visual Studio 2013
.NET Framework版本:4.5
实现代码:
InkCollector ink;
RecognizerContext recogn;publicinputFrm(){
InitializeComponent();
Control.ControlCollection btnList = panel3.Controls;foreach(Control cinbtnList)
{
(casButton).Click += (s, e) =>
{if(textBox1.Text.Length >0)
{intintStart = textBox1.SelectionStart;
textBox1.Text = textBox1.Text.Remove(intStart -1,1);
textBox1.Text = textBox1.Text.Insert(intStart -1, (sasButton).Text);
textBox1.SelectionStart = intStart;
}
};
}
}privatevoidinputFrm_Load(objectsender, EventArgs e){//创建笔画采集器ink =newInkCollector(picText.Handle);
ink.Enabled =true;
ink.Stroke +=newInkCollectorStrokeEventHandler((s, e1) =>
{
recogn.StopBackgroundRecognition();
recogn.Strokes.Add(e1.Stroke);
recogn.BackgroundRecognizeWithAlternates(0);
});//创建识别器Recognizers recs =newRecognizers();
Recognizer rec = recs.GetDefaultRecognizer();
recogn = rec.CreateRecognizerContext();
recogn.RecognitionFlags = RecognitionModes.WordMode;
recogn.Strokes = ink.Ink.Strokes;
recogn.RecognitionWithAlternates +=newRecognizerContextRecognitionWithAlternatesEventHandler((s, e1) =>
{stringresult ="";
RecognitionAlternates alts;if(e1.RecognitionStatus == RecognitionStatus.NoError)
{
alts = e1.Result.GetAlternatesFromSelection();foreach(RecognitionAlternate altinalts)
{
result += alt.ToString() +" ";
}
}
result = result.Trim();
Control.CheckForIllegalCrossThreadCalls =false;string[] arr = result.Split();for(inti =0; i < arr.Length; i++)
{stringstr ="btnSelect_"+ (i +1);
Control btn = panel3.Controls.Find(str,false).First();
btn.Text = arr[i];
}
Control.CheckForIllegalCrossThreadCalls =true;
});
}privatevoidpicText_MouseUp(objectsender, MouseEventArgs e){
timer1.Enabled =true;
}privatevoidpicText_MouseDown(objectsender, MouseEventArgs e){
timer1.Enabled =false;
}privatevoidtimer1_Tick(objectsender, EventArgs e){
Write();if(!ink.CollectingInk)
{
Strokes strokesToDelete = ink.Ink.Strokes;
recogn.StopBackgroundRecognition();
ink.Ink.DeleteStrokes(strokesToDelete);
recogn.Strokes = ink.Ink.Strokes;
ink.Ink.DeleteStrokes();
picText.Refresh();
}
timer1.Stop();
}privatevoidWrite(){
List lstPoints =newList();foreach(varstrokeinink.Ink.Strokes)
{
lstPoints.AddRange(stroke.GetPoints());
}
ink.Ink.DeleteStrokes();
Stroke sk = ink.Ink.CreateStroke(lstPoints.ToArray());
recogn.Strokes.Add(sk);
textBox1.SelectedText = ink.Ink.Strokes.ToString();
}privatevoidbtnDel_Click(objectsender, EventArgs e){if(!string.IsNullOrEmpty(textBox1.Text))
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length -1);
}privatevoidbtnClear_Click(objectsender, EventArgs e){if(!ink.CollectingInk)
{
Strokes strokesToDelete = ink.Ink.Strokes;
recogn.StopBackgroundRecognition();
ink.Ink.DeleteStrokes(strokesToDelete);
recogn.Strokes = ink.Ink.Strokes;
ink.Ink.DeleteStrokes();
picText.Refresh();
textBox1.Clear();
}
}
实现效果:
最后附上一个关于Microsoft.Ink的API说明文档:https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-3.5/ms581553(v=vs.90),以方便大家更加详细的了解以及使用。
有需要源代码的朋友们可私信:手写输入获取
由简入繁,拿来即用
后续精彩,持续关注