---
title: "基于vc++2008托管代码开发Windows Vista语音识别"
author: "winzenghua"
date: "2009-01-06T11:34:00Z"
tags: []
slug: "vc-2008-windows-vista"
draft: false
lastmod: "2009-01-06T11:34:00Z"
id: 948168
url: "https://java4gis.iteye.com/blog/948168"
---

废话少说看代码

#pragma once  
#include "Window1.g.h"  
#include "Resources.Designer.h"

using namespace System;  
using namespace System::Collections::Generic;  
using namespace System::Text;  
using namespace System::Windows;  
using namespace System::Windows::Controls;  
using namespace System::Windows::Data;  
using namespace System::Windows::Documents;  
using namespace System::Windows::Input;  
using namespace System::Windows::Media;  
using namespace System::Windows::Media::Imaging;  
using namespace System::Windows::Shapes;

using namespace System::Reflection;  
using namespace System::Windows::Threading;  
using namespace System::IO;  
using namespace System::Xml;  
using namespace System::Collections::ObjectModel;  
using namespace System::ComponentModel;

using namespace System::Speech::Recognition;  
using namespace System::Speech::Recognition::SrgsGrammar;  
using namespace System::Speech::Synthesis;

namespace speechReco  
{  
/// &lt;summary&gt;  
/// Interaction logic for Window1.xaml  
/// &lt;/summary&gt;

//ORIGINAL LINE: public partial class Window1 : System.Windows.Window  
//INSTANT C++ TODO TASK: C++ does not support 'partial' types. You must manually combine the entire Window1 type in one place.  
public ref class Window1 : System::Windows::Window  
{  
private:  
SpeechRecognizer ^sharedRecognizer;  
SpeechRecognitionEngine ^appRecognizer;  
SrgsDocument ^sdCmnrules;

public:  
Window1()  
{  
InitializeComponent();

sharedRecognizer = gcnew SpeechRecognizer();  
sharedRecognizer-&gt;AudioLevelUpdated += gcnew EventHandler&lt;AudioLevelUpdatedEventArgs^&gt;(this, &Window1::sharedRecognizer\_AudioLevelUpdated);  
sharedRecognizer-&gt;AudioSignalProblemOccurred += gcnew EventHandler&lt;AudioSignalProblemOccurredEventArgs^&gt;(this, &Window1::sharedRecognizer\_AudioSignalProblemOccurred);  
sharedRecognizer-&gt;AudioStateChanged += gcnew EventHandler&lt;AudioStateChangedEventArgs^&gt;(this, &Window1::sharedRecognizer\_AudioStateChanged);  
sharedRecognizer-&gt;EmulateRecognizeCompleted += gcnew EventHandler&lt;EmulateRecognizeCompletedEventArgs^&gt;(this, &Window1::sharedRecognizer\_EmulateRecognizeCompleted);  
sharedRecognizer-&gt;LoadGrammarCompleted += gcnew EventHandler&lt;LoadGrammarCompletedEventArgs^&gt;(this, &Window1::sharedRecognizer\_LoadGrammarCompleted);  
sharedRecognizer-&gt;RecognizerUpdateReached += gcnew EventHandler&lt;RecognizerUpdateReachedEventArgs^&gt;(this, &Window1::sharedRecognizer\_RecognizerUpdateReached);  
sharedRecognizer-&gt;SpeechDetected += gcnew EventHandler&lt;SpeechDetectedEventArgs^&gt;(this, &Window1::sharedRecognizer\_SpeechDetected);  
sharedRecognizer-&gt;SpeechHypothesized += gcnew EventHandler&lt;SpeechHypothesizedEventArgs^&gt;(this, &Window1::sharedRecognizer\_SpeechHypothesized);  
sharedRecognizer-&gt;SpeechRecognitionRejected += gcnew EventHandler&lt;SpeechRecognitionRejectedEventArgs^&gt;(this, &Window1::sharedRecognizer\_SpeechRecognitionRejected);  
sharedRecognizer-&gt;SpeechRecognized += gcnew EventHandler&lt;SpeechRecognizedEventArgs^&gt;(this, &Window1::sharedRecognizer\_SpeechRecognized);  
sharedRecognizer-&gt;StateChanged += gcnew EventHandler&lt;System::Speech::Recognition::StateChangedEventArgs^&gt;(this, &Window1::sharedRecognizer\_StateChanged);

//load SRGS library  
array&lt;System::Byte&gt; ^ba = speechReco::Properties::Resources::cmnrules;  
MemoryStream ^ms = gcnew MemoryStream(ba);  
ms-&gt;Position = 0;  
XmlReader ^xr = XmlReader::Create(ms);  
sdCmnrules = gcnew SrgsDocument(xr);  
//populate ComboBox  
for each(SrgsRule ^rule in sdCmnrules-&gt;Rules)  
{  
if (rule-&gt;Scope == SrgsRuleScope::Public)  
{  
cbRules-&gt;Items-&gt;Add(rule-&gt;Id);  
}  
}  
//default to integer rule  
cbRules-&gt;SelectedValue = "integer";  
cbRules-&gt;SelectionChanged += gcnew SelectionChangedEventHandler(this, &Window1::cbRules\_SelectionChanged);

this-&gt;btnSharedColor-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnSharedColor\_Click);  
this-&gt;btnInProcColor-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnInProcColor\_Click);  
this-&gt;btnTapDictation-&gt;PreviewMouseLeftButtonDown += gcnew MouseButtonEventHandler(this, &Window1::btnTapDictation\_PreviewMouseLeftButtonDown);  
this-&gt;btnTapDictation-&gt;PreviewMouseLeftButtonUp += gcnew MouseButtonEventHandler(this, &Window1::btnTapDictation\_PreviewMouseLeftButtonUp);  
this-&gt;btnSrgs-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnSrgs\_Click);  
this-&gt;btnAdvGrammarBuilder-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnAdvGrammarBuilder\_Click);  
this-&gt;btnWavFile-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnWavFile\_Click);  
this-&gt;btnSynthPhonemes-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnSynthPhonemes\_Click);  
this-&gt;btnEnable-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnEnable\_Click);  
this-&gt;btnDisable-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnDisable\_Click);  
this-&gt;btnUnload-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnUnload\_Click);  
this-&gt;btnEmulate-&gt;Click += gcnew RoutedEventHandler(this, &Window1::btnEmulate\_Click);  
}

private:  
void btnEmulate\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
//sharedRecognizer.EmulateRecognize("green");  
sharedRecognizer-&gt;EmulateRecognizeAsync("green");  
//sharedRecognizer.EmulateRecognize("stop listening");  
}

void btnUnload\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
sharedRecognizer-&gt;UnloadAllGrammars();  
}

void btnDisable\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
sharedRecognizer-&gt;Enabled = false;  
}

void btnEnable\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
sharedRecognizer-&gt;Enabled = true;  
}

System::String ^recoPhonemes;  
void btnSynthPhonemes\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
//this is a trick to figure out phonemes used by synthesis engine

//txt to wav  
MemoryStream ^audioStream = gcnew MemoryStream();  
SpeechSynthesizer ^synth = gcnew SpeechSynthesizer();  
synth-&gt;SetOutputToWaveStream(audioStream);  
PromptBuilder ^pb = gcnew PromptBuilder();  
pb-&gt;AppendBreak(PromptBreak::ExtraSmall); //'e' wont be recognized if this is large, or non-existent?  
synth-&gt;Speak(pb);  
System::String ^textToSpeak = this-&gt;txtSynthTxt-&gt;Text-&gt;Trim();  
synth-&gt;Speak(textToSpeak);  
//synth.Speak(pb);  
synth-&gt;SetOutputToNull();  
audioStream-&gt;Position = 0;

