資源簡介
基于CSocket編程的C++多人聊天室,使用MFC,C/S,服務端可以看到在線人數
代碼片段和文件信息
//?Chat.cpp?:?實現文件
//
#include?“stdafx.h“
#include?“客戶端.h“
#include?“Chat.h“
#include?“afxdialogex.h“
//?CChat?對話框
IMPLEMENT_DYNAMIC(CChat?CDialogEx)
CChat::CChat(CWnd*?pParent?/*=NULL*/)
:?CDialogEx(CChat::IDD?pParent)
?m_receiveMsg(_T(““))
?m_sendMsg(_T(““))
{
}
CChat::~CChat()
{
}
void?CChat::DoDataExchange(CDataExchange*?pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX?IDC_RECEIVEMSG?m_receiveMsg);
DDX_Text(pDX?IDC_SENDMSG?m_sendMsg);
DDX_Control(pDX?IDC_RECEIVEMSG?m_receiveCtrl);
}
BEGIN_MESSAGE_MAP(CChat?CDialogEx)
ON_BN_CLICKED(IDC_SEND?&CChat::OnBnClickedSend)
ON_BN_CLICKED(IDC_CANCEL?&CChat::OnBnClickedCancel)
ON_WM_CLOSE()
END_MESSAGE_MAP()
//?CChat?消息處理程序
void?CChat::OnBnClickedSend()
{
//?TODO:?在此添加控件通知處理程序代碼
CTime?time;
time?=?CTime::GetCurrentTime();??//獲取現在時間
CString?strTime?=?time.Format(“%Y-%m-%d?%H:%M:%S??“);
theApp.head.type?=?3;
UpdateData(true);
m_receiveMsg?+=?strTime?+?(CString)“(“?+?theApp.m_userID?+?(CString)“)?“?+?theApp.m_userNAME?+?(CString)“\r\n????“?+?m_sendMsg?+?(CString)“\r\n“;
CHead?Msg;
Msg.type?=?3;
strcpy_s(Msg.fromID(CStringA)theApp.m_userID);
strcpy_s(Msg.usermsg.NAME(CStringA)theApp.m_userNAME);
strcpy_s(Msg.usermsg.ID(CStringA)theApp.m_userID);
strcpy_s(Msg.toID(CStringA)m_toID);
strcpy_s(Msg.iptheApp.head.ip);
strcpy_s(Msg.msg(CStringA)m_sendMsg);
m_sendMsg?=?““;
UpdateData(false);
m_receiveCtrl.LineScroll(m_receiveCtrl.GetLineCount()-1);
theApp.m_tcp->Send(&Msgsizeof(Msg));
}
void?CChat::OnBnClickedCancel()
{
//?TODO:?在此添加控件通知處理程序代碼
OnClose();
CDialogEx::OnCancel();
}
BOOL?CChat::OnInitDialog()
{
CDialogEx::OnInitDialog();
//?TODO:??在此添加額外的初始化
m_caption?=(CString)“正與(“?+??m_toID?+?(CString)“)?“?+?m_name?+?(CString)“?聊天中“;
SetWindowTextW(m_caption);//設置對話框標題
return?TRUE;??//?return?TRUE?unless?you?set?the?focus?to?a?control
//?異常:?OCX?屬性頁應返回?FALSE
}
void?CChat::OnClose()
{
//?TODO:?在此添加消息處理程序代碼和/或調用默認值
theApp.m_userMsg.erase(m_toID);
CDialogEx::OnClose();
}
評論
共有 條評論