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

📄 tmautodialdlg.cpp

📁 单路开发文档和源代码例子12-13 VC源码 自动拨号
💻 CPP
字号:
// TmAutoDialDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TmAutoDial.h"
#include "TmAutoDialDlg.h"
#include "fstream.h"

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

#define		INIFILE_PATH		"autodial.ini"

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CTmAutoDialDlg dialog

CTmAutoDialDlg::CTmAutoDialDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTmAutoDialDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTmAutoDialDlg)
	m_strExterDial = _T("");
	m_strCodePath = _T("");
	m_DateTimeBegin = 0;
	m_DateTimeEnd = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTmAutoDialDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTmAutoDialDlg)
	DDX_Control(pDX, IDC_STOP, m_Stop);
	DDX_Control(pDX, IDC_STARTDIAL, m_Start);
	DDX_Control(pDX, IDC_STSTATUS, m_StStatus);
	DDX_Control(pDX, IDC_CODEPATH, m_cCodePath);
	DDX_Control(pDX, IDC_CODELIST, m_CodeList);
	DDX_Text(pDX, IDC_EXTERDIAL, m_strExterDial);
	DDX_Text(pDX, IDC_CODEPATH, m_strCodePath);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEBEGIN, m_DateTimeBegin);
	DDX_DateTimeCtrl(pDX, IDC_DATETIMEEND, m_DateTimeEnd);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTmAutoDialDlg, CDialog)
	//{{AFX_MSG_MAP(CTmAutoDialDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_STARTDIAL, OnStartdial)
	ON_BN_CLICKED(IDC_SELECTPATH, OnSelectpath)
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_STOP, OnStop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTmAutoDialDlg message handlers

BOOL CTmAutoDialDlg::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);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	m_iCurrentDialID = 0;
	m_nDialEndWait = NULL;
	m_nStartDialWait = NULL;
	m_nCheckValidateTimer = NULL;

	InitIni();

	m_TmDialProc.InitWindow();
	m_TmDialProc.SetCallbackhWnd(m_hWnd);

	if(TV_Initialize())
	{
		SetWindowText("自动拨号 -> 设备正常连接");
		m_StStatus.SetWindowText("准备就绪");
	}else
	{
		SetWindowText("自动拨号 -> 设备没有正常连接");
		m_StStatus.SetWindowText("设备没有正常连接");
	}
	TV_RegMsgWnd(GetSafeHwnd(),FALSE);//注册接受消息的窗口
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTmAutoDialDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTmAutoDialDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTmAutoDialDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void	CTmAutoDialDlg::InitIni()
{
	CString strPath=GetModulePath()+INIFILE_PATH;
	CIniFile IniFile;
	IniFile.SetPath(strPath);
	IniFile.ReadFile();

	m_DateTimeBegin=IniFile.GetValueI("AUTO","bt");
	if(m_DateTimeBegin <= 0 ) m_DateTimeBegin = CTime::GetCurrentTime();
	m_DateTimeEnd =IniFile.GetValueI("AUTO","et");
	if(m_DateTimeEnd <= 0) m_DateTimeEnd=m_DateTimeBegin;

	m_strExterDial = IniFile.GetValue("AUTO","ED");
	m_strCodePath = IniFile.GetValue("AUTO","PATH");

	GetCodeListFromText(m_strCodePath);
	UpdateData(FALSE);

}

void	CTmAutoDialDlg::SaveIni()
{
	UpdateData(TRUE);
	CString strPath=GetModulePath()+INIFILE_PATH;
	CIniFile IniFile;
	IniFile.SetPath(strPath);
	IniFile.ReadFile();

	IniFile.SetValueI("AUTO","bt",m_DateTimeBegin.GetTime());
	
	IniFile.SetValueI("AUTO","et",m_DateTimeEnd.GetTime());
	
	IniFile.SetValue("AUTO","ED",m_strExterDial);
	IniFile.SetValue("AUTO","PATH",m_strCodePath);
	IniFile.WriteFile();

}

