TalkDlg.cpp
上传用户:guangzhiyw
上传日期:2007-01-09
资源大小:495k
文件大小:6k
源码类别:

ICQ/即时通讯

开发平台:

Visual C++

  1. // TalkDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "client.h"
  5. #include "TalkDlg.h"
  6. #include "LookDlg.h"
  7. #include "mainfrm.h"
  8. #include "frienddetail.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CTalkDlg dialog
  16. CTalkDlg::CTalkDlg(CWnd* pParent /*=NULL*/)
  17. : CDialog(CTalkDlg::IDD, pParent)
  18. {
  19. //{{AFX_DATA_INIT(CTalkDlg)
  20. m_strEmail = _T("");
  21. m_strID = _T("");
  22. m_strName = _T("");
  23. //}}AFX_DATA_INIT
  24. nPhotoId=0;
  25. pInfo=NULL;
  26. pState=NULL;
  27. hIcon=AfxGetApp()->LoadIcon(IDR_MESSAGE1);
  28. bClosed=TRUE;
  29. }
  30. void CTalkDlg::DoDataExchange(CDataExchange* pDX)
  31. {
  32. CDialog::DoDataExchange(pDX);
  33. //{{AFX_DATA_MAP(CTalkDlg)
  34. DDX_Control(pDX, IDC_SHOW_DETAIL, m_headBmp);
  35. DDX_Control(pDX, IDC_TALK_RECORD, m_TalkRecord);
  36. DDX_Control(pDX, IDC_TALK, m_TalkEdit);
  37. DDX_Text(pDX, IDC_EMAIL, m_strEmail);
  38. DDX_Text(pDX, IDC_ID, m_strID);
  39. DDX_Text(pDX, IDC_NAME, m_strName);
  40. //}}AFX_DATA_MAP
  41. }
  42. BEGIN_MESSAGE_MAP(CTalkDlg, CDialog)
  43. //{{AFX_MSG_MAP(CTalkDlg)
  44. ON_BN_CLICKED(ID_SHOW_TALK, OnShowTalk)
  45. ON_BN_CLICKED(IDC_SHOW_DETAIL, OnShowDetail)
  46. ON_WM_CLOSE()
  47. //}}AFX_MSG_MAP
  48. ON_MESSAGE(WM_SEND_MSG,OnSendMsg)
  49. ON_MESSAGE(WM_REFRESH_DATA,OnRefreshData)
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CTalkDlg message handlers
  53. BOOL CTalkDlg::OnInitDialog() 
  54. {
  55. CDialog::OnInitDialog();
  56. SetIcon(hIcon,TRUE);
  57. SetIcon(hIcon,FALSE);
  58. CMenu* pMenu=GetSystemMenu(FALSE);
  59. pMenu->EnableMenuItem(SC_RESTORE,MF_GRAYED|MF_DISABLED);
  60. pMenu->EnableMenuItem(SC_SIZE,MF_GRAYED|MF_DISABLED);
  61. pMenu->EnableMenuItem(SC_MAXIMIZE,MF_GRAYED|MF_DISABLED);
  62. m_TalkEdit.LimitText(LimitMaxMsgLength);
  63. CRect r1,r2;
  64. GetWindowRect(&r1);
  65. m_TalkRecord.GetWindowRect(&r2);
  66. r1.SetRect(r1.left,r1.top,r1.right,r2.top);
  67. MoveWindow(r1);
  68. return TRUE;
  69. }
  70. void CTalkDlg::OnShowTalk() 
  71. {
  72. static BOOL bShowRecord=TRUE;
  73. CRect r1,r2;
  74. GetWindowRect(&r1);
  75. m_TalkRecord.GetWindowRect(&r2);
  76. if(bShowRecord)
  77. r1.SetRect(r1.left,r1.top,r1.right,r2.bottom+4);
  78. else
  79. r1.SetRect(r1.left,r1.top,r1.right,r2.top);
  80. bShowRecord=!bShowRecord;
  81. MoveWindow(r1);
  82. }
  83. LRESULT CTalkDlg::OnSendMsg(WPARAM wParam,LPARAM lParam)
  84. {
  85. pInfo=(UserInfo*)wParam;
  86. pState=(FriendState*)lParam;
  87. if(bClosed)
  88. {
  89. m_TalkEdit.SetWindowText("");
  90. m_strID.Format("%d",pInfo->Id);
  91. nPhotoId=0;
  92. if(pInfo->HaveDetail)
  93. {
  94. m_strEmail=pInfo->Email;
  95. m_strName=pInfo->Name;
  96. if(pInfo->PhotoId>=0&&pInfo->PhotoId<UINT(GetApp()->m_nNumberHeadBmp))
  97. nPhotoId=pInfo->PhotoId;
  98. }
  99. else
  100. {
  101. m_strEmail="";
  102. m_strName="";
  103. }
  104. m_headBmp.SetBitmap(GetApp()->m_paBitmap[2*nPhotoId]);
  105. UpdateData(FALSE);
  106. bClosed=FALSE;
  107. }
  108. ShowWindow(SW_NORMAL);
  109. m_TalkEdit.SetFocus();
  110. return 0;
  111. }
  112. void CTalkDlg::OnShowDetail() 
  113. {
  114. if(pState==NULL)return;
  115. if(pState->pFInfo==NULL)
  116. {
  117. pState->pFInfo=new CFriendDetailDlg;
  118. pState->pFInfo->Create(CFriendDetailDlg::IDD,GetDesktopWindow());
  119. }
  120. pState->pFInfo->SendMessage(WM_SHOW_FRIEND_DETAIL,(WPARAM)pInfo,(LPARAM)pState);
  121. }
  122. void CTalkDlg::OnOK() 
  123. {
  124. if(m_TalkEdit.GetWindowTextLength()==0)
  125. {
  126. CString str;
  127. str.LoadString(IDS_CANNOT_SEND_NULL);
  128. MessageBox(str);
  129. return;
  130. }
  131. m_TalkEdit.SetReadOnly();
  132. GetDlgItem(IDOK)->EnableWindow(FALSE);
  133. SendMessage(WM_SYSCOMMAND,SC_MINIMIZE,0);
  134. CMsg1 msg;
  135. msg.index=SEND_MSG_TO_FRIEND;
  136. msg.MyId=GetApp()->m_uCurrentUserID;
  137. msg.FriendId=pInfo->Id;
  138. msg.Time=CTime::GetCurrentTime();
  139. if(pState->OnlineState)
  140. {
  141. msg.tarIP=pState->IP;
  142. msg.nPort=pState->Port;
  143. }
  144. else
  145. {
  146. msg.tarIP=GetApp()->m_uServerIP;
  147. msg.nPort=GetApp()->m_uServerPort;
  148. }
  149. m_TalkEdit.GetWindowText(msg.Msg);
  150. m_bCancel=FALSE;
  151. tryagain:
  152. if(GetApp()->m_Socket.SendData(&msg,m_bCancel))
  153. {
  154. m_TalkEdit.SetWindowText("");
  155. m_TalkEdit.SetReadOnly(FALSE);
  156. GetDlgItem(IDOK)->EnableWindow(TRUE);
  157. CString str;
  158. str.Format("(%s)%srn%srnrn",msg.Time.Format("%Y-%m-%d %H:%M:%S"),GetApp()->m_aUserInfo[0]->Name,msg.Msg);
  159. m_TalkRecord.SetSel(0,0);
  160. m_TalkRecord.ReplaceSel(str);
  161. if(pState->pRecv==NULL)
  162. {
  163. pState->pRecv=new CLookDlg;
  164. pState->pRecv->Create(CLookDlg::IDD,GetDesktopWindow());
  165. }
  166. pState->pRecv->m_TalkRecord.SetSel(0,0);
  167. pState->pRecv->m_TalkRecord.ReplaceSel(str);
  168. CDialog::OnOK();
  169. bClosed=TRUE;
  170. }
  171. else if(!m_bCancel)
  172. {
  173. if(msg.tarIP==GetApp()->m_uServerIP&&msg.nPort==GetApp()->m_uServerPort)
  174. {
  175. if(AfxMessageBox(IDS_ASK_RETRY,MB_YESNO)==IDYES)
  176. {
  177. goto tryagain;
  178. }
  179. else
  180. {
  181. m_TalkEdit.SetReadOnly(FALSE);
  182. GetDlgItem(IDOK)->EnableWindow();
  183. m_TalkEdit.SetFocus();
  184. }
  185. }
  186. else
  187. {
  188. msg.tarIP=GetApp()->m_uServerIP;
  189. msg.nPort=GetApp()->m_uServerPort;
  190. goto tryagain;
  191. }
  192. }
  193. else
  194. {
  195. m_TalkEdit.SetWindowText("");
  196. m_TalkEdit.SetReadOnly(FALSE);
  197. GetDlgItem(IDOK)->EnableWindow(TRUE);
  198. }
  199. }
  200. void CTalkDlg::OnClose() 
  201. {
  202. OnCancel();
  203. }
  204. void CTalkDlg::OnCancel() 
  205. {
  206. bClosed=TRUE;
  207. m_bCancel=TRUE;
  208. CDialog::OnCancel();
  209. }
  210. LRESULT CTalkDlg::OnRefreshData(WPARAM wParam,LPARAM lParam)
  211. {
  212. m_strEmail=pInfo->Email;
  213. m_strName=pInfo->Name;
  214. if(pInfo->PhotoId>=0&&pInfo->PhotoId<UINT(GetApp()->m_nNumberHeadBmp))
  215. m_headBmp.SetBitmap(GetApp()->m_paBitmap[2*pInfo->PhotoId]);
  216. UpdateData(FALSE);
  217. return 1;
  218. }
  219. BOOL CTalkDlg::PreTranslateMessage(MSG* pMsg) 
  220. {
  221. if(pMsg->message==WM_KEYDOWN)
  222. {
  223. if(pMsg->wParam==VK_RETURN&&GetKeyState(VK_CONTROL)&0x80)
  224. {
  225. OnOK();
  226. return 1;
  227. }
  228. }
  229. return CDialog::PreTranslateMessage(pMsg);
  230. }