📄 serverdlg.cpp
字号:
}
void CServerDlg::OnClose(WPARAM wParam,LPARAM lParam)
{
int i;
for(i=0;i<MAX1&&SocketAccept[i]!=wParam;i++){}
if(i==MAX1) return;
sendinfo info;
info.type=3;//people exit
CTime t=CTime::GetCurrentTime();
CString strTime="[%y-%m-%d %H:%M:%S]",s;
strTime=t.Format(strTime);
s=uinfo[i].username;
memcpy(info.name,s,s.GetLength()+1);
CString m;
m=strTime+"系统消息:\r\n 用户"+uinfo[i].username+"退出聊天室";
m_strMsg=m_strMsg+"\r\n"+strTime+"用户"+uinfo[i].username+"(IP:"+uinfo[i].userip+")退出服务器";
UpdateData(FALSE);
people.Format("%d",atoi(people)-1);
GetDlgItem(IDC_STATIC2)->SetWindowText(people);
CEdit *edit=new CEdit;
edit=(CEdit *)GetDlgItem(IDC_EDIT4);
edit->LineScroll(edit->GetLineCount(),0);
memcpy(info.msg,m,m.GetLength()+1);
for(int j=0;j<MAX&&SocketAccept[j]!=INVALID_SOCKET;j++)
{
if(i!=j)
send(SocketAccept[j],(char*)&info,sizeof(info),0);
}
uinfo[i].userip="";
uinfo[i].username="";
closesocket(SocketAccept[i]);
SocketAccept[i]=INVALID_SOCKET;
}
void CServerDlg::OnReceive(WPARAM wParam,LPARAM lParam)
{
int i;
for(i=0;i<MAX1&&SocketAccept[i]!=wParam;i++){}
if(i==MAX1) return;
int read;
sendinfo info;
read=recv(SocketAccept[i],(char *)&info,sizeof(info),0);
switch(info.type)
{
case 1:// to all
int j;
for(j=0;j<MAX&&SocketAccept[j]!=INVALID_SOCKET;j++)
{
if(i!=j)
send(SocketAccept[j],(char *)&info,sizeof(info),0);
}
break;
case 2://to somebody
case 6://接受文件
case 7:
for(j=0;j<MAX&&uinfo[j].username!=info.name;j++){}//&&uinfo[j].userip==
send(SocketAccept[j],(char *)&info,sizeof(info),0);
break;
case 0://new people in
for(j=0;j<MAX&&uinfo[j].username!=info.name;j++)
{
}
if(uinfo[j].username==info.name)
{
sendinfo err;
err.type=4;//异常
CString m;
m="该用户昵称已经存在,请更换";
memcpy(err.msg,m,m.GetLength()+1);
send(SocketAccept[i],(char*)&err,sizeof(err),0);
uinfo[i].userip="";
closesocket(SocketAccept[i]);
SocketAccept[i]=INVALID_SOCKET;
}
else
{
CTime t=CTime::GetCurrentTime();
CString strTime="[%y-%m-%d %H:%M:%S]",s;
strTime=t.Format(strTime);
s=info.name;
uinfo[i].username=info.name;
s="\r\n"+strTime+"系统消息:\r\n "+s+"进入聊天室";
m_strMsg=m_strMsg+"\r\n"+strTime+"用户"+uinfo[i].username+"(IP:"+uinfo[i].userip+")进入服务器";
UpdateData(FALSE);
CEdit *edit=new CEdit;
edit=(CEdit *)GetDlgItem(IDC_EDIT4);
edit->LineScroll(edit->GetLineCount(),0);
memcpy(info.msg,s,s.GetLength()+1);//MessageBox("a");
for(j=0;j<MAX;j++)
{
if(SocketAccept[j]!=INVALID_SOCKET)
send(SocketAccept[j],(char*)&info,sizeof(info),0);
}
for(j=0;j<MAX;j++)
{
if(i!=j&&uinfo[j].username!="")
{
sendinfo namelist;
CString name,ms;
namelist.type=0;
name=uinfo[j].username;
ms="";
memcpy(namelist.msg,ms,ms.GetLength()+1);
memcpy(namelist.name,name,name.GetLength()+1);
send(SocketAccept[i],(char*)&namelist,sizeof(info),0);
}
}
}
break;
case 5://request for sending file
{
CString smsg;
smsg=info.msg;
smsg=uinfo[i].userip+"|"+smsg;
memcpy(info.msg,smsg,smsg.GetLength()+1);
for(j=0;j<MAX&&uinfo[j].username!=info.name;j++){}//&&uinfo[j].userip==
send(SocketAccept[j],(char *)&info,sizeof(info),0);
}
break;
}
}
LRESULT CServerDlg::onShowTask(WPARAM wParam,LPARAM lParam)
//wParam接收的是图标的ID,而lParam接收的是鼠标的行为
{
SetForegroundWindow();
if(wParam!=IDR_MAINFRAME)
return 1;
switch(lParam)
{
case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭”
{
LPPOINT lpoint=new tagPOINT;
::GetCursorPos(lpoint);//得到鼠标位置
CMenu menu;
menu.CreatePopupMenu();//声明一个弹出式菜单
//增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口(已
//隐藏),将程序结束。
menu.AppendMenu(MF_STRING,WM_DESTROY,"关闭");
//确定弹出式菜单的位置
menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
//资源回收
HMENU hmenu=menu.Detach();
menu.DestroyMenu();
delete lpoint;
}
break;
case WM_LBUTTONDBLCLK://双击左键的处理
{
this->ShowWindow(SW_SHOW);//简单的显示主窗口完事儿
}
break;
}
return 0;
/*
if( lParam == WM_RBUTTONDOWN )
{
CMenu* menu;
menu = new CMenu();
menu->LoadMenu(IDR_MENU1);// 菜单是要提前做好的
CMenu* pPopup=menu->GetSubMenu(0);
::SetMenuDefaultItem(pPopup->m_hMenu,0,TRUE); //设置粗体字
CPoint Point;
GetCursorPos(&Point);
pPopup->TrackPopupMenu(TPM_LEFTALIGN,Point.x,Point.y,AfxGetMainWnd(),NULL);
}
return 0;*/
}
void CServerDlg::OnButtonMin()
{
// TODO: Add your control notification handler code here
m_nid.cbSize = (DWORD)sizeof(NOTIFYICONDATA);
m_nid.hWnd = this->m_hWnd;
m_nid.uID = IDR_MAINFRAME;
m_nid.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP ;
m_nid.uCallbackMessage = WM_SHOWTASK;
//自定义的消息名称 WM_SHOWTASK 头函数中定义为WM_USER+1
m_nid.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(m_nid.szTip,"QC服务器");//当鼠标放在上面时,所显示的内容
Shell_NotifyIcon(NIM_ADD,&m_nid);//在托盘区添加图标
this->ShowWindow(SW_HIDE);
}
void CServerDlg::OnDestroy()
{
CDialog::OnDestroy();
Shell_NotifyIcon(NIM_DELETE,&m_nid);//在托盘区删除图标
// TODO: Add your message handler code here
}
void CServerDlg::OnButtonClear()
{
// TODO: Add your control notification handler code here
CClearDlg dlg;dlg.dlg=this;
if(dlg.DoModal()==IDOK)
{
for(int i=0;i<MAX&&uinfo[i].username!=dlg.name;i++)
{
}
sendinfo info;
info.type=3;
CTime t=CTime::GetCurrentTime();
CString strTime="[%y-%m-%d %H:%M:%S]",s;
strTime=t.Format(strTime);
s=uinfo[i].username;
memcpy(info.name,s,s.GetLength()+1);
CString m;
m=strTime+"系统消息:\r\n 用户"+uinfo[i].username+"被踢出聊天室";
m_strMsg=m_strMsg+"\r\n"+strTime+"用户"+uinfo[i].username+"(IP:"+uinfo[i].userip+")被管理员踢出出服务器";
UpdateData(FALSE);
CEdit *edit=new CEdit;
edit=(CEdit *)GetDlgItem(IDC_EDIT4);
edit->LineScroll(edit->GetLineCount(),0);
memcpy(info.msg,m,m.GetLength()+1);
for(int j=0;j<MAX&&SocketAccept[j]!=INVALID_SOCKET;j++)
{
send(SocketAccept[j],(char*)&info,sizeof(info),0);
}
uinfo[i].userip="";
uinfo[i].username="";
closesocket(SocketAccept[i]);
SocketAccept[i]=INVALID_SOCKET;
people.Format("%d",atoi(people)-1);
GetDlgItem(IDC_STATIC2)->SetWindowText(people);
}
}
HBRUSH CServerDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
return hbr;
}
void CServerDlg::HostInfo()
{
char wsHostName[128]; // 存储主机名
if (gethostname(wsHostName,128)==0) //正确的情况
{
hostent* pHost;
pHost = gethostbyname(wsHostName); //pHost返回的是指向主机的列表
LPCSTR psz = inet_ntoa(*(struct in_addr *)pHost->h_addr_list[0]);//得到指向ip的psz变量
CString list;
list=psz;
list=" IP地址:"+list;
list=wsHostName+list;
list="主机名:"+list;
GetDlgItem(IDC_STATIC1)->SetWindowText(list);
people='0';
GetDlgItem(IDC_STATIC2)->SetWindowText(people);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -