?? 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 + -