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

📄 ch30.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<html>

<head>
<title>Chapter 30 -- Creating TAPI-Enabled Applications</title>
<meta NAME="GENERATOR" CONTENT="Microsoft FrontPage 3.0">
</head>

<body TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<!-- Spidersoft WebZIP Ad Banner Insert -->
<!-- End of Spidersoft WebZIP Ad Banner Insert-->

<h1><font COLOR="#FF0000">Chapter 30</font></h1>

<h1><b><font SIZE="5" COLOR="#FF0000">Creating TAPI-Enabled Applications</font></b> </h1>

<hr WIDTH="100%">

<h3 ALIGN="CENTER"><font SIZE="+2" COLOR="#000000">CONTENTS<a NAME="CONTENTS"></a> </font></h3>

<ul>
  <li><a HREF="#DesigningtheTAPIFONEApplication">Designing the TAPIFONE Application</a> </li>
  <li><a HREF="#ThelibTAPIModule">The libTAPI Module</a> </li>
  <li><a HREF="#frmTAPITheMainForm">frmTAPI-The Main Form</a> <ul>
      <li><a HREF="#LayingOutthefrmTAPIForm">Laying Out the frmTAPI Form</a> </li>
      <li><a HREF="#ThefrmTAPIMenu">The frmTAPI Menu</a> </li>
      <li><a HREF="#CodingthefrmTAPIForm">Coding the frmTAPI Form</a> </li>
    </ul>
  </li>
  <li><a HREF="#TheCallandAboutDialogBoxes">The Call and About Dialog Boxes</a> <ul>
      <li><a HREF="#LayingOutandCodingtheCallDialogBo">Laying Out and Coding the Call Dialog Box</a> 
      </li>
      <li><a HREF="#LayingOutandCodingtheAboutDialogB">Laying Out and Coding the About Dialog Box</a> 
      </li>
    </ul>
  </li>
  <li><a HREF="#TestingTAPIFONE">Testing TAPIFONE</a> </li>
  <li><a HREF="#Summary">Summary</a> </li>
</ul>

<hr>

<p><font COLOR="#000000">In this chapter, you'll put together all the things you learned 
about TAPI in a single application. The </font><tt><font FACE="Courier">TAPIFONE</font></tt> 
application is a complete single-line telephone device that runs on a Windows workstation. 
With a voice-data modem and sound card and this program up and running on your machine, 
you can completely eliminate the telephone handset from your desk. </p>

<p>In this program you'll add code for handling both inbound and outbound calls. You'll 
also give users access to the various TAPI dialog boxes and will maintain a set of 
configuration values for <tt><font FACE="Courier">TAPIFONE</font></tt> in the Windows 
registry. You'll be able to write (and store) call notes on each outbound call made 
through <tt><font FACE="Courier">TAPIFONE</font></tt>. </p>

<p>You'll use <tt><font FACE="Courier">Data Access</font></tt> objects to maintain a 
simple phone book database and you'll also be able to keep track of outbound calls in a 
call log. This log can be exported to a comma-separated value (CSV) text file that can be 
loaded into Excel or Word. </p>

<p>When you complete this project you should have a full understanding of TAPI services 
and how you can use them to build full-featured telephone devices in Visual Basic. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Note</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>The best hardware configuration for this application is a voice-data modem and a sound 
      card with external speakers and a microphone. You can also use this application with a 
      standard modem and an attached telephone handset, but you'll only be able to make outbound 
      calls.</p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<h2><a NAME="DesigningtheTAPIFONEApplication"><font SIZE="5" COLOR="#FF0000">Designing the 
</font><tt><font SIZE="5" COLOR="#FF0000" FACE="Courier">TAPIFONE</font></tt><font
SIZE="5" COLOR="#FF0000"> Application</font></a></h2>

<p>The <tt><font FACE="Courier">TAPIFONE</font></tt> application is designed to replace 
the telephone handset that appears on your desktop. As long as your Windows workstation 
has a voice-data modem or telephony card and an external microphone and speakers, you can 
use the <tt><font FACE="Courier">TAPIFONE</font></tt> for all your in- and outbound voice 
call processing. </p>

