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

📄 maindlg.cpp

📁 类似于QQ的聊天工具,分为客户端和服务器端,有共享空间,能发布公告,可传输文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			{
				xStep = -rc.right / MOVES;
				xEnd = -width + NEAR_SIDE;
			}
			else
			{
				xStep = -rc.left / MOVES;
				xEnd = 0;
			}
			break;
		}
	case ALIGN_RIGHT:
		{
			//向右移藏
			yStep = 0;
			yEnd = rc.top;
			if (hide)
			{
				xStep = (m_ScreenX - rc.left) / MOVES;
				xEnd = m_ScreenX - NEAR_SIDE;
			}
			else
			{
				xStep = (m_ScreenX - rc.right) / MOVES;
				xEnd = m_ScreenX - width;
			}
			break;
		}
	default:
		return;
	}
	//动画滚动窗体.
	for (i = 0; i < MOVES; i++)
	{
		rc.left += xStep;
		rc.top += yStep;
		
		//每进行一次循环到根据改变了的rc.left和rc.top改变列表的显示出来的大小
		SetWindowPos(NULL, rc.left, rc.top, 0, 0,
			SWP_NOSIZE | SWP_NOSENDCHANGING);
		RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);

	}
	
	//最后根据xEnd和yEnd确定最后的大小(即列表保留在屏幕上的部分)
	SetWindowPos(NULL, xEnd, yEnd, 0, 0, SWP_NOSIZE);

	if (!hide)	//如果窗体已被显示,设置定时器.监视鼠标.
	{
		SetTimer(9999, 50, NULL);
	}
}

//如果位置合适让窗体自动隐藏
void CMainDlg::NearSide(HWND hWnd)
{
	int             change = 0;
	RECT            rect;
	
	GetWindowRect(&rect);
	m_alignType = ALIGN_NONE;
	//向左隐藏
	if (rect.left < NEAR_SIZE)
	{
		m_alignType = ALIGN_LEFT;
		if ((rect.left != 0) && rect.right != NEAR_SIDE)
		{
			//变为紧贴左边的状态
			rect.right -= rect.left;
			rect.left = 0;
			change = 1;
		}
	}
	//向右隐藏
	else if (rect.right > m_ScreenX - NEAR_SIZE)
	{
		m_alignType = ALIGN_RIGHT;
		if (rect.right != m_ScreenX && rect.left != m_ScreenX - NEAR_SIDE)
		{
			//变为紧贴右边的状态
			rect.left += (m_ScreenX - rect.right);
			rect.right = m_ScreenX;
			change = 1;
		}
	}
	//调整上下
	if (rect.top < NEAR_SIZE)
	{
		m_alignType = ALIGN_TOP;
		if (rect.top != 0 && rect.bottom != NEAR_SIDE)
		{
			//变为紧贴上方的状态
			rect.bottom -= rect.top;
			rect.top = 0;
			change = 1;
		}
	}
	if (change)
	{
		//移动窗体到指定位置
		MoveWindow(rect.left, rect.top, rect.right - rect.left,
			rect.bottom - rect.top, TRUE);
	}
}

//窗体的移动事件
void CMainDlg::OnMoving(UINT fwSide, LPRECT pRect) 
{
	//未靠边界由pRect测试
	if (m_alignType == ALIGN_NONE)
	{
		//在上边有效距离内,自动靠拢。
		if (pRect->top < NEAR_SIZE)	
		{
			m_alignType = ALIGN_TOP;
			pRect->bottom -= pRect->top;
			pRect->top = 0;
		}
		//在左边有效距离内
		else if (pRect->left < NEAR_SIZE)	
		{
			m_alignType = ALIGN_LEFT;
			pRect->right -= pRect->left;
			pRect->left = 0;
		}
		//在右边有效距离内,m_ScreenX为屏幕宽度,可由GetSystemMetrics(SM_CYSCREEN)得到。
		else if (pRect->right + NEAR_SIZE > m_ScreenX)	
		{
			m_alignType = ALIGN_RIGHT;
			pRect->left += (m_ScreenX - pRect->right);
			pRect->right = m_ScreenX;
		}
	}
	else
	{
		//靠边界由鼠标测试
		POINT           pt;
		//获取鼠标的坐标
		GetCursorPos(&pt);
		if (m_alignType == ALIGN_TOP)
		{
			////////////////////////////////////////
			//由于我们移动窗体时,鼠标在标题栏内,//
			//当鼠标位置超过有效距离后,		  //	
			//我们可以考虑用户要向下拖动鼠标。    //
			//我们便解除上部停靠。                //
			////////////////////////////////////////
			if (pt.y > NEAR_SIZE)	
			{
				m_alignType = ALIGN_NONE;
				pRect->bottom += (NEAR_SIZE-pRect->top);
				pRect->top = NEAR_SIZE;
			}
			else
			{
				pRect->bottom -= pRect->top;
				pRect->top = 0;
				//在上部停靠时,考虑停留在左右边角。
				if (pRect->left < NEAR_SIZE)	
				{
					pRect->right -= pRect->left;
					pRect->left = 0;
				}
				else if (pRect->right + NEAR_SIZE > m_ScreenX)
				{
					pRect->left += (m_ScreenX - pRect->right);
					pRect->right = m_ScreenX;
				}
			}
			
		}
		if (m_alignType == ALIGN_LEFT)
		{
			////////////////////////////////////////////////////
			//鼠标可以在整个标题条来回移动,				  //
			//所以我们不能简单用左边界和鼠标的距离来解除停靠,//
			//这里我们在鼠标离开右边界时解除停靠。			  //
			////////////////////////////////////////////////////
			if (pt.x - pRect->right > 0)	
			{
				m_alignType = ALIGN_NONE;
				pRect->right += (NEAR_SIZE-pRect->left);
				pRect->left = NEAR_SIZE;
			}
			else
			{
				pRect->right -= pRect->left;
				pRect->left = 0;
				//考虑左上角。
				if (pRect->top < NEAR_SIZE)	
				{
					pRect->bottom -= pRect->top;
					pRect->top = 0;
				}
			}
		}
		else if (m_alignType == ALIGN_RIGHT)
		{
			//当鼠标离开左边界时,解除停靠。
			if (pt.x < pRect->left)	
			{
				m_alignType = ALIGN_NONE;
				pRect->left -= NEAR_SIZE;
				pRect->right -= NEAR_SIZE;
			}
			else
			{
				pRect->left += (m_ScreenX - pRect->right);
				pRect->right = m_ScreenX;
				//考虑右上角。
				if (pRect->top < NEAR_SIZE)	
				{
					pRect->bottom -= pRect->top;
					pRect->top = 0;
				}
			}
		}
	}
}

void CMainDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == 9999)
	{
		POINT           pt;
		RECT            rc;
		
		GetCursorPos(&pt);
		//GetWindowRect(hWnd, &rc);
		GetWindowRect(&rc);
		if (!PtInRect(&rc, pt))	//若鼠标不在窗体内,隐藏窗体.
		{
			//KillTimer(hWnd, WM_TIMER);
			KillTimer(9999);
			//HideSide(hWnd, TRUE);
			HideSide(GetSafeHwnd(),TRUE);
		}
	}
	else
	{
		CString strUser = m_lstUserList.GetItemText(nIDEvent, 0);
		if(strUser.IsEmpty())
		{
			m_lstUserList.SetItemText(nIDEvent, 0, m_strListRowValue[nIDEvent]);
		}
		else
		{
			m_lstUserList.SetItemText(nIDEvent, 0, "");
		}
	}
	CDialog::OnTimer(nIDEvent);
}

//鼠标在窗体内部的运动事件
void CMainDlg::OnNcMouseMove(UINT nHitTest, CPoint point) 
{

	RECT            rc;	
	GetWindowRect(&rc);
	if (rc.left < 0 || rc.top < 0 || rc.right > m_ScreenX)	//未显示
	{
		HideSide(GetSafeHwnd(), FALSE);
	}else
	{
		HideSide(GetSafeHwnd(), FALSE);
		SetTimer(9999,50,NULL);
	}
}

//////////////////////////////
//个人资料
void CMainDlg::OnBtnMineinfo() 
{
	if(m_hModifyIfWnd)
	{
		::SetActiveWindow(m_hModifyIfWnd);
		return;
	}
	
	CMineInfo* pMineInfo = new CMineInfo(this);
	pMineInfo->Create(IDD_MINEINFO);
	m_hModifyIfWnd = pMineInfo->m_hWnd;

	/////////////////////////
	CMsgBag mbTool;
	tagPacketParam pp;
	CString strBuf;
	pp.order = "1012";
	pp.UserId = m_pMyInfoNode->m_strUsrId;
	pp.paramNum = 1;
	mbTool.DataPackage(strBuf, &pp);
	(m_pLoadDlg->GetCClntSock()).OnSend(strBuf);

	pMineInfo->ShowWindow(true);
}


/////////////////////////////////////////////////////////////////////////////
//注销更换用户
void CMainDlg::OnBtnChangeuser() 
{
	// TODO: Add your control notification handler code here
	if(m_hFileShareWnd)
	{
		::SendMessage(m_hFileShareWnd, WM_FILESHARE, 0, 1999);
		::SendMessage(m_hFileShareWnd, WM_CLOSE, 0, 0);
	}
	if(m_hModifyIfWnd)
	{
		::SendMessage(m_hModifyIfWnd, WM_CLOSE, 0, 0);
	}
	if(m_hConfirmWnd)
	{
		::SendMessage(m_hConfirmWnd, WM_CLOSE, 0, 0);
	}
	if(m_hPsPlaceWnd)
	{
		::SendMessage(m_hFileShareWnd, WM_FILESHARE, 0, 1999);
		::SendMessage(m_hPsPlaceWnd, WM_CLOSE, 0, 0);
	}

	m_pLoadDlg->GetCClntSock().CloseClient();
	m_pLoadDlg->DelClentList();
	m_pLoadDlg->SetDlgItemText(IDC_EDIT_PSW, "");
	m_pLoadDlg->GetDlgItem(IDC_EDIT_UID)->SetFocus();
	m_pLoadDlg->ShowWindow(true);
	delete this;
}

/*---------------------------------------------------------------------------
 向系统发消息 [Add] dong 12-17
 --------------------------------------------------------------------------*/
void CMainDlg::OnBtnSendcall() 
{
	// TODO: Add your control notification handler code here
	if(GetMyNode()->m_hChatWnd)
	{
		::SetActiveWindow(GetMyNode()->m_hChatWnd);
		return;
	}

	CChatDlg* dlgChatDlg = new CChatDlg(this);
	dlgChatDlg->Create(IDD_CHATDLG);
	GetMyNode()->m_hChatWnd = dlgChatDlg->m_hWnd;
	dlgChatDlg->m_pUserNode = GetMyNode();

	dlgChatDlg->GetDlgItem(IDC_ID)->SetWindowText("10000");
	dlgChatDlg->GetDlgItem(IDC_NAME)->SetWindowText("系统公告");
	dlgChatDlg->GetDlgItem(IDC_FILETRANSFER)->EnableWindow(false);
	dlgChatDlg->ShowWindow(true);
}


/////////////////////////////////////
//打开个人空间前的确认窗口
void CMainDlg::OnBtnPersonalplace() 
{
	// TODO: Add your control notification handler code here
	if(m_hConfirmWnd)
	{
		::SetActiveWindow(m_hConfirmWnd);
		return;
	}
	if(m_hPsPlaceWnd)
	{
		::SetActiveWindow(m_hConfirmWnd);
		return;
	}

	CCnfmUserDlg* pCnfmUserDlg = new CCnfmUserDlg(this);
	pCnfmUserDlg->Create(IDD_CNFMUSERDLG);
	pCnfmUserDlg->GetDlgItem(IDC_EDIT_CONFIRMID)->SetFocus();
	m_hConfirmWnd = pCnfmUserDlg->m_hWnd;
	pCnfmUserDlg->ShowWindow(true);
}


/////////////////////////////////////////////////////////////////////////////
//个人空间窗口
void CMainDlg::PersonalPlace()
{
	CFileShareDlg* pPsPlaceDlg = new CFileShareDlg(this);
	pPsPlaceDlg->Create(IDD_FILESHARE);
	m_hPsPlaceWnd = pPsPlaceDlg->m_hWnd;
	CString strCaption = "个人空间 [" + m_pMyInfoNode->m_strUsrId + "]";
	CString stcMsg1 = "★您可以从服务器取回您保存的个人资料";
	CString stcMsg6 = "★您可以将您的个人资料上传到服务器为您保存";
	pPsPlaceDlg->GetDlgItem(IDC_STC1)->SetWindowText(stcMsg1);
	pPsPlaceDlg->GetDlgItem(IDC_STC6)->SetWindowText(stcMsg6);
	pPsPlaceDlg->SetWindowText(strCaption);
	pPsPlaceDlg->SetPersonalStatus(true);
	pPsPlaceDlg->Filesharebrush();
	Sleep(100);
	pPsPlaceDlg->ShowWindow(true);
}


/////////////////////////////////////////////////////////////////////////////
//根据用户id创建相应的聊天窗口
CWnd* CMainDlg::CreateChatWindow(CString strUserId)
{
	tagClientInfo* pTemp = GetUserNode(strUserId);
	
	if(pTemp->m_nIndexInList != -1)
	{
		KillTimer(pTemp->m_nIndexInList);
		m_lstUserList.SetItemText(pTemp->m_nIndexInList, 0, 
								m_strListRowValue[pTemp->m_nIndexInList]);
		m_strListRowValue[pTemp->m_nIndexInList] = "";
		pTemp->m_nIndexInList = -1;
	}

	if(pTemp->m_hChatWnd)
	{
		::SetActiveWindow(pTemp->m_hChatWnd);
		return pTemp->m_pChatWnd;
	}

	CChatDlg* dlgChatDlg = new CChatDlg(this);
	dlgChatDlg->Create(IDD_CHATDLG);
	pTemp->m_hChatWnd = dlgChatDlg->m_hWnd;
	pTemp->m_pChatWnd = dlgChatDlg;
	dlgChatDlg->m_pUserNode = pTemp;
	//////////////////////
	//在这里设置窗体要显示的用户资料
	dlgChatDlg->GetDlgItem(IDC_ID)->SetWindowText(pTemp->m_strUsrId);
	dlgChatDlg->GetDlgItem(IDC_NAME)->SetWindowText(pTemp->m_strUsrName);
	dlgChatDlg->GetDlgItem(IDC_IP)->SetWindowText(pTemp->m_strIp);
	dlgChatDlg->GetDlgItem(IDC_EDIT_CHATLOG)->SetWindowText(pTemp->m_strChatlog);
	CString strFace = pTemp->m_strFace;
	UINT nFaceID[] = {IDI_FACE0, IDI_FACE1, IDI_FACE2, IDI_FACE3, IDI_FACE4};
	dlgChatDlg->m_urFace.SetIcon(AfxGetApp()->LoadIcon(nFaceID[atoi(strFace)]));

	return dlgChatDlg;
}

/////////////////////////////////////////////////////////////////////////////
//P2P文件传输网络事件回调函数
void CMainDlg::OnFileEvent(WPARAM wParam, LPARAM lParam)
{
	m_pLoadDlg->m_FT.OnAccept(this);
}

void CMainDlg::OnBtnFiletronfer() 
{
	// 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 strUId;
	CString strInfo;
	strInfo = m_lstUserList.GetItemText(nIndex,0);
	strUId = strInfo.Mid(strInfo.ReverseFind('[') + 1);
	strUId = strUId.Left(strUId.GetLength() - 1);
	
	//创建聊天窗口
	CChatDlg* pDlg = (CChatDlg*)CreateChatWindow(strUId);
	pDlg->FileTransfer();
}


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


void CMainDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_ttToolTip.AddToolTip("发系统公告", GetDlgItem(IDC_BTN_SENDCALL));
	m_ttToolTip.AddToolTip("文件传输", GetDlgItem(IDC_BTN_FILETRONFER));
	m_ttToolTip.AddToolTip("文件共享", GetDlgItem(IDC_BTN_FILESHARE));
	m_ttToolTip.AddToolTip("个人文件存储", GetDlgItem(IDC_BTN_PERSONALPLACE));
	m_ttToolTip.AddToolTip("个人资料", GetDlgItem(IDC_BTN_MINEINFO));

	CDialog::OnMouseMove(nFlags, point);
}


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


/////////////////////////////////////////////////////////////////////////////
//根据用户id查找用户在用户列表中的位置
int CMainDlg::GetIndexInList(CString strUserId)
{
	for(int i = 0; i < m_lstUserList.GetItemCount(); i++)
	{
		CString strUsrIf = m_lstUserList.GetItemText(i, 0);
		if(strUsrIf.IsEmpty())
		{
			strUsrIf = m_strListRowValue[i];
		}
		strUsrIf = strUsrIf.Mid(strUsrIf.ReverseFind('[') + 1);
		
		if(strUsrIf.Left(strUsrIf.GetLength() - 1) == strUserId)
		{
			return i;
		}
	}
	return -1;
}

⌨️ 快捷键说明

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