📄 ch19.htm
字号:
BOOL BeginOLE (void)<br>
{<br>
DWORD dwVer;<br>
<br>
// Initialize OLE<br>
<br>
SetMessageQueue(96);<br>
dwVer = CoBuildVersion();<br>
<br>
if (rmm != HIWORD(dwVer)) return FALSE; //
error<br>
<br>
if (FAILED(CoInitialize(NULL))) return FALSE; <br>
<br>
return TRUE;<br>
}<br>
<br>
<br>
/************************************************************************* <br>
EndOLE - This closes up the OLE.<br>
<br>
inputs<br>
none<br>
returns<br>
BOOL - TRUE if succede<br>
*/<br>
<br>
BOOL EndOLE (void)<br>
{<br>
// Free up all of OLE<br>
<br>
CoUninitialize ();<br>
<br>
return TRUE;<br>
}</font></tt> </p>
</blockquote>
<hr>
<p>The code in Listing 19.3 shows three steps in the <tt><font FACE="Courier">BeginOLE</font></tt>
routine. The first line creates a message queue that can hold up to 96 messages. The next
two lines check the OLE version, and the last line actually initializes the OLE session.
The <tt><font FACE="Courier">EndOLE</font></tt> session simply releases the OLE session
you created in <tt><font FACE="Courier">BeginOLE</font></tt>. </p>
<h4>Selecting the TTS Engine Object</h4>
<p>After creating the OLE session, you need to locate and select a valid TTS engine
object. There are just a few steps to the process. First, you need a few local variables
to keep track of your progress. Listing 19.4 shows the initial declarations for the <tt><font
FACE="Courier">FindAndSelect</font></tt> procedure. </p>
<hr>
<blockquote>
<b><p>Listing 19.4. Declarations for the <tt><font FACE="Courier">FindAndSelect</font></tt>
procedure.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>/*************************************************************************
<br>
FindAndSelect - This finds and selects according to the specific TTSMODEINFOW.<br>
<br>
inputs<br>
PTTSMODEINFOW pTTSInfo - desired mode<br>
returns<br>
PITTSCENTRAL - ISRCentral interface to TTS engine <br>
sets:<br>
<br>
*/<br>
<br>
PITTSCENTRALW FindAndSelect (PTTSMODEINFOW pTTSInfo)<br>
{<br>
HRESULT hRes; <br>
TTSMODEINFOW ttsResult; // final
result<br>
WchAR Zero =
0;<br>
PITTSFINDW pITTSFind; // find
interface<br>
PIAUDIOMULTIMEDIADEVICE pIAMM; // multimedia
device interface for audio-dest<br>
PITTSCENTRALW pITTSCentral; // central
interface</font></tt> </p>
</blockquote>
<hr>
<p>Next, you need to create an instance of the <tt><font FACE="Courier">TTSFind</font></tt>
object. This will be used to select an available TTS engine. Listing 19.5 shows how this
is done. </p>
<hr>
<blockquote>
<b><p>Listing 19.5. Creating the <tt><font FACE="Courier">TTSFind</font></tt> object.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>hRes = CoCreateInstance(CLSID_TTSEnumerator, NULL, CLSCTX_ALL,
IID_ITTSFindW,<br>
&nbs
p; (void**)&pITTSFind); <br>
if (FAILED(hRes)) return NULL;<br>
<br>
hRes = pITTSFind->Find(pTTSInfo, NULL, &ttsResult); <br>
<br>
if (hRes)<br>
{<br>
pITTSFind->Release();<br>
return NULL; // error
<br>
}</font></tt> </p>
</blockquote>
<hr>
<p>The next step is to locate and select an audio output object. This will be used by the
TTS engine for playback of the text. Listing 19.6 shows the code needed to select an
available audio device. </p>
<hr>
<blockquote>
<b><p>Listing 19.6. Selecting an available audio device.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>// Get the audio dest<br>
hRes = CoCreateInstance(CLSID_MMAudioDest, NULL, CLSCTX_ALL, <font
FACE="ZAPFDINGBATS">Â</font>IID_IAudioMultiMediaDevice,(void**)&pIAMM); <br>
if (hRes)<br>
{<br>
pITTSFind->Release;<br>
return NULL; // error<br>
}<br>
pIAMM->DeviceNumSet (WAVE_MappeR);</font></tt> </p>
</blockquote>
<hr>
<p>The code in Listing 19.6 uses the <tt><font FACE="Courier">DeviceNumSet</font></tt>
method of the <tt><font FACE="Courier">MMAudioDest</font></tt> interface to find the
available WAVE output device for the pc. </p>
<p>Once you have successfully created the <tt><font FACE="Courier">TSFind</font></tt>
object and the <tt><font FACE="Courier">MMAudioDest</font></tt> object, you're ready to
use the <tt><font FACE="Courier">Select</font></tt> method of <tt><font FACE="Courier">TTSFind</font></tt>
to return a handle to a valid TTS engine object. After getting the handle, you can release
the <tt><font FACE="Courier">TTSFind</font></tt> object because it was needed only to
locate a valid TTS engine object. Listing 19.7 shows how this is done. </p>
<hr>
<blockquote>
<b><p>Listing 19.7. Selecting the TTS engine and releasing <tt><font FACE="Courier">TTSFind</font></tt>.
<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>// Pass off the multi-media-device interface as an
IUnknown (since it is one)<br>
<br>
// Should do select now<br>
<br>
hRes = pITTSFind->Select(ttsResult.gModeID, &pITTSCentral,
(LPUNKNOWN) pIAMM);<br>
<br>
if (hRes) {<br>
pITTSFind->Release();<br>
return NULL;<br>
};<br>
<br>
// free random stuff up<br>
<br>
pITTSFind->Release();<br>
<br>
return pITTSCentral;<br>
}</font></tt> </p>
</blockquote>
<hr>
<p>After getting a valid TTS engine and a valid audio output, you can start sending text
to the TTS engine using the <tt><font FACE="Courier">TextData</font></tt> method of the <tt><font
FACE="Courier">TTSCentral</font></tt> interface. </p>
<h4>Sending Text to the TTS Engine</h4>
<p>The <tt><font FACE="Courier">TTSDEMO</font></tt> project uses a simple dialog box to
accept text from the user and send it to the TTS engine. Figure 19.1 shows the dialog box
in design mode. </p>
<p><a HREF="f19-1.gif"><b>Figure 19.1 : </b><i>The dialog box from the TTSDEMO project.</i></a>
</p>
<p>This dialog box has a single text window and two command buttons-<tt><font
FACE="Courier">Speak</font></tt> and <tt><font FACE="Courier">Exit</font></tt>. When the
user presses the <tt><font FACE="Courier">OK</font></tt> button, the text typed into the
window is sent to the TTS engine for playback. The code in Listing 19.8 shows the <tt><font
FACE="Courier">CallBack</font></tt> routine that handles the dialog box events. </p>
<hr>
<blockquote>
<b><p>Listing 19.8. Handling the dialog box callback.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>/*************************************************************************
<br>
DialogProc<br>
*/<br>
BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)<br>
{<br>
switch (uMsg) {<br>
case WM_COMMAND:<br>
switch (LOWORD(wParam)) <br>
{ <br>
case IDOK:<br>
{
<br>
char szSpeak[1024];
<br>
WchAR
wszSpeak[1024];<br>
SDATA
data;<br>
<br>
//
Speak<br>
GetDlgItemText
(hWnd, IDC_EDIT, szSpeak, sizeof(szSpeak));<br>
data.dwSize
= (DWORD)<br>
MultiByteToWideChar(CP_ACP,
0, szSpeak, -1, wszSpeak,<br>
sizeof(wszSpeak)
/ sizeof(WchAR)) * sizeof(WchAR);<br>
data.pData
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -