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

📄 chatdlg.cpp

📁 基于TCP的局域网多用户通信、文件传送程序详解
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	nid.hWnd = m_hWnd;
	nid.uID = ID_SYSTEMTRAY;
	nid.uFlags = 0;
	::Shell_NotifyIcon(NIM_DELETE, &nid);
	KillTimer(0);
////////////////////////////////////////////////////////////////////////
	FILE *pFile;
	if ((pFile = fopen(fileName, "w")) != NULL)
	{
		fprintf(pFile, "%s", m_strRecv);
		fclose(pFile);
	}
	
	return CDialog::DestroyWindow();
}

BOOL CChatDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	HINSTANCE hInst = AfxGetResourceHandle();
	LPCTSTR lpID = MAKEINTRESOURCE(IDR_ACCELERATOR1);
	HACCEL d_hAccel = LoadAccelerators(hInst, lpID);
	if (d_hAccel == NULL)
	{
		return false;
	}
	return ::TranslateAccelerator(m_hWnd, d_hAccel, pMsg);
	
//	return CDialog::PreTranslateMessage(pMsg);
}

void CChatDlg::ToggleSize()
{
	CRect rect;
	CString str;
	if (m_bToggleSize)
	{
		str = "<<关闭记录";
		rect = m_rectFull;
	}
	else
	{
		str = "聊天记录>>";
		rect = m_rectHalf;
	}
	SetWindowPos(NULL, 0, 0, rect.Width(),
		rect.Height(), SWP_NOMOVE);
	m_more.SetWindowText(str);
	m_bToggleSize = !m_bToggleSize;
}

BOOL CChatDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	InitNet();
///////////////////////对话框大小控制//////////////////////////
	GetWindowRect(&m_rectFull);
	m_rectHalf = m_rectFull;
	CRect rect;
	m_more.GetWindowRect(&rect);
	m_rectHalf.bottom = rect.bottom + 5;
	m_bToggleSize = false;
	ToggleSize();
///////////////////////读用户信息文件/////////////////////////
	CString str;
	int j = 0;
	char *pos = new char;
	char *pos1 = new char;
	char temp[40];
	char temp1[40];
	fileName.Format(".\\data\\%s.db", m_strUserName);
	m_file.Open(fileName, CFile::modeCreate | CFile::modeNoTruncate
		| CFile::modeReadWrite | CFile::shareDenyWrite);
	m_file.SeekToBegin();
	while (m_file.ReadString(str))
	{
		str.TrimRight();
		str.TrimLeft();
		pos = strchr(str, ',');
		int len_str = strlen(str);
		int len_pos = strlen(pos);
		int len = len_str - len_pos;
		pos += 1;
		pos1 = pos;
		for (int i=0; i<len; i++)
		{
			temp[i] = str[i];
		}
		temp[len] = '\0';
		if (strcmp(temp, m_strUserName) != 0)
		{
			m_petname.InsertString(m_petname.GetCount(), temp);
		}
		len_str = strlen(pos1);
		pos = strchr(pos1, ',');
		len_pos = strlen(pos);
		len = len_str - len_pos;
		pos += 1;
		for (i=0; i<len; i++)
		{
			temp1[i] = pos1[i];
		}
		temp1[len] = '\0';
		pet[j] = atoi(temp1);
		if (strcmp(temp, m_strUserName) == 0)
		{
			res = pet[j];
		}
		map.SetAt(temp, pos);
		j++;
	}
	m_petname.SetCurSel(0);
	CString aa;
	m_petname.GetLBText(0, aa);
	m_strCity = map[aa];
	m_file.Close();

///////////////系统托盘/////////////////////////////////////////////////
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof (NOTIFYICONDATA);
	nid.hWnd = m_hWnd;
	nid.uID = ID_SYSTEMTRAY;
	nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	nid.uCallbackMessage = WM_SYSTEMTRAY;
	nid.hIcon = AfxGetApp()->LoadIcon(res);
	strcpy(nid.szTip, ("闻博QQ:" + m_strUserName));
	::Shell_NotifyIcon(NIM_ADD, &nid);
	HICON hIcon = AfxGetApp()->LoadIcon(res);
	m_pic.SetIcon(hIcon);
	m_nNetSel = res;
	resOwn.Format("%d", res);
	flag = false;
