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

📄 dtmf.vb

📁 一個完整的VB.NET tapi3.0 程序
💻 VB
📖 第 1 页 / 共 4 页
字号:
Option Strict Off
Option Explicit On
Friend Class frmDTMF
	Inherits System.Windows.Forms.Form
	'--------------------------------------------------------------------------
	'
	'  Copyright (C) 1997-1999 Microsoft Corporation. All rights reserved.
    '
    '  Adapted by Matthias Moetje (www.terasens.com) for JulMar.ITapi3 library
    '
    '--------------------------------------------------------------------------
	'
	' DTMF = makes Tapi3 outgoing asynchronous call, on which DTMF tones can be generated.
	'
	' This app needs a voice modem to be in the system, because
	' this can generate/detect DTMF tones.
	' Alternatively, the H323 (IP telephony) line exposed by the H323 provider
	' offers DTMF functionality as well.
	' It allows the user to make an outgoing call.
	' On the call, it selects the default terminals for audio+capture and audio+render
	' if it finds any such terminals, and also if the user choses to select them.
	' On the call, it allows the user to generate/detect digits - the user can
	' do that by pressing the corresponding buttons from the form.
	'
	
	
	'
	'user defined types
	'
	
	Enum TestError
		'TErr_NotInitialized = 0
		TErr_Ok = 1
		TErr_Fail = 2
	End Enum
	
	'
	'Constants defined by tapi.h (tapi2.x), tapi3err.h (tapi3.0) or winerror.h
	'for error codes:
	'
	'Note: all tapi constants are defined as VB constants in the file "tapi.txt", which
	'you can find in the subdirectory ..\include that comes with the TAPI3 VB samples.
	'If you use VB6.0, you can open the file tapi.txt using VB's tool "API Text Viewer";
	'then you can use this tool to copy/paste constants in your code.
	'
	
	'tapi3err.h constants
	Const LINEGENERATETERM_DONE As Short = &H1s
	Const LINEGENERATETERM_CANCEL As Short = &H2s
	
	'other constants
	Const NO_ERROR As Short = 0 'Err.Number = 0 when there is no error

	'
	'global variables
	'
	
	'TAPI objects
	Private mbInitialized As Boolean

	'current address + related info
    Private mobjOrigAddress As JulMar.Tapi3.TAddress

	Private mnPreviousAddressIndex As Integer
    Private mvarArrAudioTerminals As Object

    'current call
    Private mobjCall As JulMar.Tapi3.TCall

	
	Private Sub frmDTMF_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load


		'initialize UI
		pbDial.Enabled = True
		pbDisconnect.Enabled = False
		pbRelease.Enabled = False
		
		pbGenerate.Enabled = False
		pbStartDetect.Enabled = False
		pbStopDetect.Enabled = False
		
		'initialize Tapi
		InitializeTapiObjects()
		
		'populate combo with all addresses , regardless
		'of their possibility to support the Digit Terminal class
        Call PopulateAddressesCombo(mobjTapi, False)

        'Populate Address Type Combo
        Dim i As Long = cmbAddressType.Items.Add("Phone Number")
        VB6.SetItemData(cmbAddressType, i, JulMar.Tapi3.LINEADDRESSTYPES.PhoneNumber)
        i = cmbAddressType.Items.Add("Domain Name")
        VB6.SetItemData(cmbAddressType, i, JulMar.Tapi3.LINEADDRESSTYPES.DomainName)
        i = cmbAddressType.Items.Add("EMail Address")
        VB6.SetItemData(cmbAddressType, i, JulMar.Tapi3.LINEADDRESSTYPES.EmailName)
        i = cmbAddressType.Items.Add("IP Address")
        VB6.SetItemData(cmbAddressType, i, JulMar.Tapi3.LINEADDRESSTYPES.IPAddress)

        cmbAddressType.SelectedIndex = 0


	End Sub
	
	Private Sub frmDTMF_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'release all member data "object" variables
		Call ReleaseAllMembers()
		
		'release combo with addresses
		Call DepopulateAddressesCombo()
	End Sub
	
	'
	'This function is called only once, when the form is loaded.
	'
	'This function populates the combobox with the names of the addresses,
	'and memorize also in the "data" field the index of each address
	'that corresponds to the collection of addresses from objTapi.
	'Then it selects the 1st item in the combo.
	'
	'If bMustSupportDTMF is set on True, only addresses that have
	'the DTMF functionality are put in combo.
	'
	'Note: we use On Erorr Resume Next, because we want to add as many items as possible
	'
	Sub PopulateAddressesCombo(ByRef objTapi As JulMar.Tapi3.TTapi, ByRef bMustSupportDTMF As Boolean)
        On Error Resume Next 'this will catch errors

        Dim nResult As Integer
        Dim nIterator, nCount As Integer
        Dim collobjAddresses() As JulMar.Tapi3.TAddress
        Dim objCrtAddress As JulMar.Tapi3.TAddress

        'don't need to clean combo of previous contents,
        'because this function is called only once
        'cmbAddresses.Clear

        collobjAddresses = objTapi.Addresses
        nResult = PrintT3Result("PopulateAddressesCombo: objTapi.Addresses")

        If nResult = NO_ERROR Then
            'populate combobox with names of all addresses from collobjAddresses
            'attach to each combo item the index of the corresponding address
            nCount = 0
            nCount = collobjAddresses.Length

            For nIterator = 0 To nCount - 1

                objCrtAddress = collobjAddresses(nIterator)

                nResult = PrintT3Result("PopulateAddressesCombo: enumerate addresses")

                If nResult = NO_ERROR Then

                    If (bMustSupportDTMF = False Or bMustSupportDTMF And SupportDTMF(objCrtAddress)) Then

                        Dim NewIndex As Integer = cmbAddresses.Items.Add((objCrtAddress.AddressName))

                        nResult = PrintT3Result("PopulateAddressesCombo: adding addresses in combo")

                        If nResult = NO_ERROR Then
                            VB6.SetItemData(cmbAddresses, NewIndex, nIterator)
                        End If

                    End If
                End If

            Next nIterator
        End If


        ''set selection on 1st item
        'If cmbAddresses.Items.Count > 0 Then
        '    cmbAddresses.SelectedIndex = 0
        '    'This automatically called cmbAddresses_Click
        'End If

    End Sub
	
	Sub DepopulateAddressesCombo()
		cmbAddresses.Items.Clear()
	End Sub
	
    Private Sub cmbAddresses_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmbAddresses.TextChanged
        Call ChangeAddress()
    End Sub
	
    Private Sub cmbAddresses_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmbAddresses.SelectedIndexChanged
        Call ChangeAddress()
    End Sub
	
	Private Sub ChangeAddress()
		On Error Resume Next 'this will catch errors
		
		Dim nResult As Integer
		Dim nOrigAddressIndex As Integer
		
		'pick up the collection of Address objects
        Dim objCollAddresses() As JulMar.Tapi3.TAddress

		
        objCollAddresses = mobjTapi.Addresses
		nResult = PrintT3Result("ChangeAddress: mobjTapi.Addresses")
		If nResult <> NO_ERROR Then
			Call MsgBox("Fatal error: can't get TAPI addresses, exit program", MsgBoxStyle.Critical)
			Stop 'fatal error
		End If
		
		'find index of address selected by the user
		nOrigAddressIndex = FindOriginationAddressIndex()
		
		'
		'pick up the new address - update all related global variables
		'
		
		If mnPreviousAddressIndex <> nOrigAddressIndex Then
			'If a new address was selected, release the previous one
			'and save a "pointer" to the new one, plus related info
			'If the same address was selected, don't release/save it again,
			'just keep the previous one + its related info.
			
			'
			'clean up previous info
			'
			
            'release old terminals
			Call ReleaseAudioTerminals(mvarArrAudioTerminals)
			
			'wipe out other info
			mnPreviousAddressIndex = 0
			
			'
			'save new info
			'
			
			'validate new index
            If nOrigAddressIndex >= objCollAddresses.GetLowerBound(0) And nOrigAddressIndex <= objCollAddresses.GetUpperBound(0) Then

                'save new address
                mobjOrigAddress = objCollAddresses(nOrigAddressIndex)

                'save also the new index
                mnPreviousAddressIndex = nOrigAddressIndex

                'create new terminals
                Call GetAudioTerminals(mobjOrigAddress, mvarArrAudioTerminals)

            End If
			
		End If
		
		'release objects that are not needed anymore
		'(this decrements the reference count)
    End Sub
	
    Function SupportDTMF(ByRef objAddress As JulMar.Tapi3.TAddress) As Boolean
        On Error Resume Next 'this will catch errors

        Dim nResult, nResult1 As Integer

        Dim lGenerateDigitSupport, lMonitorDigitSupport As Integer

        'prepare return value
        SupportDTMF = False

        nResult = PrintT3Result("SupportDTMF: objAddress->Query (ITAddressCapabilities)")

        If nResult = NO_ERROR Then

            lGenerateDigitSupport = objAddress.AddressCapability(JulMar.Tapi3.ADDRESS_CAPABILITY.AC_GENERATEDIGITSUPPORT)
            nResult = PrintT3Result("SupportDTMF: AddressCapability(AC_GENERATEDIGITSUPPORT)")
            nResult1 = nResult
            lMonitorDigitSupport = objAddress.AddressCapability(JulMar.Tapi3.ADDRESS_CAPABILITY.AC_MONITORDIGITSUPPORT)
            nResult = PrintT3Result("SupportDTMF: AddressCapability(AC_MONITORDIGITSUPPORT)")

            If nResult = NO_ERROR And nResult1 = NO_ERROR Then

                If lGenerateDigitSupport <> 0 And lMonitorDigitSupport <> 0 Then
                    SupportDTMF = True
                End If

            End If

        End If


    End Function
	
    Private Sub PrintCallState(ByRef State As JulMar.Tapi3.CALL_STATE)
        Dim strMsg As String

        Select Case State
            Case JulMar.Tapi3.CALL_STATE.CS_CONNECTED
                txtStatus.Text = txtStatus.Text & Chr(13) & Chr(10)
                txtStatus.Text = txtStatus.Text & "call state: CS_CONNECTED"
            Case JulMar.Tapi3.CALL_STATE.CS_DISCONNECTED
                strMsg = "call state: CS_DISCONNECTED" & Chr(13) & Chr(10)
                strMsg = strMsg & "Call was disconnected. "
                strMsg = strMsg & "Now you should Release it. "
                strMsg = strMsg & "Then you can press Dial again or just quit."
                txtStatus.Text = txtStatus.Text & Chr(13) & Chr(10)
                txtStatus.Text = txtStatus.Text & strMsg
            Case JulMar.Tapi3.CALL_STATE.CS_HOLD
                txtStatus.Text = txtStatus.Text & Chr(13) & Chr(10)
                txtStatus.Text = txtStatus.Text & "call state: CS_HOLD"
            Case JulMar.Tapi3.CALL_STATE.CS_IDLE
                txtStatus.Text = txtStatus.Text & Chr(13) & Chr(10)
                txtStatus.Text = txtStatus.Text & "call state: CS_IDLE"
            Case JulMar.Tapi3.CALL_STATE.CS_INPROGRESS
                txtStatus.Text = txtStatus.Text & Chr(13) & Chr(10)
                txtStatus.Text = txtStatus.Text & "call state: CS_INPROGRESS"
            Case JulMar.Tapi3.CALL_STATE.CS_OFFERING
                txtStatus.Text = txtStatus.Text & Chr(13) & Chr(10)
                txtStatus.Text = txtStatus.Text & "call state: CS_OFFERING"
            Case JulMar.Tapi3.CALL_STATE.CS_QUEUED
                txtStatus.Text = txtStatus.Text & Chr(13) & Chr(10)
                txtStatus.Text = txtStatus.Text & "call state: CS_QUEUED"
            Case Else
                txtStatus.Text = txtStatus.Text & Chr(13) & Chr(10)
                txtStatus.Text = txtStatus.Text & "call state: unknown!!"
        End Select

        txtStatus.SelectionStart = Len(txtStatus.Text)
        txtStatus.SelectionLength = 0

    End Sub
	

    Private Sub pbDial_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles pbDial.Click
        On Error Resume Next 'this will catch errors

        Dim nResult As Integer

⌨️ 快捷键说明

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