📄 clientdlg.cpp
字号:
// ClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ChatClient.h"
#include "ClientDlg.h"
#include "ChatSocket.h"
#include "SetupDlg.h"
#include "msg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
m_strMsg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bAutoChat=FALSE;
m_pSocket=NULL;
m_pFile=NULL;
m_pArchiveIn=NULL;
m_pArchiveOut=NULL;
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Control(pDX, IDC_LIST_MSG, m_ListMsg);
DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDOK, OnSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//CSetupDlg类的对象
CSetupDlg dlg;
while (TRUE)
{
//调用对话框
//接受用户的输入
if (dlg.DoModal()!=IDOK)
{
//退出应用程序
CDialog::OnOK();
return TRUE;
}
//向服务器发送连接请求
//使用从设置窗口获得的
//客户机名称、服务器地址和通信端口号
if (ConnectSocket(dlg.m_strHandle,dlg.m_strServer,dlg.m_nPort))
return TRUE;
if (AfxMessageBox("请尝试别的地址!",MB_YESNO)==IDNO)
{
//退出应用程序
CDialog::OnOK();
return TRUE;
}
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//在CClientDlg类终止运行时进行的后续处理
void CClientDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
m_bAutoChat=FALSE;
if ((m_pSocket!=NULL)&&(m_pFile!=NULL)&&(m_pArchiveOut!=NULL))
{
//发送客户离开聊天室的消息
CMsg msg;
CString strTemp;
strTemp=":离开聊天室!";
msg.m_bClose=TRUE;
msg.m_strText=m_strHandle+strTemp;
msg.Serialize(*m_pArchiveOut);
m_pArchiveOut->Flush();
}
//删除CArchive对象
delete m_pArchiveOut;
m_pArchiveOut=NULL;
//删除CArchive对象
delete m_pArchiveIn;
m_pArchiveIn=NULL;
delete m_pFile;
//删除CSOcketFile对象
m_pFile=NULL;
if (m_pSocket!=NULL)
{
BYTE Buffer[50];
m_pSocket->ShutDown();
while (m_pSocket->Receive(Buffer,50)>0);
}
delete m_pSocket;
m_pSocket=NULL;
}
void CClientDlg::OnSend()
{
// TODO: Add extra validation here
//获得当前用户输入的信息
UpdateData(TRUE);
//信息是否为空字符串
if (!m_strMsg.IsEmpty())
{
//非空字符串
//发送信息
this->SendMsg(m_strMsg);
//信息设为空字符串
m_strMsg=_T("");
//更新当前显示
UpdateData(FALSE);
}
}
//建立连接
BOOL CClientDlg::ConnectSocket(LPCTSTR lpszHandle,LPCTSTR lpszAddress,UINT nPort)
{
//存储用户名
m_strHandle=lpszHandle;
//创建套接字对象
m_pSocket=new CChatSocket(this);
if (!m_pSocket->Create())
{
//错误处理
delete m_pSocket;
m_pSocket=NULL;
AfxMessageBox("套接字创建错误!");
return FALSE;
}
//发送连接请求
while (!m_pSocket->Connect(lpszAddress,nPort))
{
//无法连接
if (AfxMessageBox("无法连接服务器!\n重试?",MB_YESNO)==IDNO)
{
//错误处理
delete m_pSocket;
m_pSocket=NULL;
return FALSE;
}
}
//创建CSocketFile类对象
m_pFile=new CSocketFile(m_pSocket);
//创建CArchive类对象
m_pArchiveIn=new CArchive(m_pFile,CArchive::load);
m_pArchiveOut=new CArchive(m_pFile,CArchive::store);
//发送消息
//表明该客户进入聊天室
CString strTemp;
strTemp="进入聊天室!";
SendMsg(strTemp);
return TRUE;
}
//读取信息
void CClientDlg::OnReceive()
{
do
{
//接收消息
ReceiveMsg();
if (m_pSocket==NULL)
return;
}
while (!m_pArchiveIn->IsBufferEmpty());
}
//发送消息
void CClientDlg::SendMsg(CString& strText)
{
if (m_pArchiveOut!=NULL)
{
CMsg msg;
//消息预处理
//表明消息的来源
msg.m_strText=m_strHandle+_T(":")+strText;
TRY
{
//发送消息
msg.Serialize(*m_pArchiveOut);
//将CArchive对象中数据强制存储到CSocketFile对象中
m_pArchiveOut->Flush();
}
CATCH(CFileException,e)
{
//错误处理
m_bAutoChat=FALSE;
m_pArchiveOut->Abort();
delete m_pArchiveOut;
m_pArchiveOut=NULL;
CString strTemp;
strTemp="服务器重置连接!";
DisplayMsg(strTemp);
}
END_CATCH
}
}
//接收消息
void CClientDlg::ReceiveMsg()
{
CMsg msg;
TRY
{
//接收消息
msg.Serialize(*m_pArchiveIn);
while (!msg.m_msgList.IsEmpty())
{
CString strTemp;
//获取消息内容
strTemp=msg.m_msgList.RemoveHead();
//显示消息
DisplayMsg(strTemp);
}
}
CATCH(CFileException,e)
{
//错误处理
m_bAutoChat=FALSE;
msg.m_bClose=TRUE;
m_pArchiveOut->Abort();
CString strTemp;
strTemp="服务器重置连接!";
DisplayMsg(strTemp);
strTemp="连接关闭!";
DisplayMsg(strTemp);
}
END_CATCH
if (msg.m_bClose)
{
//处理服务器关闭的消息
delete m_pArchiveIn;
m_pArchiveIn=NULL;
delete m_pArchiveOut;
m_pArchiveOut=NULL;
delete m_pFile;
m_pFile=NULL;
delete m_pSocket;
m_pSocket=NULL;
}
}
//显示消息
void CClientDlg::DisplayMsg(LPCTSTR lpszText)
{
//显示消息
m_ListMsg.AddString(lpszText);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -