
Introduction
This article guides you to create a simple COM client using MFC, and following the same
method we can create clients for any Component when a type library file (.tlb) is available.
Note: On any machines with Windows 2000 Installed check for a "Speech" folder under
C:\WINNT.
This folder contains all the necessary components to call speech engine.
So to start, fire up the VC++ 6.0 IDE and then select File->New, select
the MFC AppWizard [exe] and Enter "SpeakText" as the Project name and save it in
the required location. Select "Dialog Base" option and uncheck the "Document/View
Architecture".
Design the user interface dialog as shown in the figure above using resource editor and name your dialog and its controls accordingly.
Note: In edit control properties, on the Styles tab select "Multiline" check box for typing multiple lines of text.
Add the following in "stdafx.h"
to import the Voice Engine Component
...
#include // MFC support for Windows Common Controls
#endif
#import "C:\WINNT\Speech\vtxtauto.tlb" no_namespace
...
Compile "stdafx.cpp"
file, which results in creation of two new files (1) vtxtauto.tlh,
(2) vtxtauto.tli
in the Debug/Release folder . These files give interface IVTxtAuto
wrapper method implementations.
Browse through SPEAKFLAGS
structure present in vtxtauto.tlh
and the other methods.
Now is the time to call the engine through the IVTxtAuto
Interface.
Add these variables in "SpeakTextDlg.h"
char sText[1000];
CLSID clsid;
HRESULT hr;
IVTxtAuto *voicePtr;
Add the following piece of code in OnInitDialog()
function
CSpeakTextDlg::OnInitDialog()
{
...
hr = CoInitialize(NULL);
hr = CLSIDFromProgID(OLESTR("Speech.VoiceText.1"), &clsid);
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, __uuidof(IVTxtAuto),
(LPVOID*)&voicePtr);
if (FAILED(hr))
{
AfxMessageBox("Cannot Initialize Speech Engine");
return FALSE;
}
else
{
_bstr_t pszSite;
voicePtr->Register(pszSite,"Radio");
voicePtr->Speak("Welcome to Text to Voice Convertion Engine",vtxtsp_NORMAL);
voicePtr->Speak("Type your Text and ask me to speak",vtxtsp_NORMAL);
}
...
}
Add the following onclick handler for the "Speak" button
CSpeakTextDlg::OnSpeak()
{
...
GetDlgItemText(IDC_TEXT, sText, 200);
if (0 == strcmp(sText,"\0") )
voicePtr->Speak("What can i speak for you",vtxtsp_NORMAL);
else
voicePtr->Speak(sText,vtxtsp_NORMAL);
...
}
Change the second parameter of Speak Function according to the value of
the SPEAKFLAGS
structure present in vtxtauto.tlh
. Add the following code for respective buttons
void CSpeakTextDlg::OnPause()
{
voicePtr->AudioPause();
}
void CSpeakTextDlg::OnResume()
{
voicePtr->AudioResume();
}
CSpeakTextDlg::OnStop()
{
voicePtr->StopSpeaking();
}
That's all. Build and Run the application.