⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch20.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<p>After handling the registration of SR services, the routine adds TTS services to the 
project. After initializing the <tt><font FACE="Courier">Voice Text</font></tt> object, 
registering the application, and enabling TTS, the <tt><font FACE="Courier">InitVText</font></tt> 
routine is called to build the menu and button options on the forms. You'll see this code 
later. </p>

<p>The next set of code to add is the <tt><font FACE="Courier">UnInitSAPI</font></tt> 
routine. This routine will be called at the end of the program. The <tt><font
FACE="Courier">UnInitSAPI</font></tt> routine deactivates all SAPI objects and releases 
all links to SAPI services. Add a new subroutine to the <tt><font FACE="Courier">modSpeech</font></tt> 
module and enter the code shown in Listing 20.3. </p>

<hr>

<blockquote>
  <b><p>Listing 20.3. Adding the <font SIZE="1" FACE="MCPdigital-B">UnInitSAPI</font> code.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub UnInitSAPI()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' remove sapi services<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objVMenu.Active = False ' stop menu<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objVMenu = Nothing&nbsp;&nbsp;' remove link<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objVCmd.Awake = False&nbsp;&nbsp;&nbsp;' close down SR<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objVCmd = Nothing&nbsp;&nbsp;&nbsp;' remove link<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objVText.Enabled = False ' stop TTS<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objVText = Nothing&nbsp;&nbsp;' remove link<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<h3>Coding the <tt><font SIZE="4" FACE="Courier"><a NAME="CodingtheInitVoiceRoutine">InitVoice</a></font></tt><font
SIZE="4"> Routine</font></h3>

<p>The <tt><font FACE="Courier">InitVoice</font></tt> routine adds all the new custom 
commands to the menu object declared in the <tt><font FACE="Courier">InitSAPI</font></tt> 
routine. After it adds the new commands, a timer object is initialized and enabled. The 
timer will be used to check for a valid voice command. Add a new subroutine called <tt><font
FACE="Courier">InitVoice</font></tt> and enter the code shown in Listing 20.4. </p>

<hr>

<blockquote>
  <b><p>Listing 20.4. Adding the <font SIZE="1" FACE="MCPdigital-B">InitVoice</font> 
  routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub InitVoice()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' build added voice menu commands<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim x As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;ReDim Preserve cVCmdMenu(11) As String <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(1) = &quot;New&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(2) = &quot;Open&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(3) = &quot;Exit&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(4) = &quot;Toggle Toolbar&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(5) = &quot;Read&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(6) = &quot;Forward&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(7) = &quot;Rewind&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(8) = &quot;Stop&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(9) = &quot;Cut&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(10) = &quot;Copy&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cVCmdMenu(11) = &quot;Paste&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;For x = 1 To 11<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objVMenu.Add 100 + x, cVCmdMenu(x), 
  &quot;MDI Menu&quot;, cVCmdMenu(x)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Next x<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objVMenu.Active = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' start timer loop<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmMDI!SRTimer.Interval = 500 ' every 1/2 second<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmMDI!SRTimer.Enabled = True ' turn it on<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>As you can see, adding custom menu options involves adding the command strings to the 
menu object and then activating the menu object. The code that sets the timer is needed to 
poll the <tt><font FACE="Courier">SpokenCommand</font></tt> property every half second. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Tip</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>You do not have to add custom commands to SAPI-enabled applications that have a 
      declared menu. All menu items are automatically registered as voice commands by the 
      operating system. The commands added here are really shortcuts to existing menu options.</p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<h3><a NAME="CodingtheInitVTextRoutine">Coding the <tt><font SIZE="4" FACE="Courier">InitVText</font></tt><font
SIZE="4"> Routine</font></a></h3>

<p>When you add TTS services to the application, you need only to initialize the TTS 
object and then &quot;turn on&quot; the menu and/or command buttons that allow users to 
gain access to the TTS engine. In this application a set of buttons for the toolbar needs 
initialization. The code in Listing 20.5 shows how this is done. </p>

<hr>