CString CTmAutoDialDlg::GetModulePath()
{
	char szSourcePath[_MAX_PATH];
	GetModuleFileName(NULL, szSourcePath, _MAX_PATH); 
    *(strrchr(szSourcePath, '\\') + 1) = '\0';          
	CString RetStr=szSourcePath;
	return RetStr;
}

void CTmAutoDialDlg::OnStartdial() 
{
	if(!TV_GetInitState())
	{
		MessageBox("设备没有正常打开,请重新插入设备","错误",MB_OK|MB_ICONWARNING);
		return;
	}
	UpdateData(TRUE);
	m_iCurrentDialID= 0;
	if(!m_nCheckValidateTimer)
	{
		m_nCheckValidateTimer = SetTimer(0x20,1000,NULL);
		m_StStatus.SetWindowText("正在检测合法拨号时间...");
	}
	m_Start.EnableWindow(FALSE);
	m_Stop.EnableWindow(TRUE);
}

BOOL CTmAutoDialDlg::IsValidateTime()
{
	if(UpdateData(TRUE))//这样就可以随时可以更新出局号码
	{
		CTime ct=CTime::GetCurrentTime();
		long lbt=m_DateTimeBegin.GetTime();
		long let=m_DateTimeEnd.GetTime();
		
		CTime cct(m_DateTimeBegin.GetYear(),m_DateTimeBegin.GetMonth(),m_DateTimeBegin.GetDay(),ct.GetHour(),ct.GetMinute(),ct.GetSecond());
		long lCurt=cct.GetTime();

		if((lCurt >= lbt && lCurt < let )|| (lbt == let))//如果相等就当时间无效处理
		{
			return TRUE;
		}else
			return FALSE;
	}else
		return FALSE;
}

BOOL CTmAutoDialDlg::StartDial()
{
	UpdateData(TRUE);//这样就可以随时可以更新出局号码
	//检测时间是否合法,超过指定时间就停止
	if(IsValidateTime())
	{
		CString strCode;	
		if((int)m_iCurrentDialID >= m_CodeList.GetCount())//已经拨完了
		{
			m_StStatus.SetWindowText("自动拨号已停止,号码已经拨完");
			m_Start.EnableWindow(TRUE);
			m_Stop.EnableWindow(FALSE);
		}else
		{
			m_CodeList.GetText(m_iCurrentDialID,strCode);
			m_CodeList.SetCurSel(m_iCurrentDialID);
			m_iCurrentDialID++;
			m_TmDialProc.StartDial(m_strExterDial,strCode);
			m_StStatus.SetWindowText("正在呼叫->"+strCode);
		}
	}else//时间已到,提示拨号完成
	{
		m_StStatus.SetWindowText("自动拨号已停止,时间已到");
		m_Start.EnableWindow(TRUE);
		m_Stop.EnableWindow(FALSE);
	}
	return TRUE;
}

void CTmAutoDialDlg::OnSelectpath() 
{
	static char szFilter[] = "Text Files(*.txt)|*.txt|All Files (*.*)|*.*||";
 	CFileDialog dlgLoad(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, (LPCTSTR)szFilter, NULL);
	if(dlgLoad.DoModal()!=IDOK)
		return;
	CString str=dlgLoad.GetPathName();
	if(!str.IsEmpty())
	{
		m_cCodePath.SetWindowText(str);
		GetCodeListFromText(str);
		m_iCurrentDialID = 0;//重新开始
	}
}

//得到所有号码,一行为一个号码
void CTmAutoDialDlg::GetCodeListFromText(const CString &strPath)
{
	if(strPath.IsEmpty())
		return;
	m_CodeList.ResetContent();
	ifstream file;
	file.open(strPath);
	if(!file.fail())
	{
		char buf[MaxDataLen];//最多一次拨46个号码
		while (file.getline(buf,MaxDataLen))
		{
			m_CodeList.InsertString(m_CodeList.GetCount(),buf);
		}
		file.close();
	}
}