<p>The <tt><font FACE="Courier">TAPIFONE</font></tt> project has three forms and three 
code modules. The forms are: 

<ul>
  <li><tt><font FACE="Courier">frmTAPI</font></tt>-This is the main form. It uses a tab layout 
    with five tabs for dialing, a phone book, reviewing the call log, selecting a line device, 
    and updating setup values. </li>
  <li><tt><font FACE="Courier">frmCall</font></tt>-This is a small dialog box that appears 
    each time the user attempts to place a call. Users will be able to add call notes from 
    this screen or, if needed, cancel the outbound call completely. </li>
  <li><tt><font FACE="Courier">frmVPhone</font></tt>-This is a simple About dialog box that 
    lists the name of the application and its version number. </li>
</ul>

<p>The three code modules in the project are: 

<ul>
  <li><tt><font FACE="Courier">libTAPI</font></tt>-This BAS library contains a handful of 
    support and helper routines used by the <tt><font FACE="Courier">TAPIFONE</font></tt> 
    project. These routines are called by one or more of the forms in the preceding list. </li>
  <li><tt><font FACE="Courier">TAPILine</font></tt>-This is the standard BAS module that 
    contains all the structures, constants, and API routines needed to support the <tt><font
    FACE="Courier">TAPILINE</font></tt> control. </li>
  <li><tt><font FACE="Courier">TAPICall</font></tt>-This is a standard BAS module that 
    contains helper routines from the <tt><font FACE="Courier">TAPILine</font></tt> module. </li>
</ul>

<p>The <tt><font FACE="Courier">TAPILine</font></tt> and <tt><font FACE="Courier">TAPICall</font></tt> 
modules need not be keyed in since they are included on the CD-ROM that comes with this 
book. These are stock structures, constants, and API declares for the <tt><font
FACE="Courier">TAPILINE</font></tt> control covered earlier in this book (<a
HREF="ch26.htm">see Chapter 26</a>, &quot;TAPI Tools-Using the <tt><font FACE="Courier">TAPILINE</font></tt> 
Control&quot;). The <tt><font FACE="Courier">libTAPI</font></tt> module will be built for 
this chapter. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Tip</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>If you do not want to enter all the code, you can locate and load the <tt><font
      FACE="Courier">TAPIFONE</font></tt> project on the CD-ROM that ships with this book. </p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<p>Along with the standard dial keypad, the <tt><font FACE="Courier">TAPIFONE</font></tt> 
application has a Phone Book page that lists the person's name and phone number along with 
the last day and time that person was called using <tt><font FACE="Courier">TAPIFONE</font></tt>. 
Users can select a name from this list and press the <tt><font FACE="Courier">Dial</font></tt> 
button directly from the phonebook. As each call is placed, users will be asked to add 
call notes. These call notes are written to a log that can be viewed online or exported to 
a comma-delimited text file for further manipulation. </p>

<p>Users can set several <tt><font FACE="Courier">TAPIFONE</font></tt> parameters to 
control the behavior of the application. These control values are stored in the Windows 
registry and recalled each time the program is run. Values are stored in the <tt><font
FACE="Courier">HKEY_CURRENT_USER\Software\Visual Basic and VBA Program Settings\TAPIFONE</font></tt> 
branch of the registry tree. Key values stored there are: 

<ul>
  <li><tt><font FACE="Courier">MinimizeOnStart</font></tt>-When this is set to &quot;1&quot; 
    the <tt><font FACE="Courier">TAPIFONE</font></tt> application loads and immediately 
    minimizes itself. Users can click on the application from the start bar to place calls. 
    When a call comes in, the application automatically pops up on the user's workstation. The 
    default is &quot;0.&quot; </li>
  <li><tt><font FACE="Courier">Monitor</font></tt>-When this is set to &quot;1&quot; the 
    application automatically opens the selected line device and waits for incoming calls. The 
    default is &quot;0.&quot; </li>
  <li><tt><font FACE="Courier">OutLog</font></tt>-When this is set to &quot;1&quot; all 
    outbound calls are sent to the log table. The default is &quot;1.&quot; </li>
  <li><tt><font FACE="Courier">OrigNumber</font></tt>-This contains the telephone number from 
    which calls are originating. The default is blank. </li>
  <li><tt><font FACE="Courier">StartPage</font></tt>-When this is set to &quot;1&quot; the 
    Phone Book page appears upon startup. When this is set to &quot;0&quot; the Dial Pad page 
    appears. The default is &quot;0.&quot; </li>
  <li><tt><font FACE="Courier">Selected</font></tt>-This is set to the line device number that 
    is used for all in- and outbound voice calls. The default value is &quot;-1.&quot; </li>
</ul>

<h2><a NAME="ThelibTAPIModule"><font SIZE="5" COLOR="#FF0000">The </font><tt><font
SIZE="5" COLOR="#FF0000" FACE="Courier">libTAPI</font></tt><font SIZE="5" COLOR="#FF0000"> 
Module</font></a></h2>

<p>The first step in building the <tt><font FACE="Courier">TAPIFONE</font></tt> project is 
to start Visual Basic and create a new project. Load the <tt><font FACE="Courier">TAPILINE.BAS</font></tt> 
and <tt><font FACE="Courier">TAPICALL.BAS</font></tt> modules from a previous project (<a
HREF="ch26.htm">See Chapters 26</a> and <a HREF="ch28.htm">28</a> for more on these 
modules) by selecting <tt><font FACE="Courier">File </font></tt>|<tt><font FACE="Courier"> 
Add File</font></tt> from the main menu. Once you've added these two BAS modules, save the 
project as <tt><font FACE="Courier">TAPIFONE.VBP</font></tt>. </p>

<p>Next you need to add a new module to the project (<tt><font FACE="Courier">Insert </font></tt>|<tt><font
FACE="Courier"> Module</font></tt>). Set its <tt><font FACE="Courier">Name</font></tt> 
property to <tt><font FACE="Courier">LibTAPIWrapper</font></tt> and save it as <tt><font
FACE="Courier">LIBTAPI.BAS</font></tt>. </p>

<p>First, add some declaration code to the module. You need to declare a handful of public 
variables to handle flags and global values for the project. You'll also add two private 
(module-level) variables used to handle TAPI device information. Finally, you'll add a 
user-defined type to make it easy to place outbound calls. Open the declaration section of 
the form and enter the code shown in Listing 30.1. </p>

<hr>

<blockquote>
  <b><p>Listing 30.1. Adding the declaration code to the <tt><font FACE="Courier">LIBTAPI</font></tt> 
  module.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>'<br>
  Private udtLineDevCaps() As LINEDEVCAPS<br>
  Private cLineDevCapsExtra() As String * 2048<br>
  '<br>
  Public gDialString As String ' phone number to dial<br>
  Public gPlaceCall As Boolean ' ok to place call<br>
  Public gName As String ' name to call<br>
  Public gLineDev As Integer ' selected line device<br>
  '<br>
  Public gMinimize As Integer ' minimize at start<br>
  Public gMonitor As Integer ' monitor at startup<br>
  Public gOutLog As Integer ' log all outbound calls<br>
  Public gOrigNumber As String ' for calledID<br>
  Public gStartPage As Integer ' for default start page<br>
  '<br>
  Type DialParams<br>
  &nbsp;&nbsp;&nbsp;&nbsp;DeviceNumber As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;DialableString As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Privilege As Long<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MediaMode As Long<br>
  End Type</font></tt> </p>
</blockquote>

<hr>

<p>Next, add the routine to handle the initialization of TAPI services for the 
application. This routine will actually perform three things: 

<ul>
  <li><font COLOR="#000000">Initialize TAPI services (calls </font><tt><font FACE="Courier">lineInitialize</font></tt>) 
  </li>
  <li><font COLOR="#000000">Verify the API version (calls </font><tt><font FACE="Courier">lineNegotiateAPIVersion</font></tt>) 
  </li>
  <li><font COLOR="#000000">Collect the line device capabilities (call </font><tt><font
    FACE="Courier">lineGetDevCaps</font></tt>) </li>
