📄 ch28.htm
字号:
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>, "TAPI Tools-Using the <tt><font FACE="Courier">TAPILINE</font></tt>
Control." 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>
'<br>
ClearHandles ' start clean<br>
TapiStart ' init tapi & get version <br>
TapiGetDevCaps ' get device names<br>
MCIStart ' init multimedia control<br>
'<br>
Me.Left = (Screen.Width - Me.Width) / 2<br>
Me.Top = (Screen.Height - Me.Height) / 2<br>
'<br>
Me.Caption = "TAPI Answer Demo" <br>
Me.Image1.Picture = LoadPicture(App.Path &
"\phone.wmf")<br>
txtStatus.Text = ""<br>
'<br>
cmdButton(1).Enabled = False<br>
cmdButton(2).Enabled = False<br>
'<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>
'<br>
' close up shop<br>
'<br>
Tapiline1.LineDrop "", 0 ' drop active call<br>
Tapiline1.LineClose ' close open line <br>
Tapiline1.LineShutdown ' uninit TAPI<br>
'<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>
'<br>
' start tapi and get lines<br>
'<br>
Dim x As Integer<br>
Dim lRtn As Long<br>
'<br>
lRtn = Tapiline1.LineInitialize(App.EXEName) <br>
If lRtn < 0 Then<br>
MsgBox "Unable to Start TAPI!",
vbCritical<br>
Unload Me<br>
End If<br>
'<br>
For x = 0 To Tapiline1.NumDevices - 1 <br>
Tapiline1.LineNegotiateAPIVersion x,
&H10000, &H10004<br>
Next x<br>
'<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 &H10000 (decimal 65536) and &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>
'<br>
' load device names into list box<br>
'<br>
Dim a As Long<br>
Dim b As LINEDEVCAPS<br>
Dim c As String * 2048<br>
Dim x As Integer<br>
'<br>
For x = 0 To Tapiline1.NumDevices - 1 <br>
' make the call to get dev caps<br>
a = Tapiline1.LineGetDevCaps(x) <br>
If a < 0 Then <br>
lstLines.AddItem
"<<<ERROR>>> DevCaps..." & LineErr(a) <br>
Else<br>
LineDevCapsFunc
TAPI_READ, b, c<br>
c = Clean(c)<br>
lstLines.AddItem
GetOffset(Len(b) + 4, b.dwLineNameOffset, <font FACE="ZAPFDINGBATS">Â</font>b.dwLineNameSize,
c)<br>
End If<br>
Next x<br>
'<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>
'<br>
' init MCI control for ringing phone<br>
'<br>
MMControl1.Notify = False<br>
MMControl1.Wait = True<br>
MMControl1.Shareable = False<br>
MMControl1.DeviceType = "WaveAudio" <br>
MMControl1.filename = App.Path & "\ringin.wav" <br>
MMControl1.Command = "Open" <br>
'<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 + -