//now wav to txt (for reco phonemes)  
recoPhonemes = System::String::Empty;  
GrammarBuilder ^gb = gcnew GrammarBuilder(textToSpeak);  
Grammar ^g = gcnew Grammar(gb); //TODO the hard letters to recognize are 'g' and 'e'  
SpeechRecognitionEngine ^reco = gcnew SpeechRecognitionEngine();  
reco-&gt;SpeechHypothesized += gcnew EventHandler&lt;SpeechHypothesizedEventArgs^&gt;(this, &Window1::reco\_SpeechHypothesized);  
reco-&gt;SpeechRecognitionRejected += gcnew EventHandler&lt;SpeechRecognitionRejectedEventArgs^&gt;(this, &Window1::reco\_SpeechRecognitionRejected);  
reco-&gt;UnloadAllGrammars(); //only use the one word grammar  
reco-&gt;LoadGrammar(g);  
reco-&gt;SetInputToWaveStream(audioStream);  
RecognitionResult ^rr = reco-&gt;Recognize();  
reco-&gt;SetInputToNull();  
if (rr != nullptr)  
{  
recoPhonemes = StringFromWordArray(rr-&gt;Words, WordType::Pronunciation);  
}  
txtRecoPho-&gt;Text = recoPhonemes;  
}

void reco\_SpeechRecognitionRejected(System::Object ^sender, SpeechRecognitionRejectedEventArgs ^e)  
{  
recoPhonemes = StringFromWordArray(e-&gt;Result-&gt;Words, WordType::Pronunciation);  
}

void reco\_SpeechHypothesized(System::Object ^sender, SpeechHypothesizedEventArgs ^e)  
{  
recoPhonemes = StringFromWordArray(e-&gt;Result-&gt;Words, WordType::Pronunciation);  
}

void btnWavFile\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
sharedRecognizer-&gt;Enabled = false;

appRecognizer = gcnew SpeechRecognitionEngine();  
appRecognizer-&gt;SetInputToWaveFile("spoken.wav");  
appRecognizer-&gt;LoadGrammar(gcnew DictationGrammar());  
RecognitionResult ^rr = appRecognizer-&gt;Recognize();  
appRecognizer-&gt;SetInputToNull();  
if (rr == nullptr)  
{  
MessageBox::Show("null result?");  
}  
else  
{  
//NOTE in-process recognir cannot send feedback to microphone bar  
//SpeechUI.SendTextFeedback(rr, rr.Text, true);

//show phoneme result  
System::String ^phonemes = StringFromWordArray(rr-&gt;Words, WordType::Pronunciation);  
txtRecoPho-&gt;Text = phonemes;

//show text result  
MessageBox::Show(rr-&gt;Text);  
}  
delete appRecognizer;  
}

public:  
enum class WordType  
{  
Text,  
Normalized = Text,  
Lexical,  
Pronunciation  
};

public:  
static System::String ^StringFromWordArray(ReadOnlyCollection&lt;RecognizedWordUnit^&gt; ^words, WordType type)  
{  
System::String ^text = "";  
for each (RecognizedWordUnit ^word in words)  
{  
System::String ^wordText = "";  
if (type == WordType::Text || type == WordType::Normalized)  
{  
wordText = word-&gt;Text;  
}  
else if (type == WordType::Lexical)  
{  
wordText = word-&gt;LexicalForm;  
}  
else if (type == WordType::Pronunciation)  
{  
wordText = word-&gt;Pronunciation;  
}  
else  
{  
throw gcnew InvalidEnumArgumentException(System::String::Format("[0}: is not a valid input", type));  
}  
//Use display attribute

if ((word-&gt;DisplayAttributes & DisplayAttributes::OneTrailingSpace) != 0)  
{  
wordText += " ";  
}  
if ((word-&gt;DisplayAttributes & DisplayAttributes::TwoTrailingSpaces) != 0)  
{  
wordText += " ";  
}  
if ((word-&gt;DisplayAttributes & DisplayAttributes::ConsumeLeadingSpaces) != 0)  
{  
wordText = wordText-&gt;TrimStart();  
}  
if ((word-&gt;DisplayAttributes & DisplayAttributes::ZeroTrailingSpaces) != 0)  
{  
wordText = wordText-&gt;TrimEnd();  
}

text += wordText;

}  
return text;  
}

private:  
void btnAdvGrammarBuilder\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
sharedRecognizer-&gt;Enabled = true;  
sharedRecognizer-&gt;UnloadAllGrammars();

//from [http://msdn.microsoft.com/msdnmag/issues/06/01/speechinWindowsVista/#S5](http://msdn.microsoft.com/msdnmag/issues/06/01/speechinWindowsVista/#S5)  
//\[I'd like] a \[&lt;size&gt;] \[&lt;crust&gt;] \[&lt;topping&gt;] pizza \[please]

//build the core set of choices  
Choices ^sizes = gcnew Choices("small", "regular", "large");  
Choices ^crusts = gcnew Choices("thin crust", "thick crust");  
Choices ^toppings = gcnew Choices("vegetarian", "pepperoni", "cheese");

SemanticResultKey ^srkSize = gcnew SemanticResultKey("size", sizes-&gt;ToGrammarBuilder());  
SemanticResultKey ^srkCrust = gcnew SemanticResultKey("crust", crusts-&gt;ToGrammarBuilder());  
SemanticResultKey ^srkTopping = gcnew SemanticResultKey("topping", toppings-&gt;ToGrammarBuilder());  
SemanticResultValue ^srvSize = gcnew SemanticResultValue(srkSize, "regular");  
SemanticResultValue ^srvCrust = gcnew SemanticResultValue(srkCrust, "thick crust");

//build the permutations of choices...  
//choose all three  
GrammarBuilder ^sizeCrustTopping = gcnew GrammarBuilder();  
//sizeCrustTopping.AppendChoices(sizes, "size");  
//sizeCrustTopping.AppendChoices(crusts, "crust");  
//sizeCrustTopping.AppendChoices(toppings, "topping");  
sizeCrustTopping-&gt;Append(srkSize);  
sizeCrustTopping-&gt;Append(srkCrust);  
sizeCrustTopping-&gt;Append(srkTopping);

//choose size and topping, and assume thick crust  
GrammarBuilder ^sizeAndTopping = gcnew GrammarBuilder();  
//sizeAndTopping.AppendChoices(sizes, "size");  
//sizeAndTopping.AppendChoices(toppings, "topping");  
//sizeAndTopping.AppendResultKeyValue("crust", "thick crust");  
sizeAndTopping-&gt;Append(srkSize);  
sizeAndTopping-&gt;Append(srkTopping);  
//TODO how to set default semantic value for "crust"?  
//sizeAndTopping.Append(srvCrust);  
//sizeAndTopping.Append(new SemanticResultValue(crusts.ToGrammarBuilder(), "thick crust"));  
//sizeAndTopping.Append(new SemanticResultValue("crust", "thick crust"));  
//sizeAndTopping.Append(new SemanticResultValue("thick crust"));  
//sizeAndTopping.Append(new SemanticResultKey("crust", "thick crust"));

//choose topping only, and assume the rest  
GrammarBuilder ^toppingOnly = gcnew GrammarBuilder();  
//toppingOnly.AppendChoices(toppings, "topping");  
//toppingOnly.AppendResultKeyValue("size", "regular");  
//toppingOnly.AppendResultKeyValue("crust", "thick crust");  
toppingOnly-&gt;Append(srkTopping);  
//TODO how to set default semantic value for "size" and "crust"?  
//toppingOnly.Append(srvSize);  
//toppingOnly.Append(srvCrust);  
//toppingOnly.Append(new SemanticResultKey("size", "regular"));  
//toppingOnly.Append(new SemanticResultKey("crust", "thick crust"));

//assemble the permutations  
Choices ^permutations = gcnew Choices();  
permutations-&gt;Add(sizeCrustTopping);  
permutations-&gt;Add(sizeAndTopping);  
permutations-&gt;Add(toppingOnly);

