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

📄 maindlg.cpp

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

#include "stdafx.h"
#include "NetQQ.h"
#include "MainDlg.h"
#include "UserInfo.h"
#include "FileShareDlg.h"
#include "ChatDlg.h"
#include "MineInfo.h"
#include "CnfmUserDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainDlg dialog


CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMainDlg::IDD, pParent)
{
	m_hFileShareWnd = NULL;
	m_hModifyIfWnd  = NULL;
	m_hUserInfoWnd  = NULL;
	m_hConfirmWnd   = NULL;
	m_hPsPlaceWnd   = NULL;

	m_pLoadDlg      = (CNetQQDlg*)pParent;
	//{{AFX_DATA_INIT(CMainDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMainDlg)
	DDX_Control(pDX, IDC_LST_USER, m_lstUserList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
//{{AFX_MSG_MAP(CMainDlg)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_FILESHARE, OnBtnFileshare)
	ON_NOTIFY(NM_DBLCLK, IDC_LST_USER, OnDblclkLstUser)
	ON_NOTIFY(NM_RCLICK, IDC_LST_USER, OnRclickLstUser)
	ON_WM_MOVING()
	ON_WM_NCMOUSEMOVE()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_MINEINFO, OnBtnMineinfo)
	ON_BN_CLICKED(IDC_BTN_CHANGEUSER, OnBtnChangeuser)
	ON_BN_CLICKED(IDC_BTN_SENDCALL, OnBtnSendcall)
	ON_BN_CLICKED(IDC_BTN_PERSONALPLACE, OnBtnPersonalplace)
	ON_BN_CLICKED(IDC_BTN_FILETRONFER, OnBtnFiletronfer)
	ON_WM_MOUSEMOVE()
	ON_BN_CLICKED(IDC_BUT_EXITMAIN, OnButExitmain)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_MIDMSG,  OnMidMsg)
	ON_MESSAGE(WM_FILEEVENT,  OnFileEvent)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainDlg message handlers

void CMainDlg::OnCancel() 
{
	
}


void CMainDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}


void CMainDlg::OnClose() 
{
	m_pLoadDlg->DelClentList();
	m_pLoadDlg->DestroyWindow();
	delete this;
}

///////////////////////////////////////////////////////
//添加用户
void CMainDlg::AddUser(tagClientInfo* LoadUserIf)
{
	tagClientInfo* pClntIf = LoadUserIf;

	tagClientInfo* pClient = new tagClientInfo;
			
	CString strNewUser     = pClntIf->m_strUsrName + "[" 
							 + pClntIf->m_strUsrId + "]";
	int nFaceIndex         = atoi(pClntIf->m_strFace);
	
	int nCount = m_lstUserList.GetItemCount();
	m_lstUserList.InsertItem(nCount, strNewUser, nFaceIndex);
	
}

//////////////////////////////////////////////////////
//删除用户
void CMainDlg::DelUser(char* sLeaveUserId)
{
	int nIndex = GetIndexInList(sLeaveUserId);
	if(!m_strListRowValue[nIndex].IsEmpty())
	{
		KillTimer(nIndex);
		m_strListRowValue[nIndex].Empty();
	}

	for(int i = nIndex + 1 ; i < m_lstUserList.GetItemCount(); i++)
	{
		if(!m_strListRowValue[i].IsEmpty())
		{
			KillTimer(i);
			m_lstUserList.SetItemText(i, 0, m_strListRowValue[i]);
		}
	}
	m_lstUserList.DeleteItem(nIndex);
	delete[] sLeaveUserId;

	for(i = nIndex ; i < m_lstUserList.GetItemCount(); i++)
	{
		if(!m_strListRowValue[i + 1].IsEmpty())
		{
			////////////////////////////////////////////////////////
			//获取选中用户的ID
			CString strUserID;
			strUserID = m_lstUserList.GetItemText(i, 0);
			strUserID = strUserID.Mid(strUserID.ReverseFind('[') + 1);
			strUserID = strUserID.Left(strUserID.GetLength() - 1);
			tagClientInfo* pClient = GetUserNode(strUserID);
			pClient->m_nIndexInList = i;
			m_strListRowValue[i] = m_strListRowValue[i + 1];
			m_strListRowValue[i + 1].Empty();
			SetTimer(pClient->m_nIndexInList, 400, NULL);
		}
	}	
}

BOOL CMainDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	// TODO: Add extra initialization here
	/////////////////////////////////////////////////////////////////////////
	//初始化列表控件
	m_lstUserList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	m_lstUserList.SetBkColor(RGB(223, 235, 255));
	m_lstUserList.SetTextBkColor(RGB(223, 235, 255));
	m_lstUserList.SetTextColor(RGB(55, 15, 14));
	
	m_lstUserList.InsertColumn(0, "", LVCFMT_LEFT, 155, 0);

	m_myButtonMyinfo.AutoLoad(IDC_BTN_MINEINFO,this);
	m_myButtonRoom.AutoLoad(IDC_BTN_PERSONALPLACE,this);
	m_myButtonShare.AutoLoad(IDC_BTN_FILESHARE,this);	
	m_myButtonFileSend.AutoLoad(IDC_BTN_FILETRONFER,this);
	m_myButtonCall.AutoLoad(IDC_BTN_SENDCALL,this);
	m_myButtonChange.AutoLoad(IDC_BTN_CHANGEUSER,this);
	m_myButtonClose.AutoLoad(IDC_BUT_EXITMAIN,this);

	/////////////////////////////////////////////////////////////////////////
	//图像控制与列表控制绑定
	m_imgFaceList.Create(17, 17, TRUE, 5, 5);
	UINT nFaceID[] = {IDI_FACE0, IDI_FACE1, IDI_FACE2, IDI_FACE3, IDI_FACE4};
	for(int i = 0; i < 5; i++)
	{
		m_imgFaceList.Add(AfxGetApp()->LoadIcon(nFaceID[i]));
	}
	m_lstUserList.SetImageList(&m_imgFaceList,LVSIL_SMALL);

	m_pMyInfoNode = GetUserNode(m_pLoadDlg->m_strUserId);
	SetWindowText(m_pMyInfoNode->m_strUsrName + "[" + m_pMyInfoNode->m_strUsrId + "]");
	////////////////////////////////////////////////////////////////////////
	//拥护列表悬停
	m_ScreenX = GetSystemMetrics(SM_CXSCREEN);
	m_ScreenY = GetSystemMetrics(SM_CYSCREEN);

	SetTimer(9999,50,NULL);

	NearSide(GetSafeHwnd());	//如果位置合适让窗体自动隐藏


	///////////////////////////////////////////////////////////////////////
	//P2P文件传输注册网络事件
	int nret = WSAAsyncSelect(m_pLoadDlg->m_FT.GetFlieListenSock(), m_hWnd, 
                              WM_FILEEVENT, FD_ACCEPT);

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


/*---------------------------------------------------------------------------
 创建共享文件窗口
 --------------------------------------------------------------------------*/
void CMainDlg::OnBtnFileshare() 
{
	// TODO: Add your control notification handler code here
	if(m_hFileShareWnd != NULL)
	{
		::SetActiveWindow(m_hFileShareWnd);
		return;
	}
	CFileShareDlg* pFileShareDlg;
	pFileShareDlg = new CFileShareDlg(this);
	pFileShareDlg->Create(IDD_FILESHARE);
	m_hFileShareWnd = pFileShareDlg->m_hWnd;
	pFileShareDlg->Filesharebrush();
	
	CString strCaption = "共享文件 [" + m_pLoadDlg->m_strUserId + "]";
	pFileShareDlg->SetWindowText(strCaption);
	pFileShareDlg->ShowWindow(true);
}

/*---------------------------------------------------------------------------
 客户端消息中转消息回调函数
 --------------------------------------------------------------------------*/
void CMainDlg::OnMidMsg(WPARAM wParam, LPARAM lParam)
{
	switch(lParam)
	{
	case 0003://添加登陆用户
		{
			AddUser((tagClientInfo*)wParam);
			break;
		}

	case 9004://好友资料查询
	{
		CString strBuff = (char*)wParam;
		CMsgBag mbTool;
		CString strUId = mbTool.GetDesUserId(strBuff);
		
		tagClientInfo* pTemp = GetUserNode(strUId);
		if(pTemp->m_hUserInfoWnd)
		{
			::PostMessage(pTemp->m_hUserInfoWnd, WM_SHOW, wParam, 0);
		}
		break;
	}

	case 9005://聊天
		{
			CString strBuff = (char*)wParam;

			CMsgBag mbTool;
			CString strUId = mbTool.GetDesUserId(strBuff);
			tagClientInfo* pTemp = GetUserNode(strUId);
			//创建聊天窗口
			CChatDlg* pChatDlg = (CChatDlg*)CreateChatWindow(strUId);

			if(!pChatDlg->IsWindowVisible() && pTemp->m_nIndexInList == -1)
			{
				pTemp->m_nIndexInList = GetIndexInList(strUId);
				m_strListRowValue[pTemp->m_nIndexInList] = m_lstUserList.GetItemText(pTemp->m_nIndexInList, 0);
				SetTimer(pTemp->m_nIndexInList, 400, NULL);
			}
			::PostMessage(pTemp->m_hChatWnd, WM_CHATINFO, wParam, 9005);
			break;
		}

	/********************************周涛:9007服务器公告接收****************/
		case 9007:
		{
			/*[Delete] dong 12-27*/
			/*CString strBuff = (char*)wParam;
			CMsgBag bg;
			CString Call;
			bg.SplitDate (strBuff,&Call);
			MessageBox(Call.GetBuffer(0),"服务器消息",MB_OK);*/ 
			
			/*[Add] dong 12-27*/
			CString strBuff = (char*)wParam;

			CMsgBag mbTool;
			CString strUId = mbTool.GetDesUserId(strBuff);
			
			tagClientInfo* pTemp = GetUserNode(strUId);
			
			if(pTemp->m_hChatWnd)
			{
				::SetActiveWindow(pTemp->m_hChatWnd);
			}
			else
			{
				CChatDlg* dlgChatDlg = new CChatDlg(this);
				dlgChatDlg->Create(IDD_CHATDLG);
				pTemp->m_hChatWnd = dlgChatDlg->m_hWnd;
				dlgChatDlg->m_pUserNode = pTemp;

				dlgChatDlg->GetDlgItem(IDC_ID)->SetWindowText("10000");
				dlgChatDlg->GetDlgItem(IDC_NAME)->SetWindowText("系统公告");
				dlgChatDlg->GetDlgItem(IDC_IP)->SetWindowText(pTemp->m_strIp);
				dlgChatDlg->m_urFace.SetIcon(AfxGetApp()->LoadIcon(IDI_FACE0));
				dlgChatDlg->ShowWindow(true);				
			}
			::PostMessage(pTemp->m_hChatWnd, WM_CHATINFO, wParam, 9007);
			break;
		}
	
	case 9009: //共享文件信息
	{
		if(m_hFileShareWnd)
		{
			::PostMessage(m_hFileShareWnd, WM_FILESHARE, wParam, 0);
		}
		break;
	}
		
	case 9010:	//用户下线
		{
			DelUser((char*)wParam);
			break;
		}
/********************************周涛:9012显示自已信息返回***************/		
		case 9012:
		{
			if(m_hModifyIfWnd)
			{
				::PostMessage(m_hModifyIfWnd, WM_MEINFO, wParam, 9012);
			}
			break;
		}
/********************************周涛:9011修改自已信息返回***************/		
		case 9011:
		{
			if(m_hModifyIfWnd)
			{
				::PostMessage(m_hModifyIfWnd, WM_MEINFO, wParam, 9011);
			}
			break;
		}

		case 9014: //共享文件信息
		{
			if(m_hPsPlaceWnd)
			{
				::PostMessage(m_hPsPlaceWnd, WM_FILESHARE, wParam, 0);
			}
			break;
		}

		case 9998://服务器暂停
		{
			AfxMessageBox("已和服务器断开连接...");
			OnBtnChangeuser();
		}
	}
} 

void CMainDlg::OnDblclkLstUser(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	//char msg[20];

	POSITION pos = m_lstUserList.GetFirstSelectedItemPosition();// 得到项目索引
	int nIndex = m_lstUserList.GetNextSelectedItem(pos);
	if(-1 == nIndex)
	{
		return;
	}
	///////////////////////////////////////////
	//取ID
	CString strUId;
	CString strInfo;
	strInfo = m_lstUserList.GetItemText(nIndex,0);
	if(strInfo.IsEmpty())
	{
		strInfo = m_strListRowValue[nIndex];
	}
	strUId = strInfo.Mid(strInfo.ReverseFind('[') + 1);
	strUId = strUId.Left(strUId.GetLength() - 1);
	
	//创建聊天窗口
	CChatDlg* dlgChatDlg = (CChatDlg*)CreateChatWindow(strUId);
	dlgChatDlg->ShowWindow(true);
	*pResult = 0;
}

/////////////////////////////////////////////////////////////////////////////
//右键单击查询好友资料
void CMainDlg::OnRclickLstUser(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	////////////////////////////////////////////////////////////
	//获取项目索引
	POSITION pos = m_lstUserList.GetFirstSelectedItemPosition();
	int nIndex = m_lstUserList.GetNextSelectedItem(pos);
	
	//没选任何选项
	if(-1 == nIndex)
	{
		return;
	}
	
	/////////////////////////////////////////////////////////
	//获取选中用户的ID
	CString strUserID;
	strUserID = m_lstUserList.GetItemText(nIndex,0);
	strUserID = strUserID.Mid(strUserID.ReverseFind('[') + 1);
	strUserID = strUserID.Left(strUserID.GetLength() - 1);
	
	////////////////////////////////////////////////////////
	//从用户链表中查找该用户
	tagClientInfo* pUserNode = GetUserNode(strUserID);

	/////////////////////////////////////////////////
	//判断是否已经打开此用户的资料查看窗口
	if(pUserNode->m_hUserInfoWnd)
	{//已经打开,激活该窗口
		::SetActiveWindow(pUserNode->m_hUserInfoWnd);
		return;
	}
	
	////////////////////////////////////////////////
	//没有打开,为该用户创建一个资料查看窗口
	CUserInfo*  pUserInfo = new CUserInfo(this);
	pUserInfo->Create(IDD_USERINFO);
	pUserNode->m_hUserInfoWnd = pUserInfo->m_hWnd;
	
	////////////////////////////////////////////////
	//发送消息给服务器对该用户进行查询返回该用户资料
	CMsgBag mbTool;
	tagPacketParam pp;
	CString strBuf;
	pp.order = "1004";
	pp.UserId = strUserID;
	pp.paramNum = 1;
	mbTool.DataPackage(strBuf, &pp);
	(m_pLoadDlg->GetCClntSock()).OnSend(strBuf);
	pUserInfo->m_pUserNode = pUserNode;

	Sleep(100);
	pUserInfo->SetWindowText("[" + strUserID + "]" + "好友资料");
	pUserInfo->ShowWindow(true);

	*pResult = 0;
}


/////////////////////////////////////////////////////////////////////////
//根据用户id获取该用户在本地链表中的结点
tagClientInfo* CMainDlg::GetUserNode(CString strUserId)
{
	tagClientInfo* pTemp;
	POSITION pos = (m_pLoadDlg->m_pClntList).GetHeadPosition();
	for(int i = 0; i < (m_pLoadDlg->m_pClntList).GetCount(); i++)
	{
		pTemp = (m_pLoadDlg->m_pClntList).GetNext(pos);
		if(pTemp->m_strUsrId == strUserId)
		{
			return pTemp;
			break;
		}
	}
	return NULL;
}


////////////////////////////////////////////////////////////////////////
tagClientInfo* CMainDlg::GetMyNode()
{
	return m_pMyInfoNode;
}

///////////////////////////////////////////////////////////////////////////////
//用户列表的悬停操作
//隐藏用户列表
void CMainDlg::HideSide(HWND hWnd, BOOL hide)
{
	RECT            rc;
	int             xStep, yStep;
	int             xEnd, yEnd;
	int             width;
	int             height;
	register int    i;
	
	GetWindowRect(&rc);
	width = rc.right - rc.left;
	height = rc.bottom - rc.top;
	//下边判断窗体该如何移动,由停靠方式决定
	switch (m_alignType)
	{
	case ALIGN_TOP:
		{
			//向上移藏
			xStep = 0;
			xEnd = rc.left;
			if (hide)
			{
				yStep = -rc.bottom / MOVES;
				yEnd = -height + NEAR_SIDE;
			}
			else
			{
				yStep = -rc.top / MOVES;
				yEnd = 0;
			}
			break;
		}
	case ALIGN_LEFT:
		{
			//向左移藏
			yStep = 0;
			yEnd = rc.top;
			if (hide)

⌨️ 快捷键说明

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