AddrBook.cpp
上传用户:oldpeter23
上传日期:2013-01-09
资源大小:1111k
文件大小:6k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. //NetTalk
  2. /*------------------------------------------------------------------------------*
  3.  =============================
  4.    模块名称: AddrBook.cpp
  5.  =============================
  6.  
  7.  [版权]
  8.  
  9.    2000-2002  115软件工厂  版权所有
  10.                                               
  11. *------------------------------------------------------------------------------*/
  12. #include "WndX.h"
  13. #include "AddrBook.h"
  14. #include "resource.h"
  15. #include <stdio.h>
  16. #include "NewMsgBox.h"
  17. /*------------------------------------------------------------------------------*/
  18. CAddrBook::CAddrBook()
  19. {
  20. m_bModify=FALSE;//设置modify修改
  21. }
  22. /*------------------------------------------------------------------------------*/
  23. CAddrBook::~CAddrBook()
  24. {
  25. }
  26. /*------------------------------------------------------------------------------*/
  27. LRESULT CAddrBook::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  28. {
  29. switch(uMsg)
  30. {
  31. case WM_COMMAND:
  32. return OnCommand(wParam,lParam);
  33. case WM_INITDIALOG:
  34. return OnInitDialog();
  35. case WM_CLOSE://当通讯录被关闭时,检查modify标志以保存
  36. if(m_bModify)
  37. SaveList();
  38. OnClose();
  39. break;
  40. default:
  41. return CGraphDlg::WndProc(uMsg,wParam,lParam);
  42. }
  43. return TRUE;
  44. }
  45. /*------------------------------------------------------------------------------*/
  46. BOOL CAddrBook::OnInitDialog()
  47. {
  48. CGraphDlg::OnInitDialog();
  49. SetWindowText(m_hWnd,"通讯录");
  50. HWND h=GetDlgItem(m_hWnd,IDC_LIST);
  51. LVCOLUMN lvc;
  52. lvc.mask=LVCF_TEXT|LVCF_WIDTH;
  53. lvc.cx=60;
  54. lvc.pszText="姓名";
  55. ListView_InsertColumn(h,0,&lvc);
  56. lvc.pszText="计算机名/IP";
  57. lvc.cx=100;
  58. ListView_InsertColumn(h,1,&lvc);
  59. //载入通讯录
  60. LoadList();
  61. return FALSE;
  62. }
  63. /*------------------------------------------------------------------------------*/
  64. BOOL CAddrBook::OnCommand(WPARAM wParam, LPARAM lParam)
  65. {
  66. CGraphDlg::OnCommand(wParam,lParam);
  67. switch(LOWORD(wParam))
  68. {
  69. case IDC_EDIT://编辑
  70. {
  71. HWND h=GetDlgItem(m_hWnd,IDC_LIST);
  72. int iItem=ListView_GetNextItem(h,-1,LVNI_SELECTED);
  73. if(iItem>=0)
  74. {
  75. CModifyDlg md;
  76. ListView_GetItemText(h,iItem,0,md.m_szName,64);
  77. ListView_GetItemText(h,iItem,1,md.m_szIP,64);
  78. if(md.DoModal(LPCTSTR(IDD_MODIFY),m_hWnd))
  79. {
  80. ListView_SetItemText(h,iItem,0,md.m_szName);
  81. ListView_SetItemText(h,iItem,1,md.m_szIP);
  82. m_bModify=TRUE;
  83. }
  84. }
  85. }
  86. break;
  87. case IDC_ADD://添加
  88. {
  89. HWND h=GetDlgItem(m_hWnd,IDC_LIST);
  90. CModifyDlg md;
  91. if(md.DoModal(LPCTSTR(IDD_MODIFY),m_hWnd))
  92. {
  93. LVITEM li;
  94. li.iSubItem=0;
  95. li.mask=LVIF_TEXT;
  96. li.iItem=10000;
  97. li.pszText=md.m_szName;
  98. int iItem=ListView_InsertItem(h,&li);
  99. ListView_SetItemText(h,iItem,1,md.m_szIP);
  100. m_bModify=TRUE;
  101. }
  102. }
  103. break;
  104. case IDC_DEL://删除
  105. {
  106. HWND h=GetDlgItem(m_hWnd,IDC_LIST);
  107. int iItem=ListView_GetNextItem(h,-1,LVNI_SELECTED);
  108. if(iItem>=0)
  109. {
  110. if(CMsgBox::MessageBox(m_hWnd,IDS_MSG_DELREC,IDS_WAR,MB_YESNO,IDI_INFO)==IDYES)
  111. {
  112. ListView_DeleteItem(h,iItem);
  113. m_bModify=TRUE;
  114. }
  115. }
  116. }
  117. break;
  118. case IDC_C://呼叫
  119. {
  120. HWND h=GetDlgItem(m_hWnd,IDC_LIST);
  121. int iItem=ListView_GetNextItem(h,-1,LVNI_SELECTED);
  122. if(iItem>=0)
  123. {
  124. char szIP[64];
  125. ListView_GetItemText(h,iItem,1,szIP,64);
  126. HWND hp=GetParent(m_hWnd);
  127. SendMessage(m_hWnd,WM_CLOSE,0,0);
  128. SendMessage(hp,WM_COMMAND,0,(LPARAM)szIP);
  129. }
  130. }
  131. break;
  132. }
  133. return TRUE;
  134. }
  135. /*------------------------------------------------------------------------------*/
  136. //载入通讯录
  137. void CAddrBook::LoadList()
  138. {
  139. //得到程序所在路径
  140. char szPath[256];
  141. GetModuleFileName(0,szPath,255);
  142. int i;
  143. for(i=strlen(szPath);i>0;i--)
  144. {
  145. if(szPath[i]=='\')
  146. {
  147. szPath[i+1]=0;
  148. break;
  149. }
  150. }
  151. //
  152. strcpy(szPath+strlen(szPath),"addr.db");
  153. HWND h=GetDlgItem(m_hWnd,IDC_LIST);
  154. //得到记录数
  155. int count=GetPrivateProfileInt("addr","count",0,szPath);
  156. char sz[64];
  157. LVITEM li;
  158. li.iItem=10000;
  159. li.mask=LVIF_TEXT;
  160. li.pszText=sz;
  161. //逐条读出记录,并添加到列表中
  162. for(i=0;i<count;i++)
  163. {
  164. char s[10];
  165. sprintf(s,"%da",i);
  166. li.iSubItem=0;
  167. GetPrivateProfileString("addr",s,"",sz,64,szPath);
  168. int iItem=ListView_InsertItem(h,&li);
  169. li.iSubItem=1;
  170. sprintf(s,"%db",i);
  171. GetPrivateProfileString("addr",s,"",sz,64,szPath);
  172. ListView_SetItemText(h,iItem,1,sz);
  173. }
  174. }
  175. /*------------------------------------------------------------------------------*/
  176. //保存通讯录
  177. void CAddrBook::SaveList()
  178. {
  179. //得到程序所在路径
  180. char szPath[256];
  181. GetModuleFileName(0,szPath,255);
  182. int i;
  183. for(i=strlen(szPath);i>0;i--)
  184. {
  185. if(szPath[i]=='\')
  186. {
  187. szPath[i+1]=0;
  188. break;
  189. }
  190. }
  191. //
  192. strcpy(szPath+strlen(szPath),"addr.db");
  193. HWND h=GetDlgItem(m_hWnd,IDC_LIST);
  194. //得到记录条数
  195. int count=ListView_GetItemCount(h);
  196. char sz[64];
  197. sprintf(sz,"%d",count);
  198. //逐条写入文件
  199. WritePrivateProfileString("addr","count",sz,szPath);
  200. for(i=0;i<count;i++)
  201. {
  202. char s[10];
  203. sprintf(s,"%da",i);
  204. ListView_GetItemText(h,i,0,sz,64);
  205. WritePrivateProfileString("addr",s,sz,szPath);
  206. sprintf(s,"%db",i);
  207. ListView_GetItemText(h,i,1,sz,64);
  208. WritePrivateProfileString("addr",s,sz,szPath);
  209. }
  210. }
  211. /*------------------------------------------------------------------------------*/
  212. CModifyDlg::CModifyDlg()
  213. {
  214. m_szName[0]=0;
  215. m_szIP[0]=0;
  216. }
  217. /*------------------------------------------------------------------------------*/
  218. CModifyDlg::~CModifyDlg()
  219. {
  220. }
  221. /*------------------------------------------------------------------------------*/
  222. LRESULT CModifyDlg::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  223. {
  224. switch(uMsg)
  225. {
  226. case WM_INITDIALOG:
  227. CGraphDlg::OnInitDialog();
  228. SetWindowText(m_hWnd,"请输入记录");
  229. SetDlgItemText(m_hWnd,IDC_NAME,m_szName);
  230. SetDlgItemText(m_hWnd,IDC_IP,m_szIP);
  231. return FALSE;
  232. case WM_COMMAND:
  233. CGraphDlg::OnCommand(wParam,lParam);
  234. switch(LOWORD(wParam))
  235. {
  236. case IDC_OK:
  237. GetDlgItemText(m_hWnd,IDC_NAME,m_szName,64);
  238. if(strlen(m_szName)==0)
  239. break;
  240. GetDlgItemText(m_hWnd,IDC_IP,m_szIP,64);
  241. if(strlen(m_szIP)==0)
  242. break;
  243. EndDialog(m_hWnd,TRUE);
  244. break;
  245. case IDC_CANCEL:
  246. EndDialog(m_hWnd,FALSE);
  247. break;
  248. }
  249. break;
  250. case WM_CLOSE:
  251. EndDialog(m_hWnd,FALSE);
  252. break;
  253. default:
  254. return CGraphDlg::WndProc(uMsg,wParam,lParam);
  255. }
  256. return TRUE;
  257. }