⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatdlg.cpp

📁 类似于QQ的聊天工具,分为客户端和服务器端,有共享空间,能发布公告,可传输文件
💻 CPP
字号:
// ChatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NetQQ.h"
#include "ChatDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChatDlg dialog


CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChatDlg::IDD, pParent)
{
	m_pMainDlg  = (CMainDlg*)pParent;
	m_faqStatus = faqInit;
	m_hMutex    = NULL;
	//{{AFX_DATA_INIT(CChatDlg)
	m_strUserChat = _T("");
	//}}AFX_DATA_INIT
}


void CChatDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChatDlg)
	DDX_Control(pDX, IDC_FILETRANS, m_urFace);
	DDX_Text(pDX, IDC_EDIT_CHAT, m_strUserChat);
	DDV_MaxChars(pDX, m_strUserChat, 500);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
	//{{AFX_MSG_MAP(CChatDlg)
	ON_WM_LBUTTONDOWN()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BUT_CLOSE, OnButClose)
	ON_BN_CLICKED(IDC_BUT_SEND, OnButSend)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUT_CLOSEChat, OnBUTCLOSEChat)
	ON_BN_CLICKED(IDC_BUT_MIN5, OnButMin5)
	ON_BN_CLICKED(IDC_BUT_CLOSE5, OnButClose5)
	ON_BN_CLICKED(IDC_FILETRANSFER, OnFiletransfer)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_FYES, OnBtnFyes)
	ON_BN_CLICKED(IDC_BTN_FNO, OnBtnFno)
	ON_BN_CLICKED(IDC_BTN_CANCLE, OnBtnCancle)
	ON_WM_MOUSEMOVE()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_CHATINFO, OnChatInfo)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatDlg message handlers

void CChatDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y)); 
	CDialog::OnLButtonDown(nFlags, point);
}

void CChatDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CRect rcWnd;
	GetWindowRect(rcWnd);
	m_rgn.m_hObject = CreateRoundRectRgn(0, 0,rcWnd.Width() , rcWnd.Height() ,16, 16 );
	this->SetWindowRgn(m_rgn,true);	
	// TODO: Add your message handler code here
	
	// Do not call CDialog::OnPaint() for painting messages
}

void CChatDlg::OnButClose() 
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CChatDlg::OnOK()
{

}

void CChatDlg::OnCancel()
{
	OnClose();
}



void CChatDlg::OnButSend() 
{
	// TODO: Add your control notification handler code here
	//////////////////////////////////////////////////////
	GetDlgItem(IDC_EDIT_CHAT)->GetWindowText(m_strUserChat);
	if(m_strUserChat.IsEmpty())
	{
		GetDlgItem(IDC_EDIT_CHAT)->SetFocus();
		MessageBox("请不要发送空白消息!");	
		return;
	}
    CString strSendToId;
	GetDlgItem(IDC_ID)->GetWindowText(strSendToId);

	///以下是打包
	CString strBuff;
	tagPacketParam BagChat;
	CMsgBag  m_bagTool;

	if(strSendToId == CString("10000"))	//发公告 [add] dong 12-17

	{
		BagChat.order    = "1007";
		BagChat.SentTo   = m_pUserNode->m_strUsrId; 
		BagChat.UserName = m_pUserNode->m_strUsrName;
		BagChat.paramNum = 2;
	}
	else
	{
		BagChat.order = "1005";
		BagChat.paramNum  = 1;
		BagChat.SentTo    = strSendToId;
	}
	BagChat.UserChar  = m_strUserChat;
	m_bagTool.DataPackage(strBuff, &BagChat);

	///////////////////////////////////////////////
	//问题在这里 包打好了 这样修改后又不重新修改包长
	//m_bagTool.SetSourUserId(strSendToId,strBuff); 
	//MessageBox(strBuff);
	m_pMainDlg->m_pLoadDlg->GetCClntSock().OnSend(strBuff);	

	CString strChatMsg;
	CString strMyName = m_pMainDlg->GetMyNode()->m_strUsrName;
	CString strMyId   = m_pMainDlg->GetMyNode()->m_strUsrId;
	
	CTime time = CTime::GetCurrentTime();
	CString strTime = time.Format("20%y-%m-%d %H:%M:%S");

	strChatMsg.Format("%s[%s] (%s)\r\n%s\r\n\r\n",
						strMyName, strMyId,strTime, m_strUserChat);

	m_pUserNode->m_strChatlog += strChatMsg;
	GetDlgItem(IDC_EDIT_CHATLOG)->SetWindowText(m_pUserNode->m_strChatlog);
	GetDlgItem(IDC_EDIT_CHAT)->SetWindowText("");
	GetDlgItem(IDC_EDIT_CHAT)->SetFocus();
}

/////////////////////////////////////////////////////////////////////////////
//聊天或系统公告消息接收  [Modify] dong 12-17
void CChatDlg::OnChatInfo(WPARAM wParam, LPARAM lParam)
{
	CString strBuf = (char*)wParam;

	CMsgBag mbTool;
	CString strChat;
	CString strChatMsg;
	CString strId = mbTool.GetDesUserId(strBuf);
	CTime time = CTime::GetCurrentTime();
	CString m_strTime = time.Format("20%y-%m-%d %H:%M:%S");

	mbTool.SplitDate(strBuf, &strChat);

	if(lParam == 9005)//好友聊天
	{
		CString strName = m_pUserNode->m_strUsrName;
		strChatMsg.Format("%s[%s] (%s)\r\n%s\r\n\r\n",
										strName, strId,m_strTime, strChat);
	}

	else if(lParam == 9007)//系统公告 [add] dong 12-17
	{
		strChatMsg.Format("【系统公告】 %s\r\n %s\r\n\r\n",
											m_strTime, strChat);
	}

	m_pUserNode->m_strChatlog += strChatMsg;
	GetDlgItem(IDC_EDIT_CHATLOG)->SetWindowText(m_pUserNode->m_strChatlog);

	delete (char*)wParam;
}


BOOL CChatDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	
	//自己定义按纽
	m_myButtonMin.AutoLoad(IDC_BUT_MIN5,this);	
	m_myButtonClose.AutoLoad(IDC_BUT_CLOSE5,this);

	m_myButtonSend.AutoLoad(IDC_BUT_SEND,this);
	m_myButtonCloseWin.AutoLoad(IDC_BUT_CLOSEChat,this);

	m_myButtonFile.AutoLoad(IDC_FILETRANSFER,this);
	m_myButtonAccept.AutoLoad(IDC_BTN_FYES,this);		
	m_myButtonRefuse.AutoLoad(IDC_BTN_FNO,this);
	m_myButtonCancle.AutoLoad(IDC_BTN_CANCLE,this);

	// TODO: Add extra initialization here
	m_ttToolTip.CreateToolTip(this);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CChatDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	delete this;
}

void CChatDlg::OnBUTCLOSEChat() 
{
	// TODO: Add your control notification handler code here
	OnClose();
}

void CChatDlg::OnButMin5() 
{
	// TODO: Add your control notification handler code here
	PostMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);	
}

void CChatDlg::OnButClose5() 
{
	// TODO: Add your control notification handler code here
	OnClose();
}

void CChatDlg::OnFiletransfer() 
{
	// TODO: Add your control notification handler code here
	DWORD exitCode = 0;
	HANDLE handle  = m_pMainDlg->m_pLoadDlg->m_FT.GetFTHandle();
    GetExitCodeThread(handle,&exitCode);
    if(exitCode == STILL_ACTIVE)
	{
		CString msg = "系统检测到文件正在传输,请捎后再试...";
		InsertNewChatInfo(msg);
		return;
	}

	CString strMyId = m_pMainDlg->GetMyNode()->m_strUsrId;
	m_pMainDlg->m_pLoadDlg->m_FT.Filesend(this, strMyId);
}

void CChatDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	DWORD exitCode = 0;
	HANDLE handle  = m_pMainDlg->m_pLoadDlg->m_FT.GetFTHandle();
    GetExitCodeThread(handle,&exitCode);
    if(exitCode == STILL_ACTIVE)
	{
		CString msg = "文件正在传输,关闭将终止文件传输,确定关闭吗?";
		int ret	= MessageBox(msg, "系统警告", MB_OKCANCEL | MB_ICONWARNING);
		if(ret == IDOK)
		{
			_ThreadParam* pTmp = m_pMainDlg->m_pLoadDlg->m_FT.GetFtThreadParam();
			TerminateThread(handle, exitCode);		
			closesocket(pTmp->m_sSock);
			if(!m_hMutex)
			{
				pTmp->m_pFile->Close();
			}
			delete pTmp;
			CloseHandle(handle);
		}
		else
		{
			return;
		}	
	}
	
	m_pUserNode->m_hChatWnd = NULL;
	OnDestroy();
}


/////////////////////////////////////////////////////////////////////////////
//接收文件传输
void CChatDlg::OnBtnFyes() 
{
	// TODO: Add your control notification handler code here
	
	m_faqStatus = faqYes;
	GetDlgItem(IDC_BTN_FYES)->ShowWindow(false);
	GetDlgItem(IDC_BTN_FNO)->ShowWindow(false);
	ReleaseMutex(m_hMutex);
	m_hMutex = NULL;
}


