Example2_ChatRoomDlg.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:8k
源码类别:

游戏

开发平台:

Visual C++

  1. // Example2_ChatRoomDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Example2_ChatRoom.h"
  5. #include "Example2_ChatRoomDlg.h"
  6. #include "afxsock.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CExample2_ChatRoomDlg dialog
  51. CExample2_ChatRoomDlg::CExample2_ChatRoomDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CExample2_ChatRoomDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CExample2_ChatRoomDlg)
  55. m_sInputString = _T("");
  56. m_sShowString = _T("");
  57. m_uPort = 4000;
  58. //}}AFX_DATA_INIT
  59. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  60. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61. }
  62. void CExample2_ChatRoomDlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CExample2_ChatRoomDlg)
  66. DDX_Text(pDX, IDC_INPUTTEXT, m_sInputString);
  67. DDV_MaxChars(pDX, m_sInputString, 1024);
  68. DDX_Text(pDX, IDC_SHOWTEXT, m_sShowString);
  69. DDX_Text(pDX, IDC_PORT, m_uPort);
  70. DDV_MinMaxUInt(pDX, m_uPort, 1024, 100000);
  71. //}}AFX_DATA_MAP
  72. }
  73. BEGIN_MESSAGE_MAP(CExample2_ChatRoomDlg, CDialog)
  74. //{{AFX_MSG_MAP(CExample2_ChatRoomDlg)
  75. ON_WM_SYSCOMMAND()
  76. ON_WM_PAINT()
  77. ON_WM_QUERYDRAGICON()
  78. ON_EN_UPDATE(IDC_INPUTTEXT,OnInputText)
  79. ON_BN_CLICKED(IDC_CONNECTSERVER, OnConnectserver)
  80. ON_BN_CLICKED(IDC_SETSERVER, OnSetserver)
  81. //}}AFX_MSG_MAP
  82. END_MESSAGE_MAP()
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CExample2_ChatRoomDlg message handlers
  85. BOOL CExample2_ChatRoomDlg::OnInitDialog()
  86. {
  87. CDialog::OnInitDialog();
  88. // Add "About..." menu item to system menu.
  89. // IDM_ABOUTBOX must be in the system command range.
  90. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  91. ASSERT(IDM_ABOUTBOX < 0xF000);
  92. CMenu* pSysMenu = GetSystemMenu(FALSE);
  93. if (pSysMenu != NULL)
  94. {
  95. CString strAboutMenu;
  96. strAboutMenu.LoadString(IDS_ABOUTBOX);
  97. if (!strAboutMenu.IsEmpty())
  98. {
  99. pSysMenu->AppendMenu(MF_SEPARATOR);
  100. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  101. }
  102. }
  103. // Set the icon for this dialog.  The framework does this automatically
  104. //  when the application's main window is not a dialog
  105. SetIcon(m_hIcon, TRUE); // Set big icon
  106. SetIcon(m_hIcon, FALSE); // Set small icon
  107. // TODO: Add extra initialization here
  108. if(AfxSocketInit(NULL)==0)
  109. {
  110. AfxMessageBox("CSocket Inital Error");
  111. }
  112. BYTE f0,f1,f2,f3;
  113. CString name;
  114. CClientSocket::GetLocalHostName(name);
  115. CClientSocket::GetIpAddress(name,f0,f1,f2,f3);
  116. ((CIPAddressCtrl *)(GetDlgItem(IDC_IPADDRESS)))->SetAddress(f0,f1,f2,f3);
  117. m_bInit=false;
  118. m_bClient=false;
  119. return TRUE;  // return TRUE  unless you set the focus to a control
  120. }
  121. void CExample2_ChatRoomDlg::OnSysCommand(UINT nID, LPARAM lParam)
  122. {
  123. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  124. {
  125. CAboutDlg dlgAbout;
  126. dlgAbout.DoModal();
  127. }
  128. else
  129. {
  130. CDialog::OnSysCommand(nID, lParam);
  131. }
  132. }
  133. // If you add a minimize button to your dialog, you will need the code below
  134. //  to draw the icon.  For MFC applications using the document/view model,
  135. //  this is automatically done for you by the framework.
  136. void CExample2_ChatRoomDlg::OnPaint() 
  137. {
  138. if (IsIconic())
  139. {
  140. CPaintDC dc(this); // device context for painting
  141. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  142. // Center icon in client rectangle
  143. int cxIcon = GetSystemMetrics(SM_CXICON);
  144. int cyIcon = GetSystemMetrics(SM_CYICON);
  145. CRect rect;
  146. GetClientRect(&rect);
  147. int x = (rect.Width() - cxIcon + 1) / 2;
  148. int y = (rect.Height() - cyIcon + 1) / 2;
  149. // Draw the icon
  150. dc.DrawIcon(x, y, m_hIcon);
  151. }
  152. else
  153. {
  154. CDialog::OnPaint();
  155. }
  156. }
  157. // The system calls this to obtain the cursor to display while the user drags
  158. //  the minimized window.
  159. HCURSOR CExample2_ChatRoomDlg::OnQueryDragIcon()
  160. {
  161. return (HCURSOR) m_hIcon;
  162. }
  163. void CExample2_ChatRoomDlg::OnInputText()
  164. {
  165. if(!m_bInit)
  166. {
  167. AfxMessageBox("Not Connection to The Server");
  168. return;
  169. }
  170. CString in;
  171. CMessg msg;
  172. GetDlgItemText(IDC_INPUTTEXT,in);
  173. if(in.GetLength()<1)
  174. {
  175. return;
  176. }
  177. if(in.GetAt(in.GetLength()-1)=='n')
  178. {
  179. in.TrimRight(" ");
  180. SetDlgItemText(IDC_INPUTTEXT,"");
  181. if(in.GetLength()>2)
  182. {
  183. m_sMsgList+=in;
  184. SetDlgItemText(IDC_SHOWTEXT,m_sMsgList);
  185. int m_iLineCurrentPos=((CEdit *)(GetDlgItem(IDC_SHOWTEXT)))->GetLineCount();
  186. ((CEdit *)(GetDlgItem(IDC_SHOWTEXT)))->LineScroll(m_iLineCurrentPos);
  187. msg.m_strText=in;
  188. if(!m_bClient)
  189. {
  190. POSITION pos;
  191. for(pos=m_connectionList.GetHeadPosition();pos!=NULL;)
  192. {
  193. CClientSocket * t= (CClientSocket *)m_connectionList.GetNext(pos);
  194. t->SendMessage(&msg);
  195. }
  196. }
  197. else
  198. {
  199. m_clientsocket.SendMessage(&msg);
  200. }
  201. }
  202. }
  203. }
  204. void CExample2_ChatRoomDlg::ProcessPendingAccept()
  205. {
  206. CClientSocket* pSocket = new CClientSocket();
  207. if (m_pListenSocket.Accept(*pSocket))
  208. {
  209. CMessg msg;
  210. msg.m_strText="一个游客进入聊天室了";
  211. m_sShowString+="一个游客进入聊天室了n";
  212. POSITION pos;
  213. for(pos=m_connectionList.GetHeadPosition();pos!=NULL;)
  214. {
  215. CClientSocket * t= (CClientSocket *)m_connectionList.GetNext(pos);
  216. t->SendMessage(&msg);
  217. }
  218. pSocket->Init(this);
  219. m_connectionList.AddTail(pSocket);
  220. }
  221. else
  222. delete pSocket;
  223. }
  224. void CExample2_ChatRoomDlg::ClearContent()
  225. {
  226. if(m_bClient)
  227. {
  228. m_clientsocket.Close();
  229. }
  230. else
  231. {
  232. m_pListenSocket.Close();
  233. m_connectionList.RemoveAll();
  234. }
  235. m_bInit=false;
  236. m_sMsgList="";
  237. SetDlgItemText(IDC_INPUTTEXT,"");
  238. SetDlgItemText(IDC_SHOWTEXT,"");
  239. }
  240. void CExample2_ChatRoomDlg::OnClearconnection() 
  241. {
  242. ClearContent();
  243. }
  244. void CExample2_ChatRoomDlg::CloseSessionSocket()
  245. {
  246. if(!m_bClient)
  247. {
  248. for(POSITION pos=m_connectionList.GetHeadPosition();pos!=NULL;)
  249. {
  250. POSITION t_pos=pos;
  251. CClientSocket * t = (CClientSocket*)m_connectionList.GetNext(pos);
  252. if(t->m_bClose==true)
  253. {
  254. m_connectionList.RemoveAt(t_pos);
  255. }
  256. }
  257. }
  258. }
  259. void CExample2_ChatRoomDlg::OnConnectserver() 
  260. {
  261. // TODO: Add your control notification handler code here
  262. if(!m_bInit)
  263. {
  264. BYTE f0,f1,f2,f3;
  265. CString name;
  266. ((CIPAddressCtrl *)(GetDlgItem(IDC_IPADDRESS)))->GetAddress(f0,f1,f2,f3);
  267. CString ip;
  268. ip.Format("%d.%d.%d.%d",f0,f1,f2,f3);
  269. m_bClient=true;
  270. m_clientsocket.Create();
  271. if(m_clientsocket.Connect(ip,GetDlgItemInt(IDC_PORT)))
  272. {
  273. m_clientsocket.Init(this);
  274. SetDlgItemText(IDC_SHOWTEXT,"Client Connection Succeed");
  275. m_bInit=true;
  276. }
  277. else
  278. {
  279. m_clientsocket.Close();
  280. AfxMessageBox("client connection failed");
  281. m_bInit=false;
  282. }
  283. }
  284. }
  285. void CExample2_ChatRoomDlg::OnSetserver() 
  286. {
  287. // TODO: Add your control notification handler code here
  288. if(!m_bInit)
  289. {
  290. m_bClient=false;
  291. m_bInit=true;
  292. if(m_pListenSocket.Init(GetDlgItemInt(IDC_PORT),this)==FALSE)
  293. {
  294. m_bInit=false;
  295. return;
  296. }
  297. }
  298. }