Handwriting input under wpf

Posted by ramrod737 on Wed, 01 Jan 2020 19:04:13 +0100

The origin of the problem, referring to the handwriting input under winform, realizes the same function under wpf. At last, it is found that the handwritten input cannot be done in the characteristic area. The reason for the problem is that the handle of the control obtained under wpf is the same as that of the whole form.

Finally, the InkCanvas under wpf can be used for handwriting, and then its ink can be converted into Microsoft's ink class:

<InkCanvas   Name="inkInput"  Grid.Column="1"  StrokeCollected="inkInput_StrokeCollected"> 
</InkCanvas>


private RecognizerContext rct;

private void inkInput_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
        { 

            MemoryStream ms = new MemoryStream();
            inkInput.Strokes.Save(ms);
            InkCollector myInkCollector = new InkCollector();
            Ink ink = new Ink();
            ink.Load(ms.ToArray());


            rct.StopBackgroundRecognition();

            rct.Strokes = ink.Strokes;
            // rct.CharacterAutoCompletion = RecognizerCharacterAutoCompletionMode.Full;

            rct.BackgroundRecognizeWithAlternates(0);


            timer.Start();
        }

Initialize an event at the beginning of the page

 private void WpfWorkflowModule_Loaded(object sender, RoutedEventArgs e)
 { 
   ink_Here(); 
 }

 private void ink_Here()
 {
            Recognizers recos = new Recognizers();
            Recognizer chineseReco = recos.GetDefaultRecognizer();

            rct = chineseReco.CreateRecognizerContext();
            rct.RecognitionFlags = Microsoft.Ink.RecognitionModes.WordMode;

            this.rct.RecognitionWithAlternates += new RecognizerContextRecognitionWithAlternatesEventHandler(rct_RecognitionWithAlternates);
 }

The treatment of ink recognition

 

private void rct_RecognitionWithAlternates(object sender, RecognizerContextRecognitionWithAlternatesEventArgs e)
{ 

	 
	if (!Dispatcher.CheckAccess())
	{

		Dispatcher.Invoke(DispatcherPriority.Send, new RecognizerContextRecognitionWithAlternatesEventHandler(rct_RecognitionWithAlternates), sender, e);

		return;
	}

  
	if (RecognitionStatus.NoError == e.RecognitionStatus)
	{
		this.panelChoose.Children.Clear();
		RecognitionAlternates allAlternates = e.Result.GetAlternatesFromSelection();
		// show each of the other alternates
		int i = 1;
		int width = 48;
		int height = 48; 
		foreach (RecognitionAlternate oneAlternate in allAlternates)
		{
			Label lbl = new Label();
			lbl.Name = "lbl" + i;
			lbl.Tag = oneAlternate.ToString();
			lbl.Content = lbl.Tag.ToString();
			lbl.Width = width;
			lbl.Height = height;
			lbl.FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei");
			lbl.FontSize = 24F;
			lbl.BorderThickness = new Thickness(1);
			lbl.Margin = new Thickness(2, 0, 0, 0);
			lbl.BorderBrush =System.Windows.Media. Brushes.Gray;
			lbl.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(lbl_MouseLeftButtonDown);
	   

			 
			this.panelChoose.Children.Add(lbl);
			++i; 
		}


	}
   

}

 

Topics: Windows