<blockquote>
  <b><p>Listing 20.5. Adding the <font SIZE="1" FACE="MCPdigital-B">InitVText</font> 
  routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub InitVText()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' set up vText buttons and menu<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cDir As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cPic(4) As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim x As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cDir = &quot;d:\sams\cdg\chap20\mdi\&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;cPic(0) = &quot;arw01rt.ico&quot; ' read <br>
  &nbsp;&nbsp;&nbsp;&nbsp;cPic(1) = &quot;arw01up.ico&quot; ' forward <br>
  &nbsp;&nbsp;&nbsp;&nbsp;cPic(2) = &quot;arw01lt.ico&quot; ' rewind <br>
  &nbsp;&nbsp;&nbsp;&nbsp;cPic(3) = &quot;arw01dn.ico&quot; ' stop <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;For x = 0 To 3<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(x).Picture = 
  LoadPicture(cDir &amp; cPic(x))<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Next x<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;VTextAction vTxtStop ' force &quot;stop&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' start timer loop<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmMDI.TTSTimer.Interval = 500 ' every 1/2 second<br>
  &nbsp;&nbsp;&nbsp;&nbsp;frmMDI.TTSTimer.Enabled = True ' turn it on<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>You'll notice a call to the <tt><font FACE="Courier">VTextAction</font></tt> routine. 
This routine handles the TTS service requests (<tt><font FACE="Courier">Speak</font></tt>, 
<tt><font FACE="Courier">FastForward</font></tt>, <tt><font FACE="Courier">Rewind</font></tt>, 
and <tt><font FACE="Courier">StopSpeaking</font></tt>). A timer is also enabled in order 
to track the active status of the TTS engine. </p>

<h3><a NAME="CodingtheVTextActionRoutine">Coding the <tt><font SIZE="4" FACE="Courier">VTextAction</font></tt><font
SIZE="4"> Routine</font></a></h3>

<p>The <tt><font FACE="Courier">VTextAction</font></tt> routine is the code that handles 
the various TTS service requests made by the user. This one set of code can handle all of 
the playback options for the program. It also handles the enabling of the toolbar buttons 
and the menu options. Listing 20.6 shows the code needed for the <tt><font FACE="Courier">VTextAction</font></tt> 
subroutine. </p>

<hr>

<blockquote>
  <b><p>Listing 20.6. Adding the <font SIZE="1" FACE="MCPdigital-B">VTextAction</font> 
  routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub VTextAction(Index As Integer) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' handle request to start/stop reading <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Static bVPause As Boolean<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Screen.ActiveForm.MousePointer = vbHourglass <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' handle service request<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Select Case Index<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case vTxtSpeak ' speak=0<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If Not bVText Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If 
  Len(Trim(Screen.ActiveForm.Text1.Text)) &lt;&gt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objVText.Speak 
  Screen.ActiveForm.Text1.Text, vtxtsp_NORMAL<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bVText 
  = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End 
  If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case vTxtForward ' fast forward=1<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If bVText Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objVText.AudioFastForward 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case vTxtRewind ' rewind=2<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If bVText Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objVText.AudioRewind 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Case vTxtStop ' stop speaking=3<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If bVText Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objVText.StopSpeaking 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bVText 
  = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bVPause 
  = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End Select<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' update menu<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If bVText Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;With Screen.ActiveForm <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mnuVText(0).Enabled 
  = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mnuVText(1).Visible 
  = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mnuVText(2).Visible 
  = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mnuVText(3).Visible 
  = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End With<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If Screen.ActiveForm.Caption &lt;&gt; 
  &quot;MDI NotePad&quot; Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;With 
  Screen.ActiveForm<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mnuVText(0).Enabled 
  = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mnuVText(1).Visible 
  = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mnuVText(2).Visible 
  = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.mnuVText(3).Visible 
  = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End With<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' update buttons<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If bVText Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(0).Visible = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(1).Visible = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(2).Visible = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(3).Visible = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(0).Visible = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(1).Visible = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(2).Visible = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(3).Visible = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' no open text pages<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If Not AnyPadsLeft() Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmMDI.ImgVText(0).Visible = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Screen.ActiveForm.MousePointer = vbNormal <br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -