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

📄 cp_ivrdlg.cpp

📁 Voice on phone using TAPI
💻 CPP
字号:
/*********************************************************************
 * CP_IVRDlg.cpp:
 * implementation of the CP_IVRDlg class
 *
 ********************************************************************/

#include "stdafx.h"
#include "CP_IVR.h"
#include "CP_IVRDlg.h"
#include "HDevices.h"
#include "HSettings.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/*********************************************************************
 * Globals & externals
 ********************************************************************/
#define		POS_HEIGHT	185
extern		CHDevices g_devices;
extern		CHSettings g_settings;
extern		CString g_phoneNumber;
extern		CString g_path;

/*********************************************************************
 * CAboutDlg dialog used for App About
 ********************************************************************/
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/*********************************************************************
 * CCP_IVRDlg dialog
 ********************************************************************/
CCP_IVRDlg::CCP_IVRDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCP_IVRDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCP_IVRDlg)
	m_callerID = _T("");
	m_pkUpDelay = _T("");
	m_status = _T("");
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCP_IVRDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCP_IVRDlg)
	DDX_Control(pDX, IDC_LOG, m_logList);
	DDX_Control(pDX, IDC_DEVICE_LIST, m_devList);
	DDX_Text(pDX, IDC_CALLER_ID, m_callerID);
	DDX_Text(pDX, IDC_PK_UP_DELAY, m_pkUpDelay);
	DDX_Text(pDX, IDC_STATUS, m_status);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCP_IVRDlg, CDialog)
	//{{AFX_MSG_MAP(CCP_IVRDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SEE_LOG, OnSeeLog)
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_RESTART, OnRestart)
	ON_BN_CLICKED(IDC_CLEAR_LOG, OnClearLog)
	ON_BN_CLICKED(IDC_HOOK_MIC_SPK, OnHookMicSpk)
	//}}AFX_MSG_MAP
	ON_MESSAGE (WM_STATUS_CHNGD,OnTAPIStatusChanged)
END_MESSAGE_MAP()

/*********************************************************************
 * CCP_IVRDlg impelementation
 *********************************************************************
 *
 ********************************************************************/

 
 
/*********************************************************************
 * Initializing
 ********************************************************************/
BOOL CCP_IVRDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
							 /* Call it once to position log window */
	OnSeeLog();
									   /* Load application settings */
	g_settings.Load();
						/* Update UI and display ring count to user */
	m_pkUpDelay.Format(_T("%d"),g_settings.m_ringCount);

						/* Get the application exe path, on runtime */
	GetModuleFileName(NULL,g_path.GetBuffer(512),512);

						 /* Remove .exe file name to get app folder */
	g_path.ReleaseBuffer(512);
	g_path.FreeExtra();
	int index = g_path.ReverseFind('\\');
	if (index!=-1) {
		g_path.Delete(index+1,g_path.GetLength()-index-1);
	} else {
		// UNDONE: Any errors?
	}

	UpdateData(FALSE);

			  /* return TRUE  unless you set the focus to a control */
	return TRUE;
}

/*********************************************************************
 * Control menu, maximize or minimize ...
 ********************************************************************/
void CCP_IVRDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

/*********************************************************************
 * Paintings
 ********************************************************************/
void CCP_IVRDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

/*********************************************************************
 * Drag icon
 ********************************************************************/
HCURSOR CCP_IVRDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/*********************************************************************
 * Enter || OK button
 ********************************************************************/
void CCP_IVRDlg::OnOK() 
{
//	CDialog::OnOK();
}

/*********************************************************************
 * Reposition (Show / Hide) Log box
 ********************************************************************/
void CCP_IVRDlg::OnSeeLog()
{
									   /* Get status to reverse it! */
	CRect rc;
	CString str;
											   /* Get current label */
	GetDlgItem(IDC_SEE_LOG)->GetWindowText(str);
													/* Get position */
	GetWindowRect(rc);

													 /* re position */
	if ( str ==_T("v") ) {					  /* Close the log view */
		SetWindowPos(0,0,0,rc.Width(),
			rc.Height()+POS_HEIGHT,SWP_NOMOVE|SWP_NOZORDER);
												   /* Reverse label */
		GetDlgItem(IDC_SEE_LOG)->SetWindowText(_T("^"));
	} else {									   /* Open log view */
		SetWindowPos(0,0,0,rc.Width(),
			rc.Height()-POS_HEIGHT,SWP_NOMOVE|SWP_NOZORDER);
		GetDlgItem(IDC_SEE_LOG)->SetWindowText(_T("v"));
	}
}

/*********************************************************************
 * Start TAPI operations
 ********************************************************************/
void CCP_IVRDlg::OnStart()
{
	CString str;
												 /* Shall we start? */
	GetDlgItem(IDC_START)->GetWindowText(str);
	if (str==_T("Start")) {
		try {
			TRACE(_T("\n Initializing...\n"));
			m_manager.SetParent(this);
			m_manager.WakeUp();
									   /* Button should be stop now */
			GetDlgItem(IDC_START)->SetWindowText(_T("Stop"));
			GetDlgItem(IDC_RESTART)->EnableWindow(TRUE);

								  /* Is there any known exceptions? */
		} catch (CTapiObj::TEx e) {
			CString sz;
			sz.Format(_T("Err: [code:%d], [date:%s],")
				_T("[time:%s], [reason:%s]\n"),
				e.code,e.date,e.time,e.result);
										 /* Log error in errors.log */
			CHErrLogger::LogError(sz);

								/* Is there any Unknown exceptions? */
		} catch ( ... ) {
			CString sz;
			sz.Format(_T("Err: (Unhandled exeption) ")
				_T("Unknown reason, DaliaDlg - Line:607\n"));
										 /* Log error in errors.log */
			CHErrLogger::LogError(sz);
		}
	} else {
											  /* If we have to Stop */

		m_manager.SleepAll();						   /* Stop TAPI */
		GetDlgItem(IDC_START)->SetWindowText(_T("Start"));
		GetDlgItem(IDC_RESTART)->EnableWindow(FALSE);
	}
}

/*********************************************************************
 * Our manager class event handler
 *********************************************************************
 * This is the event that Central manager use it to inform us from
 * TAPI Events, events of the line, or phone.
 * In this sample I just use them to display a text, that displays
 * current status of the operation.
 ********************************************************************/
LRESULT CCP_IVRDlg::OnTAPIStatusChanged (WPARAM wParam, LPARAM lParam)
{
				 /* Define all possible states that TAPI might be in*/
	CString tmp;
	CString stat[] = {	_T("Ready") , _T("Initializing"), 
						_T("Opening line"), _T("Restarting"), 
						_T("Ready"), _T("Busy"), 
						_T("Shutting down"), _T("(( Ring ))"), 
						_T("Picked up"), _T("Failed"), 
						_T("New DTMF Detected"), 
						_T("CallerID received")	};
	int i=0;

	switch (lParam) {
									   /* Line is being initialized */
	case ST_INIT:
		break;
											  /* Line is being open */
	case ST_OPEN: {
		m_devList.ResetContent();
		for (i=0;i<g_devices.GetCount();i++)
			m_devList.AddString(g_devices.GetItemName(i,FALSE));

		int defId = g_devices.GetDefault();
		if(defId!=-1) {
			CString defStr = g_devices.GetItemName(defId);
			m_devList.SelectString(0,defStr);
		}

		m_pkUpDelay.Format(_T("%d"),g_settings.m_ringCount);
		GetDlgItem(IDC_RESTART)->EnableWindow(TRUE);
	}
		break;

											  /* Line is restarting */
	case ST_RESTART:	
		break;
								 /* Line is ready to receive a call */
	case ST_READY:
		break;
				/* Line is busy, communicating or restarting or ... */
	case ST_BUSY:
		break;
										  /* Shutting down the line */
	case ST_SHUTDN:	
		break;
											 /* Some one is ringing */
	case ST_RING:
		break;
								  /* Line just picked up (Answered) */
	case ST_PICKED_UP:
		GetDlgItem(IDC_RESTART)->EnableWindow(FALSE);
		break;

							/* Somthing failed! refer to errors.log */
	case ST_FAILED:
		GetDlgItem(IDC_RESTART)->EnableWindow(FALSE);
		GetDlgItem(IDC_START)->SetWindowText(_T("Start"));
		break;
										 /* New key pressed by user */
	case ST_DTMF:
		tmp.Format(_T(" [%d]"),wParam);
		m_status = stat[lParam];
		m_status += tmp;
		
		m_logList.AddString(m_status);
		UpdateData(FALSE);

		m_logList.SetCurSel(m_logList.GetCount()-1);
								  /* Force status to show all messages! */
		GetDlgItem(IDC_STATUS)->UpdateWindow();
		GetDlgItem(IDC_LOG)->UpdateWindow();

		return 0;
		break;

									 /* Caller ID became available! */
	case ST_CALLER_ID:
		
		m_callerID = g_phoneNumber;
		UpdateData(FALSE);
		GetDlgItem(IDC_CALLER_ID)->UpdateWindow();
		break;
	}
						 /* This is the main part, I used to display a 
										   text regarding any event */
	m_status = stat[lParam];
	m_logList.AddString(stat[lParam]);
	UpdateData(FALSE);
						/* Move current record down by each message */
	m_logList.SetCurSel(m_logList.GetCount()-1);
							  /* Force status to show all messages! */
	GetDlgItem(IDC_STATUS)->UpdateWindow();
	GetDlgItem(IDC_LOG)->UpdateWindow();
	return 0;
}

/*********************************************************************
 * Restart the device
 ********************************************************************/
void CCP_IVRDlg::OnRestart() 
{
	// UNDONE: Here we don't give true device id to the restart!
	// Restart will correct this.									(( WARNING ))
	UpdateData(TRUE);

	UINT index = m_devList.GetCurSel();
	if (index==CB_ERR) {
		AfxMessageBox(_T("Invalid Device ID."));
		return;
	}

	g_settings.m_deviceId = g_devices.GetItemId(index);
	g_settings.m_ringCount = _wtoi(m_pkUpDelay);
	m_manager.Restart();
}

/*********************************************************************
 * Clear log box
 ********************************************************************/
void CCP_IVRDlg::OnClearLog() 
{
	m_logList.ResetContent();
}

/*********************************************************************
 * Switch between AutoAnswer and Speaker/phone. Good for headset!
 ********************************************************************/
void CCP_IVRDlg::OnHookMicSpk() 
{
	m_manager.EnableSpeakerHook();
}

/*********************************************************************
 * Exit the App.
 ********************************************************************/
void CCP_IVRDlg::OnCancel() 
{
	m_manager.SleepAll();
	PostQuitMessage(0);
	//CDialog::OnCancel();
}

⌨️ 快捷键说明

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