ex126ClientDlg.cpp
上传用户:qdhmjx
上传日期:2022-07-11
资源大小:2226k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // ex126ClientDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ex126Client.h"
  5. #include "ex126ClientDlg.h"
  6. #include "LogonDlg.h"
  7. #include "ChatSocket.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19. // Dialog Data
  20. //{{AFX_DATA(CAboutDlg)
  21. enum { IDD = IDD_ABOUTBOX };
  22. //}}AFX_DATA
  23. // ClassWizard generated virtual function overrides
  24. //{{AFX_VIRTUAL(CAboutDlg)
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  27. //}}AFX_VIRTUAL
  28. // Implementation
  29. protected:
  30. //{{AFX_MSG(CAboutDlg)
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CAboutDlg)
  37. //}}AFX_DATA_INIT
  38. }
  39. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CAboutDlg)
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  46. //{{AFX_MSG_MAP(CAboutDlg)
  47. // No message handlers
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CEx126ClientDlg dialog
  52. CEx126ClientDlg::CEx126ClientDlg(CWnd* pParent /*=NULL*/)
  53. : CDialog(CEx126ClientDlg::IDD, pParent)
  54. {
  55. //{{AFX_DATA_INIT(CEx126ClientDlg)
  56. m_strMsg = _T("");
  57. //}}AFX_DATA_INIT
  58. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  59. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  60. }
  61. void CEx126ClientDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CEx126ClientDlg)
  65. DDX_Control(pDX, IDC_LISTBOX, m_ctrlMsg);
  66. DDX_Text(pDX, IDC_EDITMSG, m_strMsg);
  67. //}}AFX_DATA_MAP
  68. }
  69. BEGIN_MESSAGE_MAP(CEx126ClientDlg, CDialog)
  70. //{{AFX_MSG_MAP(CEx126ClientDlg)
  71. ON_WM_SYSCOMMAND()
  72. ON_WM_PAINT()
  73. ON_WM_QUERYDRAGICON()
  74. ON_BN_CLICKED(IDC_SENDMSG, OnSendmsg)
  75. //}}AFX_MSG_MAP
  76. END_MESSAGE_MAP()
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CEx126ClientDlg message handlers
  79. BOOL CEx126ClientDlg::OnInitDialog()
  80. {
  81. CDialog::OnInitDialog();
  82. // Add "About..." menu item to system menu.
  83. // IDM_ABOUTBOX must be in the system command range.
  84. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  85. ASSERT(IDM_ABOUTBOX < 0xF000);
  86. CMenu* pSysMenu = GetSystemMenu(FALSE);
  87. if (pSysMenu != NULL)
  88. {
  89. CString strAboutMenu;
  90. strAboutMenu.LoadString(IDS_ABOUTBOX);
  91. if (!strAboutMenu.IsEmpty())
  92. {
  93. pSysMenu->AppendMenu(MF_SEPARATOR);
  94. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  95. }
  96. }
  97. // Set the icon for this dialog.  The framework does this automatically
  98. //  when the application's main window is not a dialog
  99. SetIcon(m_hIcon, TRUE); // Set big icon
  100. SetIcon(m_hIcon, FALSE); // Set small icon
  101. // TODO: Add extra initialization here
  102. CLogonDlg dlg;
  103. if(dlg.DoModal()==IDOK)
  104. {
  105. //创建一个新的socket
  106. m_pSocket=new CChatSocket(this);
  107. if(!m_pSocket->Create())
  108. {
  109. delete m_pSocket;
  110. m_pSocket=NULL;
  111. AfxMessageBox("产生socket失败!");
  112. return FALSE;
  113. }
  114. //连接服务器
  115. while(!m_pSocket->Connect(dlg.m_strServer,dlg.m_nPort+700))
  116. {
  117. if(AfxMessageBox("连接服务器失败!n重试?",MB_YESNO)==IDNO)
  118. {
  119. delete m_pSocket;
  120. m_pSocket=NULL;
  121. return FALSE;
  122. }
  123. }
  124. m_strClientName=dlg.m_strName;
  125. }
  126. else
  127. return FALSE;
  128. //发送登录消息给服务器
  129. SendMsg("进入聊天室");
  130. return TRUE;  // return TRUE  unless you set the focus to a control
  131. }
  132. void CEx126ClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
  133. {
  134. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  135. {
  136. CAboutDlg dlgAbout;
  137. dlgAbout.DoModal();
  138. }
  139. else
  140. {
  141. CDialog::OnSysCommand(nID, lParam);
  142. }
  143. }
  144. // If you add a minimize button to your dialog, you will need the code below
  145. //  to draw the icon.  For MFC applications using the document/view model,
  146. //  this is automatically done for you by the framework.
  147. void CEx126ClientDlg::OnPaint() 
  148. {
  149. if (IsIconic())
  150. {
  151. CPaintDC dc(this); // device context for painting
  152. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  153. // Center icon in client rectangle
  154. int cxIcon = GetSystemMetrics(SM_CXICON);
  155. int cyIcon = GetSystemMetrics(SM_CYICON);
  156. CRect rect;
  157. GetClientRect(&rect);
  158. int x = (rect.Width() - cxIcon + 1) / 2;
  159. int y = (rect.Height() - cyIcon + 1) / 2;
  160. // Draw the icon
  161. dc.DrawIcon(x, y, m_hIcon);
  162. }
  163. else
  164. {
  165. CDialog::OnPaint();
  166. }
  167. }
  168. // The system calls this to obtain the cursor to display while the user drags
  169. //  the minimized window.
  170. HCURSOR CEx126ClientDlg::OnQueryDragIcon()
  171. {
  172. return (HCURSOR) m_hIcon;
  173. }
  174. void CEx126ClientDlg::ProcessPendingRead()
  175. {
  176. //定义缓冲区
  177. char buffer[BUFFER_SIZE];
  178. //接收数据
  179. int nReceived=m_pSocket->Receive(buffer,BUFFER_SIZE,0);
  180. buffer[nReceived]=0;
  181. //将数据在列表框中显示
  182. CString str;
  183. str.Format("%s",buffer);
  184. m_ctrlMsg.AddString(str);
  185. }
  186. void CEx126ClientDlg::SendMsg(CString strMsg)
  187. {
  188. CString str;
  189. str.Format("%s:%s",m_strClientName,strMsg);
  190. m_pSocket->Send(str.GetBuffer(0),str.GetLength(),0);
  191. }
  192. void CEx126ClientDlg::OnSendmsg() 
  193. {
  194. UpdateData(TRUE);
  195. SendMsg(m_strMsg);
  196. }