📄 clientdlg.cpp
字号:
// TODO: Add your control notification handler code here
int i=m_ctrList.GetCurSel();
m_ctrList.GetText(i,m_strUser);
GetDlgItem(IDC_COMBO_USER)->SetWindowText(m_strUser);
// m_ctrUser.SetCurSel(m_ctrUser.FindString(i,m_strUser));
// m_ctrUser.SelectString(0,m_strUser);
m_ctrSend.SetFocus();
}
void CClientDlg::OnMin()
{
// TODO: Add your control notification handler code here
m_nid.cbSize = (DWORD)sizeof(NOTIFYICONDATA);
m_nid.hWnd = this->m_hWnd;
m_nid.uID =IDI_ICON1;
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));
CString title;
title="QC:"+cuser;
strcpy(m_nid.szTip,title);//当鼠标放在上面时,所显示的内容
Shell_NotifyIcon(NIM_ADD,&m_nid);//在托盘区添加图标
this->ShowWindow(SW_HIDE);
m_nid.hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
Shell_NotifyIcon(NIM_MODIFY, &m_nid);
KillTimer(1);
}
LRESULT CClientDlg::onShowTask(WPARAM wParam,LPARAM lParam)
//wParam接收的是图标的ID,而lParam接收的是鼠标的行为
{
SetForegroundWindow();
if(wParam!=IDI_ICON1)
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);//简单的显示主窗口完事儿
KillTimer(1);
m_nid.hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
Shell_NotifyIcon(NIM_MODIFY, &m_nid);
}
break;
}
return 0;
}
void CClientDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{
case 1:
{
if(m_nid.hIcon == AfxGetApp()->LoadIcon(IDI_ICON1))
m_nid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON2);
else m_nid.hIcon =AfxGetApp()->LoadIcon(IDI_ICON1);
Shell_NotifyIcon(NIM_MODIFY, &m_nid);
}
break;
case 2:
{
CString filesize;
filesize=KindChange(ByteTotal-pb);
pb=ByteTotal;
filesize="速度:"+filesize+"/s";
GetDlgItem(IDC_STATIC_SPEED)->SetWindowText(filesize);
}
break;
case 3:
{
Ishake=1;
KillTimer(3);
}
}
CDialog::OnTimer(nIDEvent);
}
void CClientDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
Shell_NotifyIcon(NIM_DELETE,&m_nid);//在托盘区删除图标
}
BOOL CClientDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
switch(pMsg->message)
{
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MOUSEMOVE:
ball.RelayEvent(pMsg);
}
return CDialog::PreTranslateMessage(pMsg);
}
void CClientDlg::OnButtonTans()
{
// TODO: Add your control notification handler code here
UpdateData(true);
/* if(m_strUser==cuser)
{
MessageBox("不能给自己发文件");
return;
}
if(m_strUser=="大家")
{
MessageBox("不能给所以人发文件");
return;
}*/
//打开文件对话框
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "所有文件 (*.*)|*.*||");
if(dlg.DoModal()==IDOK)
{
m_strFilePath=dlg.GetPathName();
m_strFileName=dlg.GetFileName();
CFile file(m_strFilePath, CFile::modeRead);
//获取文件大小
m_dwFileSize=file.GetLength();
//关闭文件
file.Close();
CString filesize;
filesize=KindChange(m_dwFileSize);
GetDlgItem(IDC_PROGRESS1)->ShowWindow(SW_SHOW);
m_ctrEnd.ShowWindow(SW_SHOW);
m_ctrEnd.SetWindowText("取消");
GetDlgItem(IDC_STATIC_RECV)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC_RECV)->SetWindowText("发送文件:");
GetDlgItem(IDC_STATIC_FILENAME)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC_FILENAME)->SetWindowText(m_strFileName);
GetDlgItem(IDC_STATIC_FILESIZE)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC_FILESIZE)->SetWindowText(filesize);
GetDlgItem(IDC_STATIC2)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC2)->SetWindowText("0%");
GetDlgItem(IDC_BUTTON_TANS)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STATIC_SPEED)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC_SPEED)->SetWindowText("速度: 0");
CString message;filesize.Format("%d",m_dwFileSize);
message=cuser+"|"+m_strFileName+"|"+filesize+"|";
SendMsg(5,m_strUser,message);
Sendto=m_strUser;
}
}
//发送数据线程
UINT SendDataThread(LPVOID lpParam)
{
CClientDlg *pDlg=(CClientDlg *)lpParam;
CFile file;
if(!file.Open(pDlg->m_strFileName,CFile::modeRead))
{
AfxMessageBox("打开文件出错!");
pDlg->GetDlgItem(IDC_BUTTON_TANS)->ShowWindow(SW_SHOW);
pDlg->Hide();
return 0;
}
CSocket sockTemp;
sockTemp.Create(2000);
sockTemp.Listen(1);
CSocket sockSend;
sockTemp.Accept(sockSend);
AfxMessageBox("成功");
pDlg->IsEnd=0;
pDlg->m_ctrProgress.SetRange(0,100);
int BufSize=SENDDATE;
int ByteNum=0,nLen=0,pos=0,pos1=0;
pDlg->ByteTotal=0;pDlg->pb=0;
CString str,str1;
LPBYTE pBuf = new BYTE[BufSize];
file.Seek(0, CFile::begin);
pDlg->SendMsg(2,pDlg->Sendto," 开始接收数据.......");
pDlg->SetTimer(2,500,0);
while(1)
{
if(pDlg->IsEnd==1)
break;
//一次读取BufSize大小的文件内容
nLen=file.Read(pBuf,BufSize);
if(nLen==0)
break;
ByteNum=sockSend.Send(pBuf, nLen);
if(ByteNum==SOCKET_ERROR)
{
AfxMessageBox("发送失败!");
break;
}
pDlg->ByteTotal+=ByteNum;
pos=int(pDlg->ByteTotal*100.0/pDlg->m_dwFileSize);
if(pos1!=pos)
{
pos1=pos;
pDlg->m_ctrProgress.SetPos(pos);
}
str.Format("%d",pos);
if(str1!=str)
{
str1=str;
str=str+"%";
pDlg->GetDlgItem(IDC_STATIC2)->SetWindowText(str);
}
}
pDlg->KillTimer(2);
sockSend.Close();
// Sleep(200);
file.Close();
pDlg->IsEnd=0;
pDlg->Hide();
pDlg->m_ctrProgress.SetPos(0);
if(pDlg->ByteTotal==pDlg->m_dwFileSize)
pDlg->SendMsg(2,pDlg->Sendto," 文件接收完成!");
return 0;
}
//接收数据线程
UINT ReceiveDataThread(LPVOID lpParam)
{
CClientDlg *pDlg=(CClientDlg *)lpParam;
//保存文件对话框
CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"所有文件 (*.*)|*.*||");
strcpy(dlg.m_ofn.lpstrFile, pDlg->m_strFileName.GetBuffer(pDlg->m_strFileName.GetLength()));
while(dlg.DoModal()!=IDOK)
{
return 0;
}
pDlg->GetDlgItem(IDC_BUTTON_END)->SetWindowText("中止");
CSocket sockRecv;
sockRecv.Create();
pDlg->GetDlgItem(IDC_STATIC_FILENAME)->SetWindowText(dlg.GetFileName());
pDlg->GetDlgItem(IDC_BUTTON_RECV)->ShowWindow(SW_HIDE);//接收按钮
pDlg->m_ctrProgress.SetRange(0,100);
pDlg->SendMsg(6,pDlg->sender,"\r\n 对方已接受请求,建立连接.......");
while(sockRecv.Connect(pDlg->f_ip,2000)==0)
{
Sleep(50);
}
CFile file;
if(!file.Open(dlg.GetPathName(),CFile::modeCreate|CFile::modeWrite))
{
AfxMessageBox("打开文件出错!");
return 0;
}
pDlg->IsEnd=0;
int BufSize=SENDDATE;
int ByteNum=0,pos,pos1=0;
pDlg->ByteTotal=pDlg->pb=0;
CString str,str1;
LPBYTE pBuf = new BYTE[BufSize];
pDlg->SendMsg(2,pDlg->sender," 开始传输数据.......");
pDlg->SetTimer(2,500,0);
while(1)
{
if(pDlg->IsEnd==1)
break;
//一次读取BufSize大小的文件内容
ByteNum=sockRecv.Receive(pBuf,BufSize);
if(ByteNum==SOCKET_ERROR)
{
AfxMessageBox("接收错误!");
break;
}
if(ByteNum==0)
break;
file.Write(pBuf,ByteNum);
pDlg->ByteTotal+=ByteNum;
pos=int(pDlg->ByteTotal*100.0/pDlg->m_dwFileSize);
if(pos1!=pos)
{
pos1=pos;
pDlg->m_ctrProgress.SetPos(pos);
}
str.Format("%d",pos);
if(str1!=str)
{
str1=str;
str=str+"%";
pDlg->GetDlgItem(IDC_STATIC2)->SetWindowText(str);
}
}
pDlg->KillTimer(2);
sockRecv.Close();
pDlg->IsEnd=0;
file.Close();
pDlg->Hide();
pDlg->m_ctrProgress.SetPos(0);
if(pDlg->ByteTotal==pDlg->m_dwFileSize)
pDlg->SendMsg(2,pDlg->sender," 文件发送完成!");
return 0;
}
void CClientDlg::OnButtonEnd()
{
// TODO: Add your control notification handler code here
CString kind;
IsEnd=1;
GetDlgItem(IDC_BUTTON_END)->GetWindowText(kind);
if(kind=="拒绝")
{
m_strRecv=m_strRecv+"\r\n\r\n"+" 您拒绝了对方的请求!文件传输被取消";
SendMsg(7,sender,"\r\n 对方拒绝了您的请求!");
Hide();
}
else if(kind=="取消")
{
m_strRecv=m_strRecv+"\r\n\r\n"+" 您取消了文件传输";
SendMsg(7,Sendto,"\r\n 对方取消了文件传输!");
Hide();
}
else if(kind=="中止")
{
m_strRecv=m_strRecv+"\r\n\r\n"+" 您中止了文件传输";
SendMsg(7,sender,"\r\n 对方中止了文件传输!");
}
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
CEdit *p=new CEdit;
p=(CEdit *)GetDlgItem(IDC_EDIT_RECV);
p->LineScroll(p->GetLineCount(),0);
}
void CClientDlg::OnButtonRecv()
{
// TODO: Add your control notification handler code here
AfxBeginThread(ReceiveDataThread,this,THREAD_PRIORITY_NORMAL);
}
void CClientDlg::Hide()
{
GetDlgItem(IDC_PROGRESS1)->ShowWindow(SW_HIDE);
m_ctrEnd.ShowWindow(SW_HIDE);
GetDlgItem(IDC_STATIC_RECV)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STATIC_FILENAME)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STATIC_FILESIZE)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STATIC2)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_BUTTON_TANS)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_BUTTON_RECV)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STATIC_SPEED)->ShowWindow(SW_HIDE);
}
void CClientDlg::SendMsg(int type, CString name, CString msg)
{
sendinfo info;
info.type=type;
memcpy(info.name,name,name.GetLength()+1);
memcpy(info.msg,msg,msg.GetLength()+1);
int charsend=send(clientsock,(char*)&info,sizeof(info),0);
if(charsend==SOCKET_ERROR)
{
MessageBox("发送过程中发生一个错误!",NULL,MB_OK);
return;
}
}
CString CClientDlg::KindChange(DWORD dwFileSize)
{
float size;
CString filesize;
if(dwFileSize<1024)
{
filesize.Format("%d字节",dwFileSize);
}
else if(dwFileSize<(1024*1024))
{
size=dwFileSize*1.0/1024;
filesize.Format("%.1fKB",size);
}
else
{
size=dwFileSize*1.0/(1024*1024);
filesize.Format("%.1fMB",size);
}
return filesize;
}
void CClientDlg::OnButtonShake()
{
// TODO: Add your control notification handler code here
if(Ishake==0)
return;
UpdateData(true);
int type;
if(m_strUser!="大家")
{
type=2;
m_strSend=" "+cuser+"对你发送了窗口抖动";
}
else if(m_strUser=="大家")
{
type=1;
m_strSend=" "+cuser+"对大家发送了窗口抖动";
}
SendMsg(type,m_strUser,"\\shake");
SendMsg(type,m_strUser,m_strSend);
Shaking();
m_strSend=_T("");
m_ctrSend.SetFocus();
GetDlgItem(IDC_EDIT_RECV)->SetWindowText(m_strRecv);
CEdit *p=new CEdit;
p=(CEdit *)GetDlgItem(IDC_EDIT_RECV);
p->LineScroll(p->GetLineCount(),0);
Ishake=0;
SetTimer(3,10000,0);
}
void CClientDlg::Shaking()
{
int ty=3;
CRect m_rect;
this->ShowWindow(SW_SHOW);
// HWND Hwnd;
// Hwnd=::GetForegroundWindow();
::SetForegroundWindow(m_hWnd);
::GetWindowRect(m_hWnd,&m_rect);
int recordy=m_rect.left;
int recordx=m_rect.top;
for(int i=0;i<3;i++)
{
m_rect.left=recordy;
m_rect.top=recordx;
m_rect.top = m_rect.top + ty;
m_rect.left = m_rect.left - ty;
::SetWindowPos(m_hWnd,NULL,m_rect.left,m_rect.top,0,0,SWP_NOSIZE);Sleep(20);
m_rect.top = m_rect.top -ty;
::SetWindowPos( m_hWnd,NULL,m_rect.left,m_rect.top,0,0,SWP_NOSIZE);Sleep(20);
m_rect.top = m_rect.top -ty;
::SetWindowPos( m_hWnd,NULL,m_rect.left,m_rect.top,0,0,SWP_NOSIZE);Sleep(20);
m_rect.left=m_rect.left+ty;
::SetWindowPos( m_hWnd,NULL,m_rect.left,m_rect.top,0,0,SWP_NOSIZE);Sleep(20);
m_rect.left=m_rect.left+ty;
::SetWindowPos( m_hWnd,NULL,m_rect.left,m_rect.top,0,0,SWP_NOSIZE);Sleep(20);
m_rect.top = m_rect.top + ty;
::SetWindowPos( m_hWnd,NULL,m_rect.left,m_rect.top,0,0,SWP_NOSIZE);Sleep(20);
m_rect.top=m_rect.top+ty;
::SetWindowPos( m_hWnd,NULL,m_rect.left,m_rect.top,0,0,SWP_NOSIZE);Sleep(20);
m_rect.top=m_rect.top+ty;
::SetWindowPos( m_hWnd,NULL,m_rect.left,m_rect.top,0,0,SWP_NOSIZE);Sleep(20);
::SetWindowPos( m_hWnd,NULL,recordy,recordx,0,0,SWP_NOSIZE);Sleep(3);
}
}
HBRUSH CClientDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
HBRUSH hbr=CreateSolidBrush(RGB(250,250,250));
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
return hbr;
}
//DEL // HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
//DEL CTLCOLOR_BTN(按钮控件)
//DEL CTLCOLOR_DLG(对话框)
//DEL CTLCOLOR_EDIT(编辑控件)
//DEL CTLCOLOR_LISTBOX(列表框)
//DEL CTLCOLOR_MSGBOX(消息框)
//DEL CTLCOLOR_SCROLLBAR(滚动条)
//DEL CTLCOLOR_STATIC(静态框)
//DEL /* if(nCtlColor==CTLCOLOR_EDIT)
//DEL {
//DEL // pDC->SetBkMode(TRANSPARENT);
//DEL // pDC->SetBkColor(RGB(255,0,255));
//DEL // 此处设置字体的颜色
//DEL pDC->SetTextColor(RGB(0,0,180));
//DEL // return m_hbr1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -