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

IP电话/视频会议

开发平台:

Visual C++

  1. //NetTalk
  2. /*------------------------------------------------------------------------------*
  3.  =============================
  4.    模块名称: MsgTalk.cpp
  5.  =============================
  6.  
  7.  [版权]
  8.  
  9.    2000-2002  115软件工厂  版权所有
  10.                                               
  11. *------------------------------------------------------------------------------*/
  12. #include "wndx.h"
  13. #include "WndX.h"
  14. #include "MsgTalk.h"
  15. #include "NewMsgBox.h"
  16. #include <Stdio.h>
  17. /*------------------------------------------------------------------------------*/
  18. CMsgTalkDlg::CMsgTalkDlg()
  19. {
  20. }
  21. /*------------------------------------------------------------------------------*/
  22. CMsgTalkDlg::~CMsgTalkDlg()
  23. {
  24. }
  25. /*------------------------------------------------------------------------------*/
  26. LRESULT CMsgTalkDlg::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  27. {
  28. switch(uMsg)
  29. {
  30. case WM_INITDIALOG:
  31. return OnInitDialog();
  32. break;
  33. case WM_COMMAND:
  34. return OnCommand(wParam,lParam);
  35. case WM_CLOSE:
  36. ShowWindow(m_hWnd,SW_HIDE);
  37. break;
  38. case WM_SHOWWINDOW:
  39. if(wParam==TRUE)
  40. {
  41. LoadList();
  42. }
  43. break;
  44. default:
  45. return CGraphDlg::WndProc(uMsg,wParam,lParam);
  46. }
  47. return TRUE;
  48. }
  49. /*------------------------------------------------------------------------------*/
  50. BOOL CMsgTalkDlg::OnCommand(WPARAM wParam, LPARAM lParam)
  51. {
  52. CGraphDlg::OnCommand(wParam,lParam);
  53. switch(LOWORD(wParam))
  54. {
  55. case IDC_SEND:
  56. {
  57. char sz[500];
  58. //check if the combo box is empty
  59. if(GetWindowTextLength(GetDlgItem(m_hWnd,IDC_OBJ))>0)
  60. {
  61. char s[64];
  62. GetDlgItemText(m_hWnd,IDC_OBJ,s,64);
  63. //if the IP  not exist in the combo box,insert it
  64. if(SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_FINDSTRING,0,(LPARAM)s)==CB_ERR)
  65. {
  66. SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_INSERTSTRING,0,(LPARAM)s);
  67. }
  68. //get the Msg content
  69. if(GetDlgItemText(m_hWnd,IDC_T,sz,256)>0)
  70. {
  71. SetDlgItemText(m_hWnd,IDC_T,"");
  72. SendMessage(GetParent(m_hWnd),WM_COMMAND,IDC_SEND,(LPARAM)sz);
  73. }
  74. }
  75. else
  76. {
  77. CMsgBox::MessageBox(m_hWnd,IDS_MSG_NOOBJ,IDS_MSG,MB_OK,IDI_INFO);
  78. SetFocus(GetDlgItem(m_hWnd,IDC_OBJ));
  79. }
  80. }
  81. break;
  82. case IDC_CLEAR:
  83. {
  84. SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_RESETCONTENT,0,0);
  85. SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_SETHORIZONTALEXTENT,0,0);
  86. }
  87. break;
  88. case IDC_MSGLIST:
  89. {
  90. if(HIWORD(wParam)==LBN_DBLCLK)
  91. {
  92. int i=SendMessage((HWND)lParam,LB_GETCURSEL,0,0);
  93. if(i!=LB_ERR)
  94. {
  95. char sz[256];
  96. SendMessage((HWND)lParam,LB_GETTEXT,i,(LPARAM)sz);
  97. SetDlgItemText(m_hWnd,IDC_T,sz);
  98. SetFocus(GetDlgItem(m_hWnd,IDC_T));
  99. }
  100. }
  101. }break;
  102. case IDC_SAVE:
  103. {
  104. if(SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETCOUNT,0,0)>0)
  105. {
  106. OPENFILENAME ofn;
  107. ZeroMemory(&ofn,sizeof(ofn));
  108. ofn.lStructSize=sizeof(ofn);
  109. ofn.hInstance=GetModuleHandle(0);
  110. ofn.hwndOwner=m_hWnd;
  111. char szFilter[100];
  112. char* p=szFilter;
  113. strcpy(p,"文本文件(*.txt)");
  114. p+=strlen(p)+1;
  115. strcpy(p,"*.txt");
  116. p+=strlen(p)+1;
  117. strcpy(p,"所有文件(*.*)");
  118. p+=strlen(p)+1;
  119. strcpy(p,"*.*");
  120. p+=strlen(p)+1;
  121. *p=0;
  122. ofn.lpstrFilter=szFilter;
  123. char szFile[256]="";
  124. ofn.lpstrFileTitle=szFile;
  125. ofn.nMaxFileTitle=256;
  126. ofn.lpstrTitle="保存对话记录";
  127. ofn.Flags=OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT;
  128. char ext[]="txt";
  129. ofn.lpstrDefExt=ext;
  130. if(GetSaveFileName(&ofn))
  131. {
  132. FILE* pf=fopen(szFile,"a+");
  133. if(pf)
  134. {
  135. char content[500];
  136. //保存记录
  137. for(int i=0;i<SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETCOUNT,0,0);i++)
  138. {
  139. SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETTEXT,i,(LPARAM)content);
  140. fprintf(pf,"%sn",content);
  141. }
  142. fclose(pf);
  143. }
  144. else
  145. {
  146. CMsgBox::MessageBox(m_hWnd,IDS_ERR_SAVEREC,IDS_ERR,MB_OK,IDI_INFO);
  147. }
  148. }
  149. }
  150. }
  151. break;
  152. }
  153. return TRUE;
  154. }
  155. /*------------------------------------------------------------------------------*/
  156. BOOL CMsgTalkDlg::OnInitDialog()
  157. {
  158. CGraphDlg::OnInitDialog();
  159. m_MyEdit.Associate(m_hWnd,IDC_T);
  160. SetWindowText(m_hWnd,"文本对话");
  161. SendDlgItemMessage(m_hWnd,IDC_T,EM_LIMITTEXT,255,0);
  162. LoadList();
  163. return TRUE;
  164. }
  165. /*------------------------------------------------------------------------------*/
  166. BOOL CMsgTalkDlg::Create(HWND hParent)
  167. {
  168. return CGraphDlg::Create(LPCTSTR(IDD_TXT_DLG),hParent);
  169. }
  170. /*------------------------------------------------------------------------------*/
  171. void CMsgTalkDlg::AddMsg(char *pszMsg)
  172. {
  173. int i=SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_ADDSTRING,0,(LPARAM)pszMsg);
  174. //ListBox在横向不会自动Scroll,所以必须处理一下:-)
  175. CRectX rc;
  176. HDC hdc=GetDC(m_hWnd);
  177. HFONT hof=(HFONT)SelectObject(hdc,(HFONT)SendMessage(m_hWnd,WM_GETFONT,0,0));
  178. DrawText(hdc,pszMsg,strlen(pszMsg), &rc, DT_CALCRECT);
  179. SelectObject(hdc,hof);
  180. ReleaseDC(m_hWnd,hdc);
  181. if(rc.Width()>SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETHORIZONTALEXTENT,0,0))
  182. {
  183. SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_SETHORIZONTALEXTENT,rc.Width()+5,0);
  184. }
  185. //不知道如何控制纵向滚动条,索性选中最后一个item
  186. i=SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_GETCOUNT,0,0);
  187. SendDlgItemMessage(m_hWnd,IDC_MSGLIST,LB_SETCURSEL,i-1,0);
  188. }
  189. void CMsgTalkDlg::LoadList()
  190. {
  191. SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_RESETCONTENT,0,0);
  192. //载入通讯录
  193. char szPath[256];
  194. GetModuleFileName(0,szPath,255);
  195. int i;
  196. for(i=strlen(szPath);i>0;i--)
  197. {
  198. if(szPath[i]=='\')
  199. {
  200. szPath[i+1]=0;
  201. break;
  202. }
  203. }
  204. strcpy(szPath+strlen(szPath),"addr.db");
  205. int count=GetPrivateProfileInt("addr","count",0,szPath);
  206. char sz[64];
  207. for(i=0;i<count;i++)
  208. {
  209. char s[10];
  210. sprintf(s,"%db",i);
  211. GetPrivateProfileString("addr",s,"",sz,64,szPath);
  212. SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_ADDSTRING,0,(LPARAM)sz);
  213. }
  214. //载入最近呼叫
  215. count=GetPrivateProfileInt("recent","count",0,"NetTalk.ini");
  216. if(count>=20)
  217. {
  218. count=20;
  219. }
  220. char szStr[256];
  221. for(i=0;i<count;i++)
  222. {
  223. char szI[4];
  224. sprintf(szI,"%d",i);
  225. if(GetPrivateProfileString("recent",szI,"",szStr,256,"NetTalk.ini"))
  226. {
  227. SendMessage(GetDlgItem(m_hWnd,IDC_OBJ),CB_INSERTSTRING,0,(LPARAM)szStr);
  228. }
  229. }
  230. SendDlgItemMessage(m_hWnd,IDC_OBJ,CB_SETCURSEL,0,0);
  231. }