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

📄 ch28.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 5 页
字号:
telephone into this control at run-time. Be sure to set the <tt><font FACE="Courier">Stretch</font></tt> 
property of the <tt><font FACE="Courier">Image</font></tt> control to <tt><font
FACE="Courier">TRUE</font></tt>. Finally, by now you should recognize the control array 
used with the command buttons. Be sure to add the first button, set its values (including 
the <tt><font FACE="Courier">Index</font></tt> value), and then use <tt><font
FACE="Courier">Edit </font></tt>|<tt><font FACE="Courier"> Copy </font></tt>and <tt><font
FACE="Courier">Edit </font></tt>|<tt><font FACE="Courier"> Paste</font></tt> to add all 
the buttons in the control array. </p>

<p>When you are finished building the form, save it as <tt><font FACE="Courier">TAPIANS.FRM</font></tt> 
and save the project as <tt><font FACE="Courier">TAPIANS.VBP</font></tt> before you go on 
to the next section. </p>

<h3><a NAME="CodingtheTAPIANSForm">Coding the <tt><font SIZE="4" FACE="Courier">TAPIANS</font></tt><font
SIZE="4"> Form</font></a></h3>

<p>There are a handful of code routines you need to add to the <tt><font FACE="Courier">TAPIANS</font></tt> 
form. But, before you add code to the form, you need to add two BAS modules to the project 
(<tt><font FACE="Courier">File </font></tt>|<tt><font FACE="Courier"> Add File</font></tt>). 
These modules can be found on the CD-ROM that ships with this book. The first is the <tt><font
FACE="Courier">TAPILINE.BAS</font></tt> module. This module contains all the TAPI 
constants, structures, and helper APIs needed to support the <tt><font FACE="Courier">TAPILINE</font></tt> 
control. The second BAS module you need to add to the project is the <tt><font
FACE="Courier">TAPICALL.BAS</font></tt> module. This routine contains several helper 
functions and subroutines for decoding TAPI messages. There are also several general 
routines in this module to handle typical TAPI operations for Visual Basic programs </p>

<p align="center">.</p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Tip</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>The easiest way to add the <tt><font FACE="Courier">TAPICALL</font></tt> and <tt><font
      FACE="Courier">TAPILINE</font></tt> modules to your project is to first copy them into the 
      project directory, and then use the <tt><font FACE="Courier">File </font></tt>|<tt><font
      FACE="Courier"> Add File</font></tt> menu option to place them into your project. You 
      probably have more than one copy of these routines on your hard disk. You used them in <a
      HREF="ch26.htm">Chapter 26</a>, &quot;TAPI Tools-Using the <tt><font FACE="Courier">TAPILINE</font></tt> 
      Control.&quot; If you can't find them on your hard disk, you can find them on the CD-ROM 
      that ships with this book. </p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<h4>Coding the <tt><font FACE="Courier">Form_Load</font></tt> and <tt><font FACE="Courier">Form_Unload</font></tt> 
Events</h4>

<p>The first code to add to the project is the code for the <tt><font FACE="Courier">Form_Load</font></tt> 
event. This code calls several support routines and handles general initialization 
controls. Add the code shown in Listing 28.1 to the <tt><font FACE="Courier">Form_Load</font></tt> 
event of the form. </p>

<hr>

<blockquote>
  <b><p>Listing 28.1. Coding the <tt><font FACE="Courier">Form_Load</font></tt> event.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Private Sub Form_Load()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;ClearHandles ' start clean<br>
  &nbsp;&nbsp;&nbsp;&nbsp;TapiStart ' init tapi &amp; get version <br>
  &nbsp;&nbsp;&nbsp;&nbsp;TapiGetDevCaps ' get device names<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MCIStart ' init multimedia control<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Me.Left = (Screen.Width - Me.Width) / 2<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Me.Top = (Screen.Height - Me.Height) / 2<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Me.Caption = &quot;TAPI Answer Demo&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;Me.Image1.Picture = LoadPicture(App.Path &amp; 
  &quot;\phone.wmf&quot;)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;txtStatus.Text = &quot;&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cmdButton(1).Enabled = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cmdButton(2).Enabled = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>The code in the <tt><font FACE="Courier">Form_Load</font></tt> event calls for support 
routines to set up TAPI and the MCI control. The rest of the code centers the form and 
sets properties for the form and various controls on the form. </p>

<p>Listing 28.2 shows the code for the <tt><font FACE="Courier">Form_Unload</font></tt> 
event. This code makes sure that TAPI services are properly closed down before exiting the 
program. Add this code to the <tt><font FACE="Courier">TAPIANS</font></tt> form. </p>

<hr>

<blockquote>
  <b><p>Listing 28.2. Adding code to the <tt><font FACE="Courier">Form_Unload</font></tt> 
  event.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Private Sub Form_Unload(Cancel As Integer) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' close up shop<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Tapiline1.LineDrop &quot;&quot;, 0 ' drop active call<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Tapiline1.LineClose ' close open line <br>
  &nbsp;&nbsp;&nbsp;&nbsp;Tapiline1.LineShutdown ' uninit TAPI<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>Nothing complex here. The only item to note is the inclusion of the two parameters for 
the <tt><font FACE="Courier">lineDrop</font></tt> method. These parameters are used only 
when passing user information on ISDN lines. Since this program assumes you'll be using 
voice-grade telephone lines, the values are left empty. </p>

<h4>Adding the <tt><font FACE="Courier">TAPIANS</font></tt> Support Routines</h4>

<p>There are just a few support routines needed for the <tt><font FACE="Courier">TAPIANS</font></tt> 
project. These routines handle the initialization of TAPI and multimedia services for the 
workstation. You'll also add a routine to clear out TAPI pointers at the start of the 
project and the end of a call. </p>

<p>First, you need a routine to handle the initial startup of TAPI services for the 
workstation. This routine should start TAPI, get a count of all the available line 
devices, and confirm the API version of the devices. The code in Listing 28.3 shows you 
how to do this. Add a new sub-routine called <tt><font FACE="Courier">TAPIStart</font></tt> 
on the form (use <tt><font FACE="Courier">Insert </font></tt>|<tt><font FACE="Courier"> 
Procedure</font></tt>), and enter the code shown in Listing 28.3. </p>

<hr>

<blockquote>
  <b><p>Listing 28.3. Adding the <tt><font FACE="Courier">TAPIStart</font></tt> procedure.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub TapiStart()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' start tapi and get lines<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim x As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim lRtn As Long<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;lRtn = Tapiline1.LineInitialize(App.EXEName) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;If lRtn &lt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Unable to Start TAPI!&quot;, 
  vbCritical<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Unload Me<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;For x = 0 To Tapiline1.NumDevices - 1 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tapiline1.LineNegotiateAPIVersion x, 
  &amp;H10000, &amp;H10004<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Next x<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>You've seen most of this before. Notice that the <tt><font FACE="Courier">lRtn</font></tt> 
value is checked after attempting to start TAPI. You'll see that the test is for a value <i>less</i> 
than zero. Several TAPI functions return positive values instead of zero when they are 
successful. However, error values are always less than zero. You'll also notice the 
hexadecimal values &amp;H10000 (decimal 65536) and &amp;H10004 (decimal 65540) as 
parameters in the <tt><font FACE="Courier">lineNegotiateAPIVersion</font></tt> method. 
These are the minimum (1.0) and maximum (1.4) API versions supported by the <tt><font
FACE="Courier">TAPILINE</font></tt> control. Notice also that the <tt><font FACE="Courier">numDevices</font></tt> 
property of the <tt><font FACE="Courier">TAPILINE</font></tt> control is set to the total 
number of recognized line devices for the workstation. You'll use this value to collect 
information on all the registered line devices. </p>

<p>Next, you need to add a routine to get the capabilities of all the line devices you 
discovered in the <tt><font FACE="Courier">TAPIStart</font></tt> routine. You'll use the <tt><font
FACE="Courier">numDevices</font></tt> property just mentioned above. Listing 28.4 shows 
the new routine <tt><font FACE="Courier">TapiGetDevCaps</font></tt>. This routine fills a 
list box with the names of all the registered devices. With this list, the user can select 
the device most appropriate for voice calls. Add the code in Listing 28.4 to your form. </p>

<hr>

<blockquote>
  <b><p>Listing 28.4. Adding the <tt><font FACE="Courier">TapiGetDevCaps</font></tt> 
  routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub TapiGetDevCaps()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' load device names into list box<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim a As Long<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim b As LINEDEVCAPS<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim c As String * 2048<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim x As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;For x = 0 To Tapiline1.NumDevices - 1 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' make the call to get dev caps<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = Tapiline1.LineGetDevCaps(x) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If a &lt; 0 Then <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lstLines.AddItem 
  &quot;&lt;&lt;&lt;ERROR&gt;&gt;&gt; DevCaps...&quot; &amp; LineErr(a) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LineDevCapsFunc 
  TAPI_READ, b, c<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = Clean(c)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lstLines.AddItem 
  GetOffset(Len(b) + 4, b.dwLineNameOffset, <font FACE="ZAPFDINGBATS">&Acirc;</font>b.dwLineNameSize, 
  c)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Next x<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>You can see that after calling the <tt><font FACE="Courier">lineGetDevCaps</font></tt> 
method of the <tt><font FACE="Courier">TAPILINE</font></tt> control, you need to call the <tt><font
FACE="Courier">lineDevCapsFunc</font></tt> API function to retrieve the data in the <tt><font
FACE="Courier">LINEDEVCAPS</font></tt> structure. This data is filled in by TAPI when you 
use the <tt><font FACE="Courier">lineGetDevCaps</font></tt> method. This routine uses the <tt><font
FACE="Courier">GetOffset</font></tt> helper function (from the TAPICALLS library) to pull 
out the line device name and insert it into the list control on the form. </p>

<p>Since this project will use a WAV file to signal a ringing phone, you'll need to add 
some code to initialize the <tt><font FACE="Courier">MCI Multimedia</font></tt> control to 
handle WAV output. Add a new subroutine called <tt><font FACE="Courier">MCIStart</font></tt> 
and enter the code shown in Listing 28.5. </p>

<hr>

<blockquote>
  <b><p>Listing 28.5. Adding the <tt><font FACE="Courier">MCIStart</font></tt> routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub MCIStart()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' init MCI control for ringing phone<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MMControl1.Notify = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MMControl1.Wait = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MMControl1.Shareable = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MMControl1.DeviceType = &quot;WaveAudio&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;MMControl1.filename = App.Path &amp; &quot;\ringin.wav&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;MMControl1.Command = &quot;Open&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>Not too much to comment on here, either. Just note that the control accesses the <tt><font
FACE="Courier">RINGIN.WAV</font></tt> file. This should be placed in your application 
directory. It is included on the CD-ROM that accompanies this book. If you have it stored 
in another location, be sure to modify the code in Listing 28.5 to point to the proper 
folder. </p>

<p>The last support routine you need to add to the project is one that clears <tt><font
FACE="Courier">TAPILINE</font></tt> control properties related to a completed call. You'll 
use this routine at the very beginning of the program (for initialization) and at the end 
of each call (for cleanup). Add a new subroutine called <tt><font FACE="Courier">ClearHandles</font></tt> 
and enter the code shown in Listing 28.6. </p>

<hr>

<blockquote>
  <b><p>Listing 28.6. Adding the <tt><font FACE="Courier">ClearHandles</font></tt> routine.<br>

⌨️ 快捷键说明

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