/////////////////////////////////////////////////////////////////////////////
//拒绝文件传输
void CChatDlg::OnBtnFno() 
{
	// TODO: Add your control notification handler code here
	m_faqStatus = faqNo;
	GetDlgItem(IDC_BTN_FYES)->ShowWindow(false);
	GetDlgItem(IDC_BTN_FNO)->ShowWindow(false);
	ReleaseMutex(m_hMutex);
	m_hMutex = NULL;
}


/////////////////////////////////////////////////////////////////////////////
//向用户通知文件传输请求消息
void CChatDlg::ShowFileTransferInfo(CString strFileInfo)
{
	GetDlgItem(IDC_BTN_FYES)->ShowWindow(true);
	GetDlgItem(IDC_BTN_FNO)->ShowWindow(true);
//	GetDlgItem(IDC_BTN_CANCLE)->ShowWindow(true);
	InsertNewChatInfo(strFileInfo);
	m_hMutex = CreateMutex(NULL, true, NULL);
}


/////////////////////////////////////////////////////////////////////////////
//获取文件传输回应标志状态
UINT CChatDlg::GetFileTransferChoose()
{
	return m_faqStatus;
}


/////////////////////////////////////////////////////////////////////////////
//设置文件传输回应标志为初始状态
void CChatDlg::InitFileAcceptQuery()
{
	m_faqStatus = faqInit;
}


/////////////////////////////////////////////////////////////////////////////
//获取文件传输回应互斥对象
HANDLE CChatDlg::GetMutex()
{
	return m_hMutex;
}


/////////////////////////////////////////////////////////////////////////////
//将新的消息加到聊天消息缓存中(文件传输用)
void CChatDlg::InsertNewChatInfo(CString strChatInfo)
{
	CTime time = CTime::GetCurrentTime();
	CString strTime = time.Format("20%y-%m-%d %H:%M:%S");

	CString strChatMsg;
	strChatMsg.Format("<%s>\r\n%s\r\n\r\n", strTime, strChatInfo);
	m_pUserNode->m_strChatlog += strChatMsg;
	GetDlgItem(IDC_EDIT_CHATLOG)->SetWindowText(m_pUserNode->m_strChatlog);
}


//////////////////////////////////////////////////////////////////////////////
void CChatDlg::SetMutexNull()
{
	m_hMutex = NULL;
}

/////////////////////////////////////////////////////////////////////////////
//间接提供文件传输接口
void CChatDlg::FileTransfer()
{
	OnFiletransfer();
}

void CChatDlg::OnBtnCancle() 
{
	// TODO: Add your control notification handler code here
	DWORD exitCode = 0;
	HANDLE handle  = m_pMainDlg->m_pLoadDlg->m_FT.GetFTHandle();
	CString msg = "确定要终止文件传输吗?";
	int ret	= MessageBox(msg, "系统警告", MB_OKCANCEL | MB_ICONWARNING);
	if(ret == IDOK)
	{
		_ThreadParam* pTmp = m_pMainDlg->m_pLoadDlg->m_FT.GetFtThreadParam();
		TerminateThread(handle, exitCode);		
		closesocket(pTmp->m_sSock);
		if(!m_hMutex)
		{
			pTmp->m_pFile->Close();
		}
		delete pTmp;
		CloseHandle(handle);
		GetDlgItem(IDC_STCT0)->SetWindowText("");
		GetDlgItem(IDC_STCT1)->SetWindowText("");
		GetDlgItem(IDC_STCT2)->SetWindowText("");
		GetDlgItem(IDC_STCT3)->SetWindowText("");
		GetDlgItem(IDC_PRGR)->ShowWindow(false);
		GetDlgItem(IDC_BTN_CANCLE)->ShowWindow(false);

	}
}

void CChatDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_ttToolTip.AddToolTip("发送文件", GetDlgItem(IDC_FILETRANSFER));
	m_ttToolTip.AddToolTip("最小化窗口", GetDlgItem(IDC_BUT_MIN5));
	m_ttToolTip.AddToolTip("关闭窗口", GetDlgItem(IDC_BUT_CLOSE5));

	CDialog::OnMouseMove(nFlags, point);
}

BOOL CChatDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(m_ttToolTip.GetToolTip().m_hWnd)
		m_ttToolTip.GetToolTip().RelayEvent(pMsg);
	return CDialog::PreTranslateMessage(pMsg);
}


void CChatDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	if(nChar == VK_CONTROL)
	{
		MessageBox("DOWN");
	}
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CChatDlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	if(nChar == VK_CONTROL)
	{
		MessageBox("UP");
	}
	CDialog::OnKeyUp(nChar, nRepCnt, nFlags);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -