在如今这个时代,对这个功能的需求量可能已经很少了,但是这也不妨碍我们或多或少的会碰到一些稀奇古怪的需求,多接触一些总归是没错的。

c语言书写教学(C实现手写输入功能)(1)

本篇主要是用到了Microsoft.Ink来实现一个手写输入,也就是类似于手机输入法中的手写功能。

实现功能:

用鼠标或者手指在屏幕上写字并识别输入到文本框

开发环境:

开发工具: Visual Studio 2013

.NET Framework版本:4.5

实现代码:

InkCollector ink; RecognizerContext recogn; public inputFrm() { InitializeComponent(); Control.ControlCollection btnList = panel3.Controls; foreach (Control c in btnList) { (c as Button).Click = (s, e) => { if (textBox1.Text.Length > 0) { int intStart = textBox1.SelectionStart; textBox1.Text = textBox1.Text.Remove(intStart - 1, 1); textBox1.Text = textBox1.Text.Insert(intStart - 1, (s as Button).Text); textBox1.SelectionStart = intStart; } }; } } private void inputFrm_Load(object sender, EventArgs e) { //创建笔画采集器 ink = new InkCollector(picText.Handle); ink.Enabled = true; ink.Stroke = new InkCollectorStrokeEventHandler((s, e1) => { recogn.StopBackgroundRecognition(); recogn.Strokes.Add(e1.Stroke); recogn.BackgroundRecognizeWithAlternates(0); }); //创建识别器 Recognizers recs = new Recognizers(); Recognizer rec = recs.GetDefaultRecognizer(); recogn = rec.CreateRecognizerContext(); recogn.RecognitionFlags = RecognitionModes.WordMode; recogn.Strokes = ink.Ink.Strokes; recogn.RecognitionWithAlternates = new RecognizerContextRecognitionWithAlternatesEventHandler((s, e1) => { string result = ""; RecognitionAlternates alts; if (e1.RecognitionStatus == RecognitionStatus.NoError) { alts = e1.Result.GetAlternatesFromSelection(); foreach (RecognitionAlternate alt in alts) { result = alt.ToString() " "; } } result = result.Trim(); Control.CheckForIllegalCrossThreadCalls = false; string[] arr = result.Split(' '); for (int i = 0; i < arr.Length; i ) { string str = "btnSelect_" (i 1); Control btn = panel3.Controls.Find(str, false).First(); btn.Text = arr[i]; } Control.CheckForIllegalCrossThreadCalls = true; }); } private void picText_MouseUp(object sender, MouseEventArgs e) { timer1.Enabled = true; } private void picText_MouseDown(object sender, MouseEventArgs e) { timer1.Enabled = false; } private void timer1_Tick(object sender, 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(); } private void Write() { List<Point> lstPoints = new List<Point>(); foreach (var stroke in ink.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(); } private void btnDel_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(textBox1.Text)) textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1); } private void btnClear_Click(object sender, 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(); } }

实现效果:

c语言书写教学(C实现手写输入功能)(2)

最后附上一个关于Microsoft.Ink的API说明文档:https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-3.5/ms581553(v=vs.90),以方便大家更加详细的了解以及使用。

有需要源代码的朋友们可私信:手写输入 获取

由简入繁,拿来即用

后续精彩,持续关注

,