实例说明
在本实例中,我们将制作一个辅助型电话程序。程序运行后,输入有效的电话号码,单击"开始拨号"按钮,即可打开Windows所带的电话拨号程序,即可实现拨号。程序运行结果如图94-1所示。
技术要点
l 创建TAPI应用程序
l TAPI错误处理
图94-1 运行结果
实现过程
■ 新建项目
打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中选择"Windows应用程序",在名称域中输入"AutoDial",然后选择保存路径。单击"确认"。
■ 添加控件
向当前窗体上添加一个Label控件、一个ListBox控件、一个TextBox控件和两个Button按钮。
■ 设置属性
将Label控件和Button按钮的Caption属性设置为跟界面一致,TextBox控件的Text属性置为空。其余属性使用默认值即可。
Private Declare Function tapiRequestMakeCall Lib "TAPI32.DLL" (ByVal DestAddress As String,ByVal AppName As String,ByVal CalledParty As String,ByVal Comment As String) As Integer
Const TAPIERR_CONNECTED As Short = 0
Const TAPIERR_DROPPED As Short = -1
Const TAPIERR_NOREQUESTRECIPIENT As Short = -2
Const TAPIERR_REQUESTQUEUEFULL As Short = -3
Const TAPIERR_INVALDESTADDRESS As Short = -4
Const TAPIERR_DESTBUSY As Short = -11
Const TAPIERR_UNKNOWNREQUESTID As Short = -15
Const TAPIERR_REQUESTFailed As Short = -16
Private Sub Command1_Click(ByVal eventSender As System.Object,ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim strPhoneNum As String
Dim res As Integer
If Trim(Text1.Text) = "" Then
List1.Items.Add("错误:没有输入电话号码")
Exit Sub
Else
strPhoneNum = Trim(Text1.Text)
End If
res = tapiRequestMakeCall(strPhoneNum,"Tapi Sample",strPhoneNum,"")
Call TapiStatus(res)
End Sub
Private Sub Command2_Click(ByVal eventSender As System.Object,ByVal eventArgs As System.EventArgs) Handles Command2.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal eventSender As System.Object,ByVal eventArgs As System.EventArgs) Handles MyBase.Load
EnableDial()
List1.Items.Add("拨号已连接")
End Sub
Private Sub text1_TextChanged(ByVal eventSender As System.Object,ByVal eventArgs As System.EventArgs) Handles text1.TextChanged
EnableDial()
End Sub
Private Sub EnableDial()
Command1.Enabled = Len(Trim(Text1.Text)) > 0
End Sub
Sub TapiStatus(ByRef strres As Integer)
Select Case strres
Case TAPIERR_CONNECTED
List1.Items.Add("拨号已连接")
Case TAPIERR_INVALDESTADDRESS
List1.Items.Add("错误的目的地址")
Case TAPIERR_NOREQUESTRECIPIENT
List1.Items.Add("没有电话接口程序运行")
Case TAPIERR_REQUESTFailed
List1.Items.Add("拨号请求由于未知原因失败")
Case TAPIERR_DESTBUSY
List1.Items.Add("对方电话正忙")
Case TAPIERR_UNKNOWNREQUESTID
List1.Items.Add("没有这个电话号码")
Case TAPIERR_DROPPED
List1.Items.Add("对方已挂断")
End Select
End Sub
■ 运行程序
单击菜单"调试|启动"或单击 图标运行程序。
小结
TAPI有两种类型:完全型和辅助型。完全型只需要TAPI库,直接操作底层TAPI函数来建立及管理TAPI会话。而辅助型则要求别的部件和程序(如Window系统电话拨号程序),这类应用程序编程比较简单,无需进行很多的编程工作。在我们的实例中使用的是辅助性TAPI,单击"开始拨号"后,出现如图94-2所示的图形。