//now build the complete pattern...  
GrammarBuilder ^pizzaRequest = gcnew GrammarBuilder();  
//pre-amble "\[I'd like] a"  
pizzaRequest-&gt;Append(gcnew Choices("I'd like a", "a"));  
//permutations "\[&lt;size&gt;] \[&lt;crust&gt;] \[&lt;topping&gt;]"  
pizzaRequest-&gt;Append(permutations);  
//post-amble "pizza \[please]"  
pizzaRequest-&gt;Append(gcnew Choices("pizza", "pizza please"));  
System::String ^debug = pizzaRequest-&gt;DebugShowPhrases;

//create the pizza grammar  
Grammar ^pizzaGrammar = gcnew Grammar(pizzaRequest);

//attach the event handler  
pizzaGrammar-&gt;SpeechRecognized += gcnew EventHandler&lt;SpeechRecognizedEventArgs^&gt;(this, &Window1::pizzaGrammar\_SpeechRecognized);

//load the grammar into the recognizer  
sharedRecognizer-&gt;LoadGrammar(pizzaGrammar);

}

void pizzaGrammar\_SpeechRecognized(System::Object ^sender, SpeechRecognizedEventArgs ^e)  
{  
StringBuilder ^resultString = gcnew StringBuilder();  
resultString-&gt;Append("Raw text result: ");  
resultString-&gt;AppendLine(e-&gt;Result-&gt;Text);  
resultString-&gt;Append("Size: ");  
resultString-&gt;AppendLine(e-&gt;Result-&gt;Semantics\["size"]-&gt;Value-&gt;ToString());  
resultString-&gt;Append("Crust: ");  
resultString-&gt;AppendLine(e-&gt;Result-&gt;Semantics\["crust"]-&gt;Value-&gt;ToString());  
resultString-&gt;Append("Topping: ");  
resultString-&gt;AppendLine(e-&gt;Result-&gt;Semantics\["topping"]-&gt;Value-&gt;ToString());  
MessageBox::Show(resultString-&gt;ToString());  
}

void cbRules\_SelectionChanged(System::Object ^sender, SelectionChangedEventArgs ^e)  
{  
//TODO  
}

void btnSrgs\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
sharedRecognizer-&gt;Enabled = true;  
sharedRecognizer-&gt;UnloadAllGrammars();

System::String ^ruleName = safe\_cast&lt;System::String^&gt;(cbRules-&gt;SelectedValue);  
//SrgsRule rule = sdCmnrules.Rules\[ruleName];

Grammar ^grammarSrgs = gcnew Grammar(sdCmnrules, ruleName);  
grammarSrgs-&gt;SpeechRecognized += gcnew EventHandler&lt;SpeechRecognizedEventArgs^&gt;(this, &Window1::grammarSrgs\_SpeechRecognized);

sharedRecognizer-&gt;LoadGrammar(grammarSrgs);  
MessageBox::Show("listening for user input based on the selected rule : " + ruleName);  
}

void grammarSrgs\_SpeechRecognized(System::Object ^sender, SpeechRecognizedEventArgs ^e)  
{  
//send text to microphone bar  
SpeechUI::SendTextFeedback(e-&gt;Result, e-&gt;Result-&gt;Text, true);  
//send actual numeric value to TextBox on form  
if (e-&gt;Result-&gt;Semantics-&gt;Value != nullptr)  
{  
this-&gt;Dispatcher-&gt;Invoke(DispatcherPriority::Render, gcnew UpdateTxtRecoDelegate(UpdateTextReco), e-&gt;Result-&gt;Semantics-&gt;Value-&gt;ToString());  
}  
}

void btnTapDictation\_PreviewMouseLeftButtonDown(System::Object ^sender, MouseButtonEventArgs ^e)  
{  
sharedRecognizer-&gt;Enabled = false;

dictationResult = System::String::Empty;  
appRecognizer = gcnew SpeechRecognitionEngine();  
appRecognizer-&gt;SetInputToDefaultAudioDevice();  
appRecognizer-&gt;SpeechRecognized += gcnew EventHandler&lt;SpeechRecognizedEventArgs^&gt;(this, &Window1::appRecognizer\_SpeechRecognized);  
DictationGrammar ^dg;  
if (cbSpelling-&gt;IsChecked == false)  
{  
dg = gcnew DictationGrammar();  
}  
else  
{  
dg = gcnew DictationGrammar("grammar:dictation#spelling");  
}  
appRecognizer-&gt;LoadGrammar(dg);  
appRecognizer-&gt;RecognizeAsync(RecognizeMode::Multiple);  
}

System::String ^dictationResult;  
void appRecognizer\_SpeechRecognized(System::Object ^sender, SpeechRecognizedEventArgs ^e)  
{  
//on UI thread  
dictationResult += e-&gt;Result-&gt;Text;  
txtReco-&gt;Text = dictationResult;  
}

void btnTapDictation\_PreviewMouseLeftButtonUp(System::Object ^sender, MouseButtonEventArgs ^e)  
{  
appRecognizer-&gt;RecognizeAsyncStop();  
delete appRecognizer;  
}

void btnInProcColor\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
sharedRecognizer-&gt;Enabled = false;

Choices ^cColor = GetColorChoices();

GrammarBuilder ^gb = gcnew GrammarBuilder(cColor);  
Grammar ^grammarColors = gcnew Grammar(gb);  
grammarColors-&gt;SpeechRecognized += gcnew EventHandler&lt;SpeechRecognizedEventArgs^&gt;(this, &Window1::grammarColors\_SpeechRecognized);

appRecognizer = gcnew SpeechRecognitionEngine();  
appRecognizer-&gt;SetInputToDefaultAudioDevice();  
appRecognizer-&gt;LoadGrammar(grammarColors);  
appRecognizer-&gt;LoadGrammar(gcnew DictationGrammar());  
appRecognizer-&gt;RecognizeAsync(RecognizeMode::Multiple);

MessageBox::Show("listening for you to say a color (e.g. Green)");  
}

Choices ^GetColorChoices()  
{  
//build a grammar list of colors  
Choices ^cColor = gcnew Choices();

Type ^t = Colors::typeid;  
array&lt;MemberInfo^&gt; ^mia = t-&gt;GetMembers(BindingFlags::Public | BindingFlags::Static);  
for each (MemberInfo ^mi in mia)  
{  
if (mi-&gt;Name-&gt;StartsWith("get\_") == true)  
continue;  
cColor-&gt;Add(mi-&gt;Name);  
}

return cColor;  
}

void btnSharedColor\_Click(System::Object ^sender, RoutedEventArgs ^e)  
{  
sharedRecognizer-&gt;Enabled = true;  
sharedRecognizer-&gt;UnloadAllGrammars();

Choices ^cColor = GetColorChoices();

GrammarBuilder ^gb = gcnew GrammarBuilder(cColor);  
Grammar ^grammarColors = gcnew Grammar(gb);  
grammarColors-&gt;SpeechRecognized += gcnew EventHandler&lt;SpeechRecognizedEventArgs^&gt;(this, &Window1::grammarColors\_SpeechRecognized);

sharedRecognizer-&gt;LoadGrammar(grammarColors);  
MessageBox::Show("listening for you to say a color (e.g. Green)");  
}

void grammarColors\_SpeechRecognized(System::Object ^sender, SpeechRecognizedEventArgs ^e)  
{  
//not on UI thread  
//txtReco.Text = e.Result.Text;  
//need to use Dispatcher to get back on UI thread

//TODO cannot convert from 'anonymous method' to 'System.Delegate' ... WTF?  
//this.Dispatcher.Invoke(DispatcherPriority.Render,  
// delegate()  
// {  
// txtReco.Text = e.Result.Text;  
// });

//http://romanski.livejournal.com/1761.html  
//INSTANT C++ TODO TASK: Anonymous methods are not converted by Instant C++ C# Edition if local variables of the outer method are referenced within the anonymous method:  
this-&gt;Dispatcher-&gt;Invoke(DispatcherPriority::Render, (System::Windows::Forms::MethodInvoker) delegate(System::Object^ sender, System::EventArgs e));  
{  
txtReco-&gt;Text = e-&gt;Result-&gt;Text;  
}

//this.Dispatcher.Invoke(DispatcherPriority.Render, new UpdateTxtRecoDelegate(UpdateTextReco), e.Result.Text);  
}