LRESULT CTmAutoDialDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if(message  == TD_CALLBACK_MESSAGE)
	{
		if(wParam == 0x1)//拨号错误,等待拨下一个
		{
			StartDialTimer();
		}
	}else if(message == TI_DIALEND_MESSAGE)
	{
		StartDialEndWaitTimer();//等待一定时间就自动挂断
	}else if(message == WM_USBPLUG_MESSAGE)
	{
		if(wParam == 0)
		{
			SetWindowText("自动拨号 -> 设备没有正常连接");
			m_StStatus.SetWindowText("设备没有正常连接");
		}else
		{
			SetWindowText("自动拨号 -> 设备正常连接");
			m_StStatus.SetWindowText("准备就绪");
		}
	}else if(message == TI_HANGUP_MESSAGE || message == TI_BUSY_MESSAGE)
	{
		m_TmDialProc.StopDial();
		StartDialTimer(2000);
	}else if(message == TI_TELHANGUP_MESSAGE)
	{
		TV_HangUpCtrl();
		StartDialTimer(2000);
	}else if(message == TI_TELOFFHOOK_MESSAGE)
	{
		TV_CloseDoPlay();
		//TV_HangUpCtrl();
	}
	return CDialog::WindowProc(message, wParam, lParam);
}


BOOL CTmAutoDialDlg::StartDialTimer(UINT nTime)//等待一定时间后开始拨号
{
	StopStartDialWait();
	m_nStartDialWait = SetTimer(0x10,nTime,NULL);//等待5秒
	m_StStatus.SetWindowText("等待呼叫下一个");
	return TRUE;
}

BOOL CTmAutoDialDlg::StartDialEndWaitTimer()//拨号结束后等待一定时间就挂段
{
	if(TV_GetTelState())
	{
		TV_HangUpCtrl();
	}else
	{
		TV_OpenDoPlay();
	}
	StopDialEndWaitTimer();
	m_nDialEndWait = SetTimer(0x11,15000,NULL);
	m_StStatus.SetWindowText("拨号完成,等待超时");
	return TRUE;
}

void CTmAutoDialDlg::StopDialEndWaitTimer()
{
	if(m_nDialEndWait)
	{
		KillTimer(m_nDialEndWait);
		m_nDialEndWait = NULL;
	}
}

void CTmAutoDialDlg::StopStartDialWait()
{
	if(m_nStartDialWait)
	{
		KillTimer(m_nStartDialWait);
		m_nStartDialWait =NULL;
	}
}

void CTmAutoDialDlg::StopCheckValidate()
{
	if(m_nCheckValidateTimer)
	{
		KillTimer(m_nCheckValidateTimer);
		m_nCheckValidateTimer = NULL;
	}
}

void CTmAutoDialDlg::OnDestroy() 
{
	TV_Disable();
	SaveIni();
	StopCheckValidate();
	StopDialEndWaitTimer();
	StopStartDialWait();
	m_TmDialProc.StopDial();
	CDialog::OnDestroy();	
}

void CTmAutoDialDlg::OnTimer(UINT nIDEvent) 
{
	if(nIDEvent == m_nStartDialWait)
	{
		StopStartDialWait();
		StartDial();
	}else if(nIDEvent == m_nDialEndWait)
	{
		StopDialEndWaitTimer();
		m_TmDialProc.StopDial();
		StartDialTimer();
	}else if(nIDEvent == m_nCheckValidateTimer )
	{
		if(IsValidateTime())
		{
			StopCheckValidate();
			StartDial();//时间合法,开始呼叫
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CTmAutoDialDlg::OnStop() 
{
	m_TmDialProc.StopDial();
	m_StStatus.SetWindowText("自动拨号已经停止");
	m_Start.EnableWindow(TRUE);
	m_Stop.EnableWindow(FALSE);
}

⌨️ 快捷键说明

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