最近在做C#手写输入功能,在网上找了个demo,用microsoft.ink.dll来做的,我把这个控件设计好加入到自己的控件中,运行一段时间就会报out of stack space 的错,想请教各位是哪里出了问题,或者如何能定位到问题,手写板控件定义相关代码如下
public partial class HandWrite : Control
{
public static FrmMain _FrmMain; //自己的主面板
InkCollector ic;
RecognizerContext rct;
string FullCACText;
public HandWrite()
{
InitializeComponent();
ic = new InkCollector(ink_here.Handle);
this.ic.Stroke += new InkCollectorStrokeEventHandler(ic_Stroke);
ic.Enabled = true;
ink_();
this.rct.RecognitionWithAlternates += new RecognizerContextRecognitionWithAlternatesEventHandler(rct_RecognitionWithAlternates);
//单字识别
rct.RecognitionFlags = RecognitionModes.WordMode;
rct.Strokes = ic.Ink.Strokes;
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
void rct_RecognitionWithAlternates(object sender, RecognizerContextRecognitionWithAlternatesEventArgs e)
{
string ResultString = "";
RecognitionAlternates alts;
if (e.RecognitionStatus == RecognitionStatus.NoError)
{
alts = e.Result.GetAlternatesFromSelection();
Console.WriteLine(alts.Count);
for (int i = 0; i < alts.Count && i < 5; i++) // 取前5个识别结果
{
RecognitionAlternate alt = alts[i];
ResultString += alt.ToString() + " ";
}
//foreach (RecognitionAlternate alt in alts)
//{
// ResultString = ResultString + alt.ToString() + " ";
//}
}
FullCACText = ResultString.Trim();
Console.WriteLine(FullCACText);
Control.CheckForIllegalCrossThreadCalls = false;
GetString(FullCACText);
Control.CheckForIllegalCrossThreadCalls = true;
}
private void GetString(string txt)
{
string[] str_temp = txt.Split(' ');
string str_temp1 = "shibie_";
string str_temp2 = "";
if (str_temp.Length > 0)
{
for (int i = 0; i < str_temp.Length; i++)
{
str_temp2 = str_temp1 + i.ToString();
Control[] con_temp = tlpBox.Controls.Find(str_temp2, true);
if (con_temp.Length > 0)
{
((Button)(con_temp[0])).Text = str_temp[i];
//Label lab = (Label)(con_temp[0]);
//lab. = str_temp[i];
}
}
}
}
void ic_Stroke(object sender, InkCollectorStrokeEventArgs e)
{
rct.StopBackgroundRecognition();
rct.Strokes.Add(e.Stroke);
rct.BackgroundRecognizeWithAlternates(0);
}
private void ink_()
{
Recognizers recos = new Recognizers();
Recognizer chineseReco = recos.GetDefaultRecognizer();
rct = chineseReco.CreateRecognizerContext();
}
private void btn_shibie_Click(object sender, EventArgs e)
{
#region 单字识别处理
List<Point> lstPoints = new List<Point>();
foreach (var stroke in ic.Ink.Strokes)
{
lstPoints.AddRange(stroke.GetPoints());
}
ic.Ink.DeleteStrokes();//清除手写区域笔画;
Stroke sk = ic.Ink.CreateStroke(lstPoints.ToArray());
rct.Strokes.Add(sk);
#endregion
//_FrmMain.tbKeyboardInput.Text= ic.Ink.Strokes.ToString();
//textBox1.SelectedText = ic.Ink.Strokes.ToString();
FrmMain._FrmMain.tbKeyboardInput.SelectedText = ic.Ink.Strokes.ToString();
}
private void handTimer_Tick(object sender, EventArgs e)
{
btn_shibie.PerformClick();
if (!ic.CollectingInk)
{
Strokes strokesToDelete = ic.Ink.Strokes;
rct.StopBackgroundRecognition();
ic.Ink.DeleteStrokes(strokesToDelete);
rct.Strokes = ic.Ink.Strokes;
ic.Ink.DeleteStrokes();//清除手写区域笔画;
ink_here.Refresh();//刷新手写区域
}
handTimer.Stop();
}
private void ink_here_MouseUp(object sender, MouseEventArgs e)
{
handTimer.Enabled = true;
}
private void ink_here_MouseDown(object sender, MouseEventArgs e)
{
handTimer.Enabled = false;
}
}
调用如下
//定义子窗口变量
public HandWrite hw;
//初始化
internal void InitHandWrting(Control control, string strLInput, string strLTip, bool isPassword)
{
// 解决单击tlp ImeMode失效
control.Focus();
hw = new HandWrite();
hw.Name = "handwrite";
hw.Dock = DockStyle.Fill;
tlpKeyboard.Controls.Add(hw);
//hw.Hand_Load();
ParamControl = control;
gbKeyboardResult.Text = strLInput + ":" + strLTip;
tbKeyboardInput.Multiline = !isPassword;
tbKeyboardInput.UseSystemPasswordChar = isPassword;
//tbKeyboardInput.ImeMode = control.ImeMode;
tbKeyboardInput.Text = control.Text;
}
//调用
private void tbNewTestName_Click(object sender, EventArgs e)
{
FrmMain._FrmMain.InitHandWrting(tbNewTestName,Properties.Resources.InfoInput_Title_NewTestName, Properties.Resources.InfoInput_Info_NewTestName, false);
FrmMain._FrmMain.ShowPKeyboard();
}