delegate void UpdateTxtRecoDelegate(System::String ^arg);  
public:  
void UpdateTextReco(System::String ^arg)  
{  
txtReco-&gt;Text = arg;  
}

#pragma region SHARED\_RECOGNIZER\_EVENTS  
private:  
void sharedRecognizer\_StateChanged(System::Object ^sender, System::Speech::Recognition::StateChangedEventArgs ^e)  
{  
System::Console::WriteLine("StateChanged : " + e-&gt;RecognizerState.ToString());  
}

void sharedRecognizer\_SpeechRecognized(System::Object ^sender, SpeechRecognizedEventArgs ^e)  
{  
//on UI thread  
System::Console::WriteLine("SpeechRecognized : " + e-&gt;Result-&gt;Text);  
//txtReco.Text = e.Result.Text;  
}

void sharedRecognizer\_SpeechRecognitionRejected(System::Object ^sender, SpeechRecognitionRejectedEventArgs ^e)  
{  
System::Console::WriteLine("SpeechRecognitionRejected : " + e-&gt;Result-&gt;Text);  
}

void sharedRecognizer\_SpeechHypothesized(System::Object ^sender, SpeechHypothesizedEventArgs ^e)  
{  
System::Console::WriteLine("SpeechHypothesized : " + e-&gt;Result-&gt;Text);  
}

void sharedRecognizer\_SpeechDetected(System::Object ^sender, SpeechDetectedEventArgs ^e)  
{  
System::Console::WriteLine("SpeechDetected : " + e-&gt;AudioPosition.TotalMilliseconds.ToString());  
}

void sharedRecognizer\_RecognizerUpdateReached(System::Object ^sender, RecognizerUpdateReachedEventArgs ^e)  
{  
System::Console::WriteLine("RecognizerUpdateReached : " + e-&gt;AudioPosition.TotalMilliseconds.ToString());  
}

void sharedRecognizer\_LoadGrammarCompleted(System::Object ^sender, LoadGrammarCompletedEventArgs ^e)  
{  
System::Console::WriteLine("LoadGrammarCompleted : " + e-&gt;Grammar-&gt;Name);  
}

void sharedRecognizer\_EmulateRecognizeCompleted(System::Object ^sender, EmulateRecognizeCompletedEventArgs ^e)  
{  
if (e-&gt;Result != nullptr)  
{  
System::Console::WriteLine("EmulateRecognizeCompleted : " + e-&gt;Result-&gt;Text);  
}  
else  
{  
System::Console::WriteLine("EmulateRecognizeCompleted : null result");  
}  
}

void sharedRecognizer\_AudioStateChanged(System::Object ^sender, AudioStateChangedEventArgs ^e)  
{  
System::Console::WriteLine("AudioStateChanged : " + e-&gt;AudioState.ToString());  
}

void sharedRecognizer\_AudioSignalProblemOccurred(System::Object ^sender, AudioSignalProblemOccurredEventArgs ^e)  
{  
System::Console::WriteLine("AudioSignalProblemOccurred : " + e-&gt;AudioSignalProblem.ToString());  
}

void sharedRecognizer\_AudioLevelUpdated(System::Object ^sender, AudioLevelUpdatedEventArgs ^e)  
{  
//System.Console.WriteLine("AudioLevelUpdated : " + e.AudioLevel.ToString());  
}  
#pragma endregion

};  
}

需要的留下eMAIL，我给大家发