TAPISampleDlg.cpp
上传用户:c1686688
上传日期:2015-06-15
资源大小:73k
文件大小:11k
源码类别:

TAPI编程

开发平台:

Visual C++

  1. // TAPISampleDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TAPISample.h"
  5. #include "TAPISampleDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CTAPISampleDlg dialog
  50. CTAPISampleDlg::CTAPISampleDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CTAPISampleDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CTAPISampleDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CTAPISampleDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CTAPISampleDlg)
  63. // NOTE: the ClassWizard will add DDX and DDV calls here
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CTAPISampleDlg, CDialog)
  67. //{{AFX_MSG_MAP(CTAPISampleDlg)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_QUERYDRAGICON()
  71. ON_BN_CLICKED(IDC_BTNWAIT, OnBtnWait)
  72. ON_BN_CLICKED(IDC_BTNOPEN, OnBtnOpen)
  73. ON_BN_CLICKED(IDC_BTNDIAL, OnBtnDial)
  74. ON_BN_CLICKED(IDC_BTNCOMHANDLE, OnBtnComHandle)
  75. ON_BN_CLICKED(IDC_BTNREAD, OnBtnRead)
  76. ON_BN_CLICKED(IDC_BTNWRITE, OnBtnWrite)
  77. ON_BN_CLICKED(IDC_BTNREADCLEAR, OnBtnReadClear)
  78. ON_BN_CLICKED(IDC_BTNWRITECLEAR, OnBtnWriteClear)
  79. //}}AFX_MSG_MAP
  80. END_MESSAGE_MAP()
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CTAPISampleDlg message handlers
  83. BOOL CTAPISampleDlg::OnInitDialog()
  84. {
  85. CDialog::OnInitDialog();
  86. // Add "About..." menu item to system menu.
  87. // IDM_ABOUTBOX must be in the system command range.
  88. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  89. ASSERT(IDM_ABOUTBOX < 0xF000);
  90. CMenu* pSysMenu = GetSystemMenu(FALSE);
  91. if (pSysMenu != NULL)
  92. {
  93. CString strAboutMenu;
  94. strAboutMenu.LoadString(IDS_ABOUTBOX);
  95. if (!strAboutMenu.IsEmpty())
  96. {
  97. pSysMenu->AppendMenu(MF_SEPARATOR);
  98. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  99. }
  100. }
  101. // Set the icon for this dialog.  The framework does this automatically
  102. //  when the application's main window is not a dialog
  103. SetIcon(m_hIcon, TRUE); // Set big icon
  104. SetIcon(m_hIcon, FALSE); // Set small icon
  105. m_hWaitForCallThread= NULL;
  106. m_hSerialHandle= NULL;
  107. m_bStopReadThread = true;
  108. m_hReadThread = NULL;
  109. GetDlgItem(IDC_BTNWAIT)->EnableWindow(false);
  110. GetDlgItem(IDC_BTNDIAL)->EnableWindow(false);
  111. GetDlgItem(IDC_BTNWRITE)->EnableWindow(false);
  112. GetDlgItem(IDC_BTNREAD)->EnableWindow(false);
  113. return TRUE;  // return TRUE  unless you set the focus to a control
  114. }
  115. void CTAPISampleDlg::OnSysCommand(UINT nID, LPARAM lParam)
  116. {
  117. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  118. {
  119. CAboutDlg dlgAbout;
  120. dlgAbout.DoModal();
  121. }
  122. else
  123. {
  124. CDialog::OnSysCommand(nID, lParam);
  125. }
  126. }
  127. // If you add a minimize button to your dialog, you will need the code below
  128. //  to draw the icon.  For MFC applications using the document/view model,
  129. //  this is automatically done for you by the framework.
  130. void CTAPISampleDlg::OnPaint() 
  131. {
  132. if (IsIconic())
  133. {
  134. CPaintDC dc(this); // device context for painting
  135. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  136. // Center icon in client rectangle
  137. int cxIcon = GetSystemMetrics(SM_CXICON);
  138. int cyIcon = GetSystemMetrics(SM_CYICON);
  139. CRect rect;
  140. GetClientRect(&rect);
  141. int x = (rect.Width() - cxIcon + 1) / 2;
  142. int y = (rect.Height() - cyIcon + 1) / 2;
  143. // Draw the icon
  144. dc.DrawIcon(x, y, m_hIcon);
  145. }
  146. else
  147. {
  148. CDialog::OnPaint();
  149. }
  150. }
  151. // The system calls this to obtain the cursor to display while the user drags
  152. //  the minimized window.
  153. HCURSOR CTAPISampleDlg::OnQueryDragIcon()
  154. {
  155. return (HCURSOR) m_hIcon;
  156. }
  157. void CTAPISampleDlg::OnBtnReadClear() 
  158. {
  159. SetDlgItemText(IDC_EDTREAD,"");
  160. SetDlgItemText(IDC_EDTREADLEN,"");
  161. }
  162. void CTAPISampleDlg::OnBtnWriteClear() 
  163. {
  164. SetDlgItemText(IDC_EDTWRITE,"");
  165. SetDlgItemText(IDC_EDTWRITELEN,"");
  166. }
  167. void CTAPISampleDlg::OnBtnOpen() 
  168. {
  169. int nRet;
  170. CString cszText;
  171. char *szError;
  172. GetDlgItemText(IDC_BTNOPEN,cszText);
  173. if(cszText == "Open")
  174. {
  175. nRet = cCall.Open();
  176. if(nRet)
  177. {
  178. cCall.GetErrorString(nRet,szError);
  179. AfxMessageBox(szError,MB_ICONERROR|MB_OK);
  180. return;
  181. }
  182. SetDlgItemText(IDC_BTNOPEN,"Close");
  183. GetDlgItem(IDC_BTNWAIT)->EnableWindow(true);
  184. GetDlgItem(IDC_BTNDIAL)->EnableWindow(true);
  185. }
  186. else
  187. {
  188. nRet = cCall.Close();
  189. if(nRet)
  190. {
  191. cCall.GetErrorString(nRet,szError);
  192. AfxMessageBox(szError,MB_ICONERROR|MB_OK);
  193. return;
  194. }
  195. SetDlgItemText(IDC_BTNOPEN,"Open");
  196. GetDlgItem(IDC_BTNWAIT)->EnableWindow(false);
  197. GetDlgItem(IDC_BTNDIAL)->EnableWindow(false);
  198. if(m_hWaitForCallThread)
  199. {
  200. TerminateThread(m_hWaitForCallThread,0);
  201. CloseHandle(m_hWaitForCallThread);
  202. m_hWaitForCallThread=NULL;
  203. }
  204. }
  205. }
  206. void CTAPISampleDlg::OnBtnWait() 
  207. {// since the waiting function holds we'll put it in a thread
  208. if(m_hWaitForCallThread)
  209. {
  210. TerminateThread(m_hWaitForCallThread,0);
  211. CloseHandle(m_hWaitForCallThread);
  212. m_hWaitForCallThread=NULL;
  213. }
  214. /// 这里创立线程如何, 线程句柄
  215. m_hWaitForCallThread = CreateThread(NULL,0,WaitForCallThread,this,0,0);
  216. GetDlgItem(IDC_BTNWAIT)->EnableWindow(false); // making it disable
  217. }
  218. void CTAPISampleDlg::OnBtnDial() 
  219. {
  220. CString cszText;
  221. char *szText;
  222. GetDlgItemText(IDC_EDTDIALNUMBER,cszText);
  223. int nRet = cCall.MakeOutgoingCall(cszText.GetBuffer(0));
  224. cszText.ReleaseBuffer();
  225. if(nRet)
  226. {
  227. cCall.GetErrorString(nRet,szText);
  228. AfxMessageBox(szText,MB_ICONERROR|MB_OK);
  229. return;
  230. }
  231. GetDlgItem(IDC_BTNCOMHANDLE)->EnableWindow(true);
  232. }
  233. void CTAPISampleDlg::OnBtnComHandle() 
  234. {
  235. long lRet;
  236. CString cszText;
  237. char *szError;
  238. GetDlgItemText(IDC_BTNCOMHANDLE,cszText);
  239. if(cszText == "Get Handle")
  240. {
  241. m_hSerialHandle = cCall.GetHandle("comm/datamodem",&lRet);
  242. if(lRet)
  243. {
  244. m_hSerialHandle = NULL;
  245. cCall.GetErrorString(lRet,szError);
  246. AfxMessageBox(szError,MB_ICONERROR|MB_OK);
  247. return;
  248. }
  249. SetDlgItemText(IDC_BTNCOMHANDLE,"Close Handle");
  250. GetDlgItem(IDC_BTNWRITE)->EnableWindow(true);
  251. GetDlgItem(IDC_BTNREAD)->EnableWindow(true);
  252. SetDlgItemText(IDC_BTNREAD,"Read");
  253. }
  254. else
  255. {
  256. if(m_hReadThread)
  257. {
  258. m_bStopReadThread = true;
  259. Sleep(1000);
  260. TerminateThread(m_hReadThread,0);
  261. CloseHandle(m_hReadThread);
  262. m_hReadThread=NULL;
  263. }
  264. CloseHandle(m_hSerialHandle);
  265. m_hSerialHandle = NULL;
  266. SetDlgItemText(IDC_BTNCOMHANDLE,"Get Handle");
  267. GetDlgItem(IDC_BTNWRITE)->EnableWindow(false);
  268. GetDlgItem(IDC_BTNREAD)->EnableWindow(false);
  269. SetDlgItemText(IDC_BTNREAD,"Read");
  270. }
  271. }
  272. void CTAPISampleDlg::OnBtnRead() 
  273. {
  274. CString cszText;
  275. GetDlgItemText(IDC_BTNREAD,cszText);
  276. if(cszText == "Read")
  277. {
  278. m_bStopReadThread = false;
  279. m_hReadThread = CreateThread(NULL,0,ReadThread,this,0,0);
  280. SetDlgItemText(IDC_BTNREAD,"Stop");
  281. }
  282. else
  283. {
  284. if(m_hReadThread)
  285. {
  286. m_bStopReadThread = true;
  287. Sleep(1000);
  288. TerminateThread(m_hReadThread,0);
  289. CloseHandle(m_hReadThread);
  290. m_hReadThread=NULL;
  291. }
  292. SetDlgItemText(IDC_BTNREAD,"Read");
  293. }
  294. }
  295. void CTAPISampleDlg::OnBtnWrite() 
  296. {
  297. char *szBuff = (char*)malloc(512);
  298. DWORD dwWriteLen =0;
  299. int nRet;
  300. char *szText;
  301. CString cszText;
  302. OVERLAPPED ov;
  303. ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  304. ov.Offset = 0;
  305. ov.OffsetHigh = 0;
  306. GetDlgItemText(IDC_EDTWRITE,szBuff,512);
  307. nRet = WriteFile(m_hSerialHandle,szBuff,strlen(szBuff),&dwWriteLen,&ov);
  308. if(!nRet)
  309. {
  310. if(!nRet)
  311. {
  312. nRet = GetLastError();
  313. if(nRet != ERROR_IO_PENDING)
  314. {
  315. cCall.GetErrorString(nRet,szText);
  316. AfxMessageBox(szText,MB_ICONERROR|MB_OK);
  317. free(szBuff);
  318. return;
  319. }
  320. }
  321. switch(WaitForSingleObject(ov.hEvent,INFINITE))
  322. {
  323. case WAIT_OBJECT_0:
  324. GetOverlappedResult(m_hSerialHandle, &ov, &dwWriteLen,FALSE);
  325. break;
  326. default:
  327. break;
  328. };
  329. }
  330. free(szBuff);
  331. SetDlgItemInt(IDC_EDTWRITELEN,GetDlgItemInt(IDC_EDTWRITELEN)+dwWriteLen);
  332. }
  333. DWORD WINAPI CTAPISampleDlg::WaitForCallThread(LPVOID lpVoid)
  334. {
  335. CTAPISampleDlg *pcDlg = (CTAPISampleDlg *)lpVoid;
  336. char *szError;
  337. int nRet = pcDlg->cCall.GetIncomingCall();
  338. if(nRet)
  339. {
  340. pcDlg->cCall.GetErrorString(nRet,szError);
  341. AfxMessageBox(szError,MB_ICONERROR|MB_OK);
  342. return nRet;
  343. }
  344. pcDlg->GetDlgItem(IDC_BTNWAIT)->EnableWindow(true);
  345. pcDlg->GetDlgItem(IDC_BTNCOMHANDLE)->EnableWindow(true);
  346. return 0;
  347. }
  348. DWORD WINAPI CTAPISampleDlg::ReadThread(LPVOID lpVoid)
  349. {
  350. CTAPISampleDlg *pcDlg = (CTAPISampleDlg *)lpVoid;
  351. char *szBuff = (char*)malloc(512);
  352. DWORD dwReadLen;
  353. int nRet;
  354. char *szText;
  355. CString cszText;
  356. OVERLAPPED ov;
  357. ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  358. ov.Offset = 0;
  359. ov.OffsetHigh = 0;
  360. for(;!pcDlg->m_bStopReadThread;)
  361. {
  362. nRet = ReadFile(pcDlg->m_hSerialHandle,szBuff,512,&dwReadLen,&ov);
  363. if(!nRet)
  364. {
  365. nRet = GetLastError();
  366. if(nRet != ERROR_IO_PENDING)
  367. {
  368. pcDlg->cCall.GetErrorString(nRet,szText);
  369. AfxMessageBox(szText,MB_ICONERROR|MB_OK);
  370. free(szBuff);
  371. return nRet;
  372. }
  373. }
  374. switch(WaitForSingleObject(ov.hEvent,INFINITE))
  375. {
  376. case WAIT_OBJECT_0:
  377. GetOverlappedResult(pcDlg->m_hSerialHandle, &ov, &dwReadLen,FALSE);
  378. break;
  379. default:
  380. break;
  381. };
  382. szBuff[dwReadLen] = 0;
  383. pcDlg->GetDlgItemText(IDC_EDTREAD,cszText);
  384. cszText += szBuff;
  385. pcDlg->SetDlgItemText(IDC_EDTREAD,cszText);
  386. pcDlg->SetDlgItemInt(IDC_EDTREADLEN,pcDlg->GetDlgItemInt(IDC_EDTREADLEN)+dwReadLen);
  387. ((CEdit*)pcDlg->GetDlgItem(IDC_EDTREAD))->LineScroll(((CEdit*)pcDlg->GetDlgItem(IDC_EDTREAD))->GetLineCount());
  388. }
  389. free(szBuff);
  390. return 0;
  391. }