www.pudn.com > 多功能聊天程序.rar > MainBordDlg.cpp
// MainBordDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MainBord.h"
#include "MainBordDlg.h"
#include "Message.h"
#include "AddFreind.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMainBordDlg dialog
CMainBordDlg::CMainBordDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMainBordDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMainBordDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMainBordDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMainBordDlg)
DDX_Control(pDX, IDC_FriendLIST, m_FriendList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMainBordDlg, CDialog)
//{{AFX_MSG_MAP(CMainBordDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_AddBTN, OnAddBTN)
ON_BN_CLICKED(IDC_StartBTN, OnStartBTN)
ON_LBN_DBLCLK(IDC_FriendLIST, OnDblclkFriendLIST)
ON_BN_CLICKED(IDC_PhoneBTN, OnPhoneBTN)
ON_BN_CLICKED(IDC_SendFileBTN, OnSendFileBTN)
ON_BN_CLICKED(IDC_HIDEBTN, OnHidebtn)
ON_MESSAGE(TESTWM_NOTIFYICON,OnTestNotifyIcon)
ON_COMMAND(ID_SUBMENU1,OnSubmenu1)
ON_COMMAND(ID_SUBMENU2,OnSubmenu2)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CLIENT_READCLOSE,OnRead)
ON_MESSAGE(WM_CLIENT_ACCEPT,OnAccept)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMainBordDlg message handlers
BOOL CMainBordDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_FriendList.SetItemHeight( 0, 18 );
start=NULL;
last=NULL;
// TODO: Add extra initialization here
GetIP_Name();
strcpy(lpszTipBefore,"QQ "+IPName);
(CEdit *)GetDlgItem(IDC_AddBTN)->EnableWindow(FALSE);
(CEdit *)GetDlgItem(IDC_MSGBTN)->EnableWindow(FALSE);
(CEdit *)GetDlgItem(IDC_PhoneBTN)->EnableWindow(FALSE);
(CEdit *)GetDlgItem(IDC_SendFileBTN)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMainBordDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMainBordDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMainBordDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMainBordDlg::OnAddBTN()
{
// TODO: Add your control notification handler code here
AddFreind mydlg;
if(mydlg.DoModal() == IDOK)
{
if(mydlg.ipstr.GetLength()==0)
{
MessageBox("您没有输入IP地址!!!","错误信息",NULL);
return;
}
ip=mydlg.ipstr;
}
else return;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,1),&wsaData)) //调用Windows Sockets DLL
{
WSACleanup();
return;
}
tempSocket=socket(PF_INET,SOCK_STREAM,0); //创建服务器端Socket,类型为SOCK_STREAM,面向连接的通信
if(tempSocket == INVALID_SOCKET)
{
return;
}
DWORD dwip;
if((dwip=inet_addr(ip))==INADDR_NONE)
{
MessageBox("IP赋值错误","错误信息");
return;
}
tempSocketAddr.sin_family=AF_INET; //使用TCP/IP协议
tempSocketAddr.sin_port = htons(8001);
tempSocketAddr.sin_addr.S_un.S_addr = dwip;
if(connect(tempSocket,(LPSOCKADDR)&tempSocketAddr,sizeof(tempSocketAddr)) == SOCKET_ERROR)
{
MessageBox("对方不在线","添加失败",NULL);
return;
}
else
{
msg.sign=6;
strcpy(msg.msg,IPName);
}
if(send(tempSocket,(char *)&msg,sizeof(msg),0)==SOCKET_ERROR)
{
MessageBox("发送消息失败","错误信息",MB_OK);
return;
}
}
void CMainBordDlg::OnStartBTN()
{
// TODO: Add your control notification handler code here
(CEdit *)GetDlgItem(IDC_AddBTN)->EnableWindow(TRUE);
(CEdit *)GetDlgItem(IDC_MSGBTN)->EnableWindow(TRUE);
(CEdit *)GetDlgItem(IDC_PhoneBTN)->EnableWindow(TRUE);
(CEdit *)GetDlgItem(IDC_SendFileBTN)->EnableWindow(TRUE);
WSADATA wsaData;
int iErrorCode;
if (WSAStartup(MAKEWORD(2,1),&wsaData)) //调用Windows Sockets DLL
{
WSACleanup();
return;
}
//m_List.InsertString(0,"服务器开始创建SOCKET。");
ServerSocket=socket(PF_INET,SOCK_STREAM,0); //创建服务器端Socket,类型为SOCK_STREAM,面向连接的通信
if(ServerSocket == INVALID_SOCKET)
{
//m_List.InsertString(0,"无法创建服务器socket!");
return;
}
m_sockServerAddr.sin_family = AF_INET;
m_sockServerAddr.sin_addr.s_addr = INADDR_ANY; //向所有的IP地址发送消息
m_sockServerAddr.sin_port = htons(8001);
if (bind(ServerSocket,(LPSOCKADDR)&m_sockServerAddr,sizeof(m_sockServerAddr)) == SOCKET_ERROR) //与选定的端口绑定
{
//m_List.InsertString(0,"无法绑定服务器。");
return;
}
iErrorCode=WSAAsyncSelect(ServerSocket,m_hWnd,WM_CLIENT_ACCEPT,FD_ACCEPT);
// 产生相应传递给窗口的消息为WM_SERVER_ACCEPT ,这是自定义消息
if (iErrorCode == SOCKET_ERROR)
{
return;
}
if (listen(ServerSocket,1) == SOCKET_ERROR) //开始监听客户连接请求
{
//m_List.InsertString(0,"服务器监听失败!");
return;
}
IsTrue = TRUE;
//m_List.AddString("服务器绑定监听成功。");
return;
}
void CMainBordDlg::OnDblclkFriendLIST()
{
// TODO: Add your control notification handler code here
if(IsTrue!=TRUE)return;//未登录
int index=m_FriendList.GetCurSel();
if(index!=LB_ERR)
{
CString strname;
m_FriendList.GetText(index,strname);
FriendList *s;
s=start;
while(s!=NULL&&strcmp(s->name,strname)!=0)
{
s=s->next;
}
if(s==NULL){MessageBox("没找到!");return;}
CString ipstr;
ipstr.Format("%s",s->IP);
CString a;
a="0#"+ipstr+"#"+strname+"#";
CFile myfile;
myfile.Open("msg.txt",CFile::modeCreate | CFile::modeWrite);
myfile.Write( a, a.GetLength());
myfile.Close();
//MessageBox(a);
WinExec("Talk.exe",SW_SHOW);
}
}
LRESULT CMainBordDlg::OnRead(WPARAM wParam, LPARAM lParam)
{
if(!IsTrue)
{
//m_List.InsertString(0,"有数据到达,但是没有工作站连接。");
}
CString str;
switch (WSAGETSELECTEVENT(lParam))
{
case FD_READ:
if(recv(Client,(char *)&msg,sizeof(msg),0) == SOCKET_ERROR)
{
// m_List.InsertString(0,"接收数据发生错误。");
return 0;
}
SelectWay();
//m_History_List.InsertString(0,str);
//MessageBox(str);
break;
case FD_CLOSE:
str = _T("工作站退出。");
//m_List.InsertString(0,str);
closesocket(Client);
//IsTrue = FALSE;
break;
}
return 0L;
}
LRESULT CMainBordDlg::OnAccept(WPARAM wParam, LPARAM lParam)
{
if (WSAGETSELECTERROR(lParam))
{
//m_List.InsertString(0,"Error detected on entry into OnServerAccept.");
return 0L;
}
if(WSAGETSELECTEVENT(lParam) == FD_ACCEPT)//如果
{
Client = accept(ServerSocket,(LPSOCKADDR)&m_sockServerAddr,0);
if (Client == INVALID_SOCKET)
{
//m_List.InsertString(0,"Server socket failed to accept connection.");
return 0L;
}
//Clinet
WSAAsyncSelect(Client,m_hWnd,WM_CLIENT_READCLOSE,FD_READ|FD_CLOSE);
IsTrue = TRUE;
}
//m_List.InsertString(0,"有客户机连接上了服务器。");
return 0L;
}
void CMainBordDlg::OnPhoneBTN()
{
// TODO: Add your control notification handler code here
if(IsTrue!=TRUE)return;//未登录
int index=m_FriendList.GetCurSel();
if(index!=LB_ERR)
{
CString strname;
m_FriendList.GetText(index,strname);
FriendList *s;
s=start;
while(s!=NULL&&strcmp(s->name,strname)!=0)
{
s=s->next;
}
if(s==NULL){MessageBox("没找到!");return;}
CString ipstr;
msg.sign=2;
ipstr.Format("%s",s->IP);
ip=ipstr;
SystemMessage=IPName+"#"+HostName+"#";
// MessageBox("here!!!");
SendMsg();
}
}
void CMainBordDlg::OnSendFileBTN()
{
// TODO: Add your control notification handler code here
int index=m_FriendList.GetCurSel();
if(index!=LB_ERR)
{//先发送对方是否接收
CString strname;
m_FriendList.GetText(index,strname);
FriendList *s;
s=start;
while(s!=NULL&&strcmp(s->name,strname)!=0)
{
s=s->next;
}
if(s==NULL){MessageBox("没找到!");return;}
CString ipstr;
ipstr.Format("%s",s->IP);
ip=ipstr;//目标IP
SystemMessage=IPName+"#"+HostName+"#";
msg.sign=4;
SendMsg();
}
}
void CMainBordDlg::SelectWay()
{
FriendList *newlist=new struct FriendList;
CString strIP,strName,temp;//添加新成员
CString message;
int i,j=0;
i=msg.sign;
message.Format("%d#%s",msg.sign,msg.msg);
// MessageBox("here!!!");
// MessageBox(message);
CFile myfile;
switch(i)
{
case 1://别人跟你聊天
myfile.Open("msg.txt",CFile::modeCreate | CFile::modeWrite);
myfile.Write(message, message.GetLength());
myfile.Close();
WinExec("Talk.exe",SW_SHOW);break;
case 2:
j=0;
temp="";
ip="";
while(msg.msg[j]!='#'){ip+=msg.msg[j];j++;}j++;
// MessageBox(ip);
while(msg.msg[j]!='#'){temp+=msg.msg[j];j++;}
if((MessageBox(temp+"请求网络聊天","请求信息",IDOK))!=1)
{
return;
}
myfile.Open("voice.txt",CFile::modeCreate | CFile::modeWrite);
myfile.Write( message, message.GetLength());
myfile.Close();
SystemMessage=IPName+"#"+HostName+"#";
msg.sign=3;
SendMsg();WinExec("Speak.exe",SW_SHOW);
break;
case 3://别人接受聊天请求
message.Format("3#%s",msg.msg);
myfile.Open("voice.txt",CFile::modeCreate | CFile::modeWrite);
myfile.Write( message, message.GetLength());
myfile.Close();
//MessageBox("here!!");
WinExec("Speak.exe",SW_SHOW);break;
case 4: //别人问你是否同意接收传送文件
j=0;
temp="";
ip="";
while(msg.msg[j]!='#'){ip+=msg.msg[j];j++;}j++;
// MessageBox(ip);
while(msg.msg[j]!='#'){temp+=msg.msg[j];j++;}
if((MessageBox(temp+"向你传送文件,你是否同意接收?","请求信息",IDOK))!=1)
{
return;
}
//同意接收,发送反馈消息
message.Format("5#%s",msg.msg);//变成被动接收文件
myfile.Open("file.txt",CFile::modeCreate | CFile::modeWrite);
myfile.Write( message, message.GetLength());
myfile.Close();
SystemMessage=IPName+"#"+HostName+"#";
msg.sign=5;
SendMsg();
WinExec("FileTransfer.exe",SW_SHOW);
break;
case 5: message.Format("4#%s",msg.msg);//别人同意接收文件,主动传送文件
myfile.Open("file.txt",CFile::modeCreate | CFile::modeWrite);
myfile.Write( message, message.GetLength());
myfile.Close();
WinExec("FileTransfer.exe",SW_SHOW);break;
break;//接收文件
case 6:message.Format("%s",msg.msg);
// MessageBox(message);
if((MessageBox(message+"请求你通过身份验证","请求信息",IDOK))!=1)
{
//MessageBox("here!!!");
return;
}
ip=message;//同意加为好友
SystemMessage=IPName+"#"+HostName+"#";
msg.sign=7;
SendMsg();
break;
case 7://MessageBox(message);
message.Format("%s",msg.msg);
// MessageBox(message);
strIP="";
strName="";
j=0;
while(msg.msg[j]!='#')
{
strIP+=msg.msg[j];
j++;
}
j++;
while(msg.msg[j]!='#')
{
strName+=msg.msg[j];
j++;
}
for(j=0;jIP,strIP);
strcpy(newlist->name,strName);
newlist->next=NULL;
if(start==NULL){start=newlist;last=newlist;}
else{
last->next=newlist;
last=newlist;
}
m_FriendList.AddString(strName);
MessageBox(strName+"通过你的身份验证!!!","验证信息",NULL);
UpdateData(TRUE);break;
default:break;
}
}
void CMainBordDlg::GetIP_Name()//获得IP和主机名
{
WORD wVersionRequested;
WSADATA wsaData;
char hostname[128];
CString strip;
PHOSTENT hostinfo;
wVersionRequested=MAKEWORD(2,0);
if(WSAStartup(wVersionRequested,&wsaData)==0)
{
if(gethostname(hostname,sizeof(hostname))==0)
{
if((hostinfo=gethostbyname(hostname))!=NULL)
{
strip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup();
}
IPName=strip;
HostName.Format("%s",hostname);
}
void CMainBordDlg::SendMsg()//接受或发送系统请求信息
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,1),&wsaData)) //调用Windows Sockets DLL
{
WSACleanup();
return;
}
tempSocket=socket(PF_INET,SOCK_STREAM,0); //创建服务器端Socket,类型为SOCK_STREAM,面向连接的通信
if(tempSocket == INVALID_SOCKET)
{
return;
}
DWORD dwip;
if((dwip=inet_addr(ip))==INADDR_NONE)
{
MessageBox("IP赋值错误","错误信息");
return;
}
tempSocketAddr.sin_family=AF_INET; //使用TCP/IP协议
tempSocketAddr.sin_port = htons(8001);
tempSocketAddr.sin_addr.S_un.S_addr = dwip;
if(connect(tempSocket,(LPSOCKADDR)&tempSocketAddr,sizeof(tempSocketAddr)) == SOCKET_ERROR)
{
MessageBox("对方不在线","失败信息",NULL);
return;
}
strcpy(msg.msg,SystemMessage);
if(send(tempSocket,(char *)&msg,sizeof(msg),0)==SOCKET_ERROR)
{
MessageBox("发送消息失败","错误信息",MB_OK);
return;
}
}
void CMainBordDlg::OnHidebtn()
{
// TODO: Add your control notification handler code here
TrayAddIcon();
ShowWindow(SW_HIDE);
}
LRESULT CMainBordDlg::OnTestNotifyIcon(WPARAM wParam, LPARAM lParam)
{
switch(lParam)
{
case WM_LBUTTONDBLCLK:
if(wParam==DEMOICON)
{
ShowWindow(SW_SHOW);
}
break;
case WM_RBUTTONDOWN:
if(wParam==DEMOICON)
{
CMenu *pm1,m2;
CPoint pp(LOWORD(lParam),HIWORD(lParam));
m2.LoadMenu(IDR_MENU1);
pm1=m2.GetSubMenu(0);
ClientToScreen(&pp);
pm1->TrackPopupMenu(TPM_RIGHTALIGN|TPM_LEFTBUTTON,
pp.x,
GetSystemMetrics(SM_CYSCREEN)-50,this);
}
break;
default:
break;
}
return 0L;
}
int CMainBordDlg::TrayAddIcon()
{
BOOL res;
NOTIFYICONDATA tnid;
HICON hicon;
hicon=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICONBEFORE));
tnid.cbSize=sizeof(NOTIFYICONDATA);
tnid.hWnd=GetSafeHwnd();
tnid.cbSize=sizeof(NOTIFYICONDATA);
tnid.hWnd=GetSafeHwnd();
tnid.uID=DEMOICON;
tnid.uFlags=NIF_ICON|NIF_TIP|NIF_MESSAGE;
tnid.uCallbackMessage=TESTWM_NOTIFYICON;
tnid.hIcon=hicon;
if(lpszTipBefore)
lstrcpyn(tnid.szTip,lpszTipBefore,sizeof(tnid.szTip));
else
tnid.szTip[0]='\0';
res=Shell_NotifyIcon(NIM_ADD,&tnid);
if(hicon)
DestroyIcon(hicon);
return res;
}
void CMainBordDlg::OnSubmenu1()
{
ShowWindow(SW_SHOW);
}
void CMainBordDlg::OnSubmenu2()
{
EndDialog(TRUE);
}