www.pudn.com > chat-dialog.rar > Client1_2Dlg.cpp


// Client1_2Dlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Client1_2.h" 
#include "Client1_2Dlg.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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CClient1_2Dlg dialog 
 
CClient1_2Dlg::CClient1_2Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CClient1_2Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CClient1_2Dlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
//	m_menuCon.LoadMenu(IDR_MENU1); 
	m_bConnect=FALSE; 
	m_sockClient.m_pParentSock=this; 
	m_pStringList=new CStringList; 
//	strMessage.Empty(); 
//	nMessageLen=0; 
 
} 
 
void CClient1_2Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CClient1_2Dlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
	 
} 
 
BEGIN_MESSAGE_MAP(CClient1_2Dlg, CDialog) 
	//{{AFX_MSG_MAP(CClient1_2Dlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(ID_SENDMSG, OnSendmsg) 
	ON_COMMAND(ID_Con, OnCon) 
	ON_COMMAND(ID_QUIT, OnQuit) 
	ON_COMMAND(ID_SAVEMSG, OnSavemsg) 
	ON_COMMAND(ID_ABOUT, OnAbout) 
	ON_BN_CLICKED(IDC_ALLARM, OnAllarm) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CClient1_2Dlg message handlers 
 
BOOL CClient1_2Dlg::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); 
		} 
	} 
	CMenu* pMenu=new CMenu; 
	pMenu->LoadMenu(IDR_MENU1); 
	SetMenu(pMenu); 
	// 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 
//	SetMenu(&m_menuCon); 
	// TODO: Add extra initialization here 
 
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_SEND); 
	pEdit->FmtLines(FALSE); 
	 
	m_sockClient.Create(); 
 
	CBitmap bitmap; 
	bitmap.LoadBitmap(IDB_CLIENTQQ); 
	m_imageList.Create(40,40,ILC_COLOR,0,5); 
	m_imageList.Add(&bitmap,RGB(0,0,0)); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CClient1_2Dlg::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 CClient1_2Dlg::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 CClient1_2Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
 
void CClient1_2Dlg::DisDlgRev(char* chRev,int nRevLen) 
{ 
	chRev[nRevLen]=NULL; 
	CString strRev(chRev); 
//	GetDlgItemText(IDC_EDIT_REV,m_strDisplay); 
	strMessage+=strRev; 
	nMessageLen+=nRevLen; 
	CSocketMsg* sockMsg=new CSocketMsg; 
	sockMsg->FormatSocketMsg(strRev); 
	strRev=Formatstr(sockMsg); 
	delete sockMsg; 
	m_strDisplay+=strRev; 
	SetDlgItemText(IDC_EDIT_REV,m_strDisplay); 
	CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_REV); 
	int nlen=pEdit->GetLineCount(); 
	pEdit->LineScroll(nlen-1); 
} 
 
 
void CClient1_2Dlg::OnSendmsg()  
{ 
	CString strMsg; 
	int nlen=1024; 
	if (m_bConnect==false) 
	{ 
		AfxMessageBox("You must connect first!",MB_OK|MB_ICONEXCLAMATION); 
		return; 
	} 
	GetDlgItemText(IDC_EDIT_SEND,strMsg); 
	if (strMsg.IsEmpty()) 
	{ 
		AfxMessageBox("Enter message!"); 
		return; 
	} 
//	strMsg.FormatMessage(strMsg); 
//	m_strDisplay+=strMsg; 
//	formatstr(m_strDisplay); 
//	char ch[2]; 
//	DisDlgRev(ch,0); 
//	char chMsg[1024]; 
//	SocketDef sockMsg; 
	if (strMsg!="") 
	{ 
		CSocketMsg* sockMsg=new CSocketMsg; 
		sockMsg->Time=GetCurTime(); 
		sockMsg->ClientID=m_strID; 
		sockMsg->SendMsg=strMsg; 
		strMsg=sockMsg->FormatMsg(); 
		nlen=strMsg.GetLength(); 
		strMsg.TrimRight(); 
		m_sockClient.Send(strMsg,nlen,0); 
		delete sockMsg; 
	} 
	strMsg.Empty(); 
	SetDlgItemText(IDC_EDIT_SEND,strMsg); 
} 
 
 
 
 
void CClient1_2Dlg::OnCon()  
{ 
	CSetConnect dlg; 
	dlg.DoModal(); 
	m_nPort=dlg.m_nPort; 
	m_strIP=dlg.m_sIP; 
	m_strID=dlg.m_strID; 
	if (m_bConnect==TRUE) 
	{ 
		AfxMessageBox("Sorry you have connected!"); 
		return; 
	} 
	 
	if (m_sockClient.Connect(m_strIP,m_nPort)) 
	{	 
		AfxMessageBox("connect successful"); 
		m_bConnect=TRUE; 
		CSocketMsg* sockMsg=new CSocketMsg; 
		CString strMsg("悄然走进聊天室"); 
		sockMsg->Time=GetCurTime(); 
		sockMsg->ClientID=m_strID; 
		sockMsg->SendMsg=strMsg; 
		sockMsg->SystemMsg=SM_CONNECTSEVER; 
		strMsg=sockMsg->FormatMsg(); 
		int nlen=strMsg.GetLength(); 
		strMsg.TrimRight(); 
		m_sockClient.Send(strMsg,nlen,0); 
		delete sockMsg; 
	} 
	else 
		AfxMessageBox("fail to connect"); 
} 
 
 
void CClient1_2Dlg::OnQuit()  
{ 
	// TODO: Add your command handler code here 
 
 
	if (m_bConnect) 
	{ 
		m_sockClient.Close(); 
	} 
	 
 
	CDialog::OnCancel(); 
} 
 
LRESULT CClient1_2Dlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	switch(message) { 
	case WM_UPDLGDATA: 
		DisDlgRev((char*)wParam,(int)lParam); 
		break; 
	case WM_SETNEWSOCKET: 
		SetNewClient((char*)wParam,(int)lParam); 
		break; 
	case WM_CLOSECLIENT: 
		CloseClient((char*)wParam,(int)lParam); 
		break; 
	case WM_ALLARM: 
		Allarm(); 
		break; 
	default: 
		break; 
	} 
	return CDialog::DefWindowProc(message, wParam, lParam); 
} 
CString CClient1_2Dlg::Formatstr(CSocketMsg *pMsg) 
{ 
	CString str; 
	str+=pMsg->Time; 
	str+="  ["; 
	str+=pMsg->ClientID; 
	str+="] :"; 
	str+=13; 
	str+=10; 
	str+=pMsg->SendMsg; 
	str+=13; 
	str+=10; 
	return str; 
} 
CString CClient1_2Dlg::GetCurTime() 
{ 
	CString strtime; 
	CTime time=CTime::GetCurrentTime(); 
	strtime=time.Format("%H:%M:%S"); 
	return strtime; 
} 
 
BOOL CClient1_2Dlg::DestroyWindow()  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	CSocketMsg* sockMsg=new CSocketMsg; 
	CString strMsg("黯然离开聊天室"); 
	sockMsg->Time=GetCurTime(); 
	sockMsg->ClientID=m_strID; 
	sockMsg->SendMsg=strMsg; 
	sockMsg->SystemMsg=SM_CLOSECLIENT; 
	strMsg=sockMsg->FormatMsg(); 
	int nlen=strMsg.GetLength(); 
	strMsg.TrimRight(); 
	m_sockClient.Send(strMsg,nlen,0); 
	delete sockMsg; 
	delete m_pStringList; 
	return CDialog::DestroyWindow(); 
} 
void CClient1_2Dlg::OnListChange(CString ClientID) 
{ 
	CListCtrl* pList=(CListCtrl*)GetDlgItem(IDC_LIST); 
	if (m_pStringList->IsEmpty()) 
	{ 
		pList->SetImageList(&m_imageList,LVSIL_NORMAL); 
		m_pStringList->AddTail(ClientID); 
		pList->InsertItem(0,ClientID,0); 
		return; 
	} 
	 
	m_pStringList->AddTail(ClientID); 
	pList->InsertItem(m_pStringList->GetCount()-1,ClientID); 
 
 
 
} 
void CClient1_2Dlg::SetNewClient(char * chDlgText, int nDlgLen) 
{ 
	CString sDlg(chDlgText); 
 
	CSocketMsg* sockMsg=new CSocketMsg; 
	sockMsg->FormatSocketMsg(sDlg); 
	CString strID=sockMsg->ClientID; 
	delete sockMsg; 
	DisDlgRev(chDlgText,nDlgLen);  
	OnListChange(strID); 
 
} 
void CClient1_2Dlg::CloseClient(char * chDlgText, int nDlgLen) 
{ 
	CString sDlg(chDlgText); 
 
	CSocketMsg* sockMsg=new CSocketMsg; 
	sockMsg->FormatSocketMsg(sDlg); 
	CString strID=sockMsg->ClientID; 
	delete sockMsg; 
//删除列表上的用户 
	CListCtrl* pList=(CListCtrl*)GetDlgItem(IDC_LIST); 
	POSITION pos=m_pStringList->Find(strID); 
	m_pStringList->RemoveAt(pos); 
 
 
//	pList->DeleteItem(ix); 
	for (int ix=0;ix<=m_pStringList->GetCount();ix++) 
	{ 
	 
		if (pList->GetItemText(ix,0)==strID) 
		{ 
			pList->DeleteItem(ix); 
			pList->UpdateData(); 
			return; 
		} 
	} 
	DisDlgRev(chDlgText,nDlgLen);  
} 
 
void CClient1_2Dlg::OnSavemsg()  
{ 
	// TODO: Add your command handler code here 
	CFileDialog dlg(FALSE,"*.*|*.*",NULL,NULL,"*.dat|*.dat||"); 
	CString strfilename; 
	BOOL bOk=dlg.DoModal(); 
	if(bOk==IDOK) 
	{ 
		strfilename=dlg.GetPathName(); 
		UpdateData(FALSE); 
	} 
	else if (bOk==IDCANCEL) 
		return; 
	CFile fSave; 
	CFileException ex; 
//	char* pFileName="ClientMsg.dat"; 
	if (!fSave.Open(strfilename,CFile::modeWrite|CFile::modeNoTruncate|CFile::modeCreate,&ex)) 
	{ 
		#ifdef _DEBUG 
			afxDump << "File could not be opened " << ex.m_cause << "\n"; 
		#endif 
	} 
	fSave.Write(strMessage,nMessageLen); 
	fSave.Close(); 
	AfxMessageBox("您已经安全退出,并保存了本次聊天纪录"); 
	OnQuit(); 
} 
 
void CClient1_2Dlg::OnAbout()  
{ 
	// TODO: Add your command handler code here 
	CAboutDlg dlg; 
	dlg.DoModal(); 
} 
void CClient1_2Dlg::Allarm() 
{ 
	AfxMessageBox("有人有话说",MB_ICONASTERISK,0); 
} 
 
void CClient1_2Dlg::OnAllarm()  
{ 
	// TODO: Add your control notification handler code here 
		CSocketMsg* sockMsg=new CSocketMsg; 
	//	sockMsg->Time=GetCurTime(); 
	//	sockMsg->ClientID=m_strID; 
	//	sockMsg->SendMsg=strMsg; 
		sockMsg->SystemMsg=SM_ALLARM; 
		CString strMsg; 
		strMsg=sockMsg->FormatMsg(); 
		int nlen=strMsg.GetLength(); 
		strMsg.TrimRight(); 
		m_sockClient.Send(strMsg,nlen,0); 
		delete sockMsg; 
}