/////////////////////////////////界面/////////////////////////////////
	GetDlgItem(IDC_BTN_REPLY)->EnableWindow(false);
	recvWnd = m_hWnd;
	CString title;
	title.Format("%s%s", m_strUserName, "的好友");
	m_friend.SetWindowText(title);
	m_bReply = false;
///////////////////////////////读文档////////////////////////////////
	fileName.Format(".\\data\\%s.lwb", m_strUserName);
	m_file.Open(fileName, CFile::modeCreate | CFile::modeNoTruncate
		| CFile::modeReadWrite | CFile::shareDenyWrite);
	m_file.SeekToBegin();
	while (m_file.ReadString(str))
	{
		str.TrimRight();
		str.TrimLeft();
		m_strRecv += str;
		m_strRecv += "\r\n";
	}
	m_file.Close();

	UpdateData(false);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

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

void CChatDlg::OnBtnSend() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_BTN_REPLY)->EnableWindow(false);
	UpdateData(true);
////////////////////////////将发送信息附加上时间头像ID等等//////////////////
	int count = m_petname.GetCurSel();
	m_petname.GetLBText(count, m_strNetUser);
	CString date;
	CTime time;
	time = CTime::GetCurrentTime();
	date = time.Format("%A,%B,%d,%Y,%H:%M:%S");
	CString temp;
	if (m_message.GetModify())
	{
		temp += "(";
		temp += date;
		temp += ")";
		temp += "\t";
		temp += m_strUserName;
		temp += "->";
		temp += m_strNetUser;
		temp += "\r\n";
		char str[200];
		int len = 0;
		int count = m_message.GetLineCount();
		if (count <= 100)
		{
			for (int i=0; i<count; i++)
			{
				len = m_message.LineLength(m_message.LineIndex(i));
				m_message.GetLine(i, str);
				str[len] = '\0';
				temp += str;
				temp += "\r\n";
			}
		}
		strSendData = m_strNetUser;
		strSendData += ":";
		strSendData += resOwn;
		strSendData += "~";
		strSendData += temp;
		m_strRecv += temp;
		numsnd = send(destSocket, strSendData, strSendData.GetLength(), 0);
		if (numsnd == SOCKET_ERROR || numsnd == 0)
		{
			MessageBox("传输失败" + GetError(GetLastError()), "警告", MB_OK);
		}
		else
		{
			UpdateData(false);
		}
	}
	m_message.SetWindowText("");
	::ShowWindow(m_hWnd, SW_HIDE);
	m_bReply = false;
}

void CChatDlg::OnBtnReply() 
{
	// TODO: Add your control notification handler code here
	KillTimer(0);
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(NOTIFYICONDATA);
	nid.hWnd = m_hWnd;
	nid.uID = ID_SYSTEMTRAY;
	nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
	nid.uCallbackMessage = WM_SYSTEMTRAY;
	nid.hIcon = AfxGetApp()->LoadIcon(res);
	strcpy(nid.szTip, ("闻博QQ" + m_strUserName));
	::Shell_NotifyIcon(NIM_MODIFY, &nid);
	m_pic.SetIcon(AfxGetApp()->LoadIcon(res));

	GetDlgItem(IDC_BTN_REPLY)->EnableWindow(false);
	GetDlgItem(IDC_BTN_SEND)->EnableWindow(true);
	m_message.SetWindowText("");
	m_message.SetReadOnly(false);
	m_message.SetFocus();
	m_bReply = true;
}

void CChatDlg::OnSelchangeComboPetname() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CString str;
	int p = m_petname.GetCurSel();
	m_petname.GetLBText(p, str);
	m_strCity = map[str];
	UpdateData(false);
}