</ul>

<p>Add a new function called <tt><font FACE="Courier">initTAPI</font></tt> to your project 
and enter the code shown in Listing 30.2. </p>

<hr>

<blockquote>
  <b><p>Listing 30.2. Adding the <tt><font FACE="Courier">initTAPI</font></tt> function.<br>
  <br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Function InitTAPI(ctrl As Control, cAppName As String) 
  As Long<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' perform initial startup of TAPI services <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim lRtn As Long<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim iNumDev As Long<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim iLoop As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;TAPIClearHandles ctrl ' keep it clean! <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' start it up<br>
  &nbsp;&nbsp;&nbsp;&nbsp;lRtn = ctrl.LineInitialize(cAppName)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If lRtn &lt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GoTo TAPIErr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' confirm devices<br>
  &nbsp;&nbsp;&nbsp;&nbsp;iNumDev = ctrl.NumDevices<br>
  &nbsp;&nbsp;&nbsp;&nbsp;For iLoop = 0 To iNumDev - 1<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lRtn = ctrl.LineNegotiateAPIVersion(iLoop, 
  65536, 65540)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If lRtn &lt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GoTo TAPIErr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Next<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' fill line device capabilities structures <br>
  &nbsp;&nbsp;&nbsp;&nbsp;ReDim Preserve udtLineDevCaps(iNumDev) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;ReDim Preserve cLineDevCapsExtra(iNumDev) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;For iLoop = 0 To iNumDev - 1<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lRtn = ctrl.LineGetDevCaps(iLoop) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If lRtn = 0 Then <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LineDevCapsFunc 
  TAPI_READ, udtLineDevCaps(iLoop), <font FACE="ZAPFDINGBATS">&Acirc;</font>cLineDevCapsExtra(iLoop) 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cLineDevCapsExtra(iLoop) 
  = Clean(cLineDevCapsExtra(iLoop))<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Next<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;GoTo TAPIExit<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  TAPIErr:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Unable to Start TAPI Services&quot;, vbCritical, 
  &quot;InitTAPI&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  TAPIExit:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;InitTAPI = lRtn<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Function</font></tt> </p>
</blockquote>

<hr>

<p>You'll notice that there are two parameters for the function. The first is the <tt><font
FACE="Courier">TAPILINE</font></tt> control used on the main form. The second is the 
application name when initializing TAPI services. You'll also notice that this routine 
loads the capabilities of each device and stores that information in a private array. This 
array can be accessed using another function to be defined later in this section. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Note</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>You might be thinking this kind of code should be placed in a Visual Basic class 
      module. And you'd be right-except for one little thing. Since the <tt><font FACE="Courier">TAPILINE</font></tt> 
      control must be placed on a form, we cannot use a class module to handle properties and 
      methods on the control. Forms and controls cannot be encapsulated in class modules. </p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<p>Next, you need to add the routine used to read the device capabilities stored in the <tt><font
FACE="Courier">udtLineDevCaps</font></tt> array. Add a new function to the project called <tt><font
FACE="Courier">ReadLineDevCaps</font></tt> and add the code shown in Listing 30.3. </p>

<hr>

<blockquote>
  <b><p>Listing 30.3. Adding the <tt><font FACE="Courier">ReadLineDevCaps</font></tt> 
  function.<br>
  <br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Function ReadLineDevCaps(iDevNum As Integer, udtLineDev 
  As LINEDEVCAPS, <font FACE="ZAPFDINGBATS">&Acirc;</font>cExtra As String) As Long<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' return line caps<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo LocalErr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim lRtn As Long<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;lRtn = 0<br>
  &nbsp;&nbsp;&nbsp;&nbsp;udtLineDev = udtLineDevCaps(iDevNum)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cExtra = Trim(cLineDevCapsExtra(iDevNum)) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Exit Function<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  LocalErr:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;lRtn = Err<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Function</font></tt> </p>
</blockquote>

⌨️ 快捷键说明

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