TAPISampleDlg.cpp.svn-base
上传用户: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. m_hWaitForCallThread = CreateThread(NULL,0,WaitForCallThread,this,0,0);
  215. GetDlgItem(IDC_BTNWAIT)->EnableWindow(false); // making it disable
  216. }
  217. void CTAPISampleDlg::OnBtnDial() 
  218. {
  219. CString cszText;
  220. char *szText;
  221. GetDlgItemText(IDC_EDTDIALNUMBER,cszText);
  222. int nRet = cCall.MakeOutgoingCall(cszText.GetBuffer(0));
  223. cszText.ReleaseBuffer();
  224. if(nRet)
  225. {
  226. cCall.GetErrorString(nRet,szText);
  227. AfxMessageBox(szText,MB_ICONERROR|MB_OK);
  228. return;
  229. }
  230. GetDlgItem(IDC_BTNCOMHANDLE)->EnableWindow(true);
  231. }
  232. void CTAPISampleDlg::OnBtnComHandle() 
  233. {
  234. long lRet;
  235. CString cszText;
  236. char *szError;
  237. GetDlgItemText(IDC_BTNCOMHANDLE,cszText);
  238. if(cszText == "Get Handle")
  239. {
  240. m_hSerialHandle = cCall.GetHandle("comm/datamodem",&lRet);
  241. if(lRet)
  242. {
  243. m_hSerialHandle = NULL;
  244. cCall.GetErrorString(lRet,szError);
  245. AfxMessageBox(szError,MB_ICONERROR|MB_OK);
  246. return;
  247. }
  248. SetDlgItemText(IDC_BTNCOMHANDLE,"Close Handle");
  249. GetDlgItem(IDC_BTNWRITE)->EnableWindow(true);
  250. GetDlgItem(IDC_BTNREAD)->EnableWindow(true);
  251. SetDlgItemText(IDC_BTNREAD,"Read");
  252. }
  253. else
  254. {
  255. if(m_hReadThread)
  256. {
  257. m_bStopReadThread = true;
  258. Sleep(1000);
  259. TerminateThread(m_hReadThread,0);
  260. CloseHandle(m_hReadThread);
  261. m_hReadThread=NULL;
  262. }
  263. CloseHandle(m_hSerialHandle);
  264. m_hSerialHandle = NULL;
  265. SetDlgItemText(IDC_BTNCOMHANDLE,"Get Handle");
  266. GetDlgItem(IDC_BTNWRITE)->EnableWindow(false);
  267. GetDlgItem(IDC_BTNREAD)->EnableWindow(false);
  268. SetDlgItemText(IDC_BTNREAD,"Read");
  269. }
  270. }
  271. void CTAPISampleDlg::OnBtnRead() 
  272. {
  273. CString cszText;
  274. GetDlgItemText(IDC_BTNREAD,cszText);
  275. if(cszText == "Read")
  276. {
  277. m_bStopReadThread = false;
  278. m_hReadThread = CreateThread(NULL,0,ReadThread,this,0,0);
  279. SetDlgItemText(IDC_BTNREAD,"Stop");
  280. }
  281. else
  282. {
  283. if(m_hReadThread)
  284. {
  285. m_bStopReadThread = true;
  286. Sleep(1000);
  287. TerminateThread(m_hReadThread,0);
  288. CloseHandle(m_hReadThread);
  289. m_hReadThread=NULL;
  290. }
  291. SetDlgItemText(IDC_BTNREAD,"Read");
  292. }
  293. }
  294. void CTAPISampleDlg::OnBtnWrite() 
  295. {
  296. char *szBuff = (char*)malloc(512);
  297. DWORD dwWriteLen =0;
  298. int nRet;
  299. char *szText;
  300. CString cszText;
  301. OVERLAPPED ov;
  302. ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  303. ov.Offset = 0;
  304. ov.OffsetHigh = 0;
  305. GetDlgItemText(IDC_EDTWRITE,szBuff,512);
  306. nRet = WriteFile(m_hSerialHandle,szBuff,strlen(szBuff),&dwWriteLen,&ov);
  307. if(!nRet)
  308. {
  309. if(!nRet)
  310. {
  311. nRet = GetLastError();
  312. if(nRet != ERROR_IO_PENDING)
  313. {
  314. cCall.GetErrorString(nRet,szText);
  315. AfxMessageBox(szText,MB_ICONERROR|MB_OK);
  316. free(szBuff);
  317. return;
  318. }
  319. }
  320. switch(WaitForSingleObject(ov.hEvent,INFINITE))
  321. {
  322. case WAIT_OBJECT_0:
  323. GetOverlappedResult(m_hSerialHandle, &ov, &dwWriteLen,FALSE);
  324. break;
  325. default:
  326. break;
  327. };
  328. }
  329. free(szBuff);
  330. SetDlgItemInt(IDC_EDTWRITELEN,GetDlgItemInt(IDC_EDTWRITELEN)+dwWriteLen);
  331. }
  332. DWORD WINAPI CTAPISampleDlg::WaitForCallThread(LPVOID lpVoid)
  333. {
  334. CTAPISampleDlg *pcDlg = (CTAPISampleDlg *)lpVoid;
  335. char *szError;
  336. int nRet = pcDlg->cCall.GetIncomingCall();
  337. if(nRet)
  338. {
  339. pcDlg->cCall.GetErrorString(nRet,szError);
  340. AfxMessageBox(szError,MB_ICONERROR|MB_OK);
  341. return nRet;
  342. }
  343. pcDlg->GetDlgItem(IDC_BTNWAIT)->EnableWindow(true);
  344. pcDlg->GetDlgItem(IDC_BTNCOMHANDLE)->EnableWindow(true);
  345. return 0;
  346. }
  347. DWORD WINAPI CTAPISampleDlg::ReadThread(LPVOID lpVoid)
  348. {
  349. CTAPISampleDlg *pcDlg = (CTAPISampleDlg *)lpVoid;
  350. char *szBuff = (char*)malloc(512);
  351. DWORD dwReadLen;
  352. int nRet;
  353. char *szText;
  354. CString cszText;
  355. OVERLAPPED ov;
  356. ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  357. ov.Offset = 0;
  358. ov.OffsetHigh = 0;
  359. for(;!pcDlg->m_bStopReadThread;)
  360. {
  361. nRet = ReadFile(pcDlg->m_hSerialHandle,szBuff,512,&dwReadLen,&ov);
  362. if(!nRet)
  363. {
  364. nRet = GetLastError();
  365. if(nRet != ERROR_IO_PENDING)
  366. {
  367. pcDlg->cCall.GetErrorString(nRet,szText);
  368. AfxMessageBox(szText,MB_ICONERROR|MB_OK);
  369. free(szBuff);
  370. return nRet;
  371. }
  372. }
  373. switch(WaitForSingleObject(ov.hEvent,INFINITE))
  374. {
  375. case WAIT_OBJECT_0:
  376. GetOverlappedResult(pcDlg->m_hSerialHandle, &ov, &dwReadLen,FALSE);
  377. break;
  378. default:
  379. break;
  380. };
  381. szBuff[dwReadLen] = 0;
  382. pcDlg->GetDlgItemText(IDC_EDTREAD,cszText);
  383. cszText += szBuff;
  384. pcDlg->SetDlgItemText(IDC_EDTREAD,cszText);
  385. pcDlg->SetDlgItemInt(IDC_EDTREADLEN,pcDlg->GetDlgItemInt(IDC_EDTREADLEN)+dwReadLen);
  386. ((CEdit*)pcDlg->GetDlgItem(IDC_EDTREAD))->LineScroll(((CEdit*)pcDlg->GetDlgItem(IDC_EDTREAD))->GetLineCount());
  387. }
  388. free(szBuff);
  389. return 0;
  390. }