void CChatDlg::OnAbout() 
{
	// TODO: Add your command handler code here
	CAboutAuth dlg;
	dlg.DoModal();
}

void CChatDlg::OnShow() 
{
	// TODO: Add your command handler code here
	::ShowWindow(m_hWnd, SW_SHOW);
}

void CChatDlg::RecvData(WPARAM wParam, LPARAM lParam)
{
//////////////////////////获得发送者头像资源ID/////////////////
	char *pRecvData = (LPSTR)wParam;
	char source[6];
	int lenData = (int)lParam;
	char *pos = strchr(pRecvData, '~');
	int lenPos = strlen(pos);
	pos += 1;
	int len = lenData - lenPos;
	for (int i=0; i<len; i++)
	{
		source[i] = pRecvData[i];
	}
	source[len] = '\0';
	m_nNetSel = atoi(source);
//////////////////////////获得发送者用户名////////////////////
	char *posName = strchr(pos, '\t');
	posName++;
	char *posNameEnd = strchr(posName, '-');
	for (i=0; i<(posNameEnd-posName); i++)
	{
		petname[i] = posName[i];
	}
	petname[i] = '\0';
	m_petname.SelectString(-1, petname);
	m_strCity = map[petname];
/////////////////////////////////////////////////////////////
	m_strRecv += pos;	//将收到的信息加入聊天纪录
	if (!m_bReply)
	{
		m_message.SetWindowText(pos);
		m_message.SetReadOnly(true);
		GetDlgItem(IDC_BTN_REPLY)->EnableWindow(true);
		GetDlgItem(IDC_BTN_SEND)->EnableWindow(false);
	}
//	MessageBox(pos);
	SetTimer(0, 200, NULL);
	UpdateData(false);
}

void CChatDlg::OnClose()
{
	CDialog::OnCancel();
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof (NOTIFYICONDATA);
	nid.hWnd = m_hWnd;
	nid.uID = ID_SYSTEMTRAY;
	nid.uFlags = 0;
	::Shell_NotifyIcon(NIM_DELETE, &nid);
	KillTimer(0);
}

void CChatDlg::OnBtnCarryFile() 
{
	// TODO: Add your control notification handler code here
//	GetDlgItem(IDC_BTN_CARRY_FILE)->EnableWindow(false);
	UpdateData(true);
	int count = m_petname.GetCurSel();
	m_petname.GetLBText(count, m_strNetUser);	//获取文件接收者的用户名
	CString str = "给";
	str += m_strNetUser;
	str += "发送文件,\n等待对方回应!";
	MessageBox(str, "闻博QQ", MB_OK);
	ShowWindow(SW_HIDE);
	str = m_strNetUser + ":" + "SendFile!";
/////////////////////// 获得主机名.//////////////////////////////////////
	char hostname[256];
	int res = gethostname(hostname, sizeof(hostname));
	if (res != 0)
	{
		printf("Error: %u\n", WSAGetLastError());
		return;
	}
//////////////////// 根据主机名获取主机信息////////////////////////////////
	hostent* pHostent = gethostbyname(hostname);
	if (pHostent==NULL)
	{
      printf("Error: %u\n", WSAGetLastError());
      return;
	}
///////////获得IP地址//////////////////////////////////////////////////////
	LPSTR lPAddr = pHostent->h_addr_list[0];
	CString IPAddr;
	if (lPAddr)
	{
		struct in_addr inAddr;
		memmove(&inAddr, lPAddr, 4);
		IPAddr = inet_ntoa(inAddr);
	}
///发送的信息为:接收方 + ":" + "SendFile!" + 发送方 + "~" + 发送方IPAddr////
	str += m_strUserName;
	str += "~";
	str += IPAddr;

	send(destSocket, str, str.GetLength(), 0);
	LPTSTR p = m_strNetUser.GetBuffer(20);
	pSendFile = AfxBeginThread(sendFile, (LPVOID)p);	//启动发送文件线程
}

⌨️ 快捷键说明

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