📄 myqqdlg.cpp
字号:
GetDlgItem(IDC_BUTTON_STOP_FILE)->EnableWindow(false);
GetDlgItem(IDCANCEL)->EnableWindow(true);
GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(true);
return 0;
}
////////////////////////////////按钮///////////////////////////////////////////////
////////////////////////////////按钮///////////////////////////////////////////////
////////////////////////////////按钮///////////////////////////////////////////////
void CMyQQDlg::OnButtonConnect() //开始连接
{
// TODO: Add your control notification handler code here
CString str;
m_You_IP.GetWindowText(str);
CString strOut,strIn;
m_You_IP.GetWindowText(strIn);
if(GetNamebyAddress(strIn,strOut)==-1)
{
GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(true);//连接不可用
return;
}
m_MsgSend="你好"+strOut+"!";
UpdateData(false);
if(m_nSockType==SOCK_TCP)
{
pThreadLisen=::AfxBeginThread(_ListenTcpThread,this); //开始TCP线程
GetDlgItem(IDC_BUTTON_SEND_MSG)->EnableWindow(true);//发送消息可用
GetDlgItem(IDC_BUTTON_SEND_FILE)->EnableWindow(true);//文件可用
GetDlgItem(IDC_BUTTON_CAPUTER)->EnableWindow(true);//抓取
}
else
{
pThreadLisen=::AfxBeginThread(_UDPThread,this); //开始UDP线程
GetDlgItem(IDC_BUTTON_SEND_MSG)->EnableWindow(true);//发送可用
GetDlgItem(IDC_BUTTON_SEND_FILE)->EnableWindow(false);//文件不可用
}
GetDlgItem(IDC_RADIO_TCP)->EnableWindow(false);//单选不可用
GetDlgItem(IDC_RADIO_UDP)->EnableWindow(false);//单选不可用
GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(true);//断开可用
GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(false);//连接不可用
GetDlgItem(IDC_RADIO_SERVER)->EnableWindow(false);
GetDlgItem(IDC_RADIO_CLIENT)->EnableWindow(false);
GetDlgItem(IDC_RADIO_BOTH)->EnableWindow(false);
GetDlgItem(IDC_IPADDRESS)->EnableWindow(false);
}
void CMyQQDlg::OnButtonDisconnect() //关闭
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(true);
GetDlgItem(IDC_RADIO_TCP)->EnableWindow(true);
((CButton*)GetDlgItem(IDC_RADIO_UDP))->EnableWindow(true);
GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(false);
GetDlgItem(IDC_BUTTON_SEND_MSG)->EnableWindow(false);
GetDlgItem(IDC_BUTTON_SEND_FILE)->EnableWindow(false);
m_AddMsgLIst.ResetContent();
GetDlgItem(IDC_BUTTON_CLEAR)->EnableWindow(false);
((CButton*)GetDlgItem(IDC_RADIO_SERVER))->EnableWindow(true);
((CButton*)GetDlgItem(IDC_RADIO_CLIENT))->EnableWindow(true);
((CButton*)GetDlgItem(IDC_RADIO_BOTH))->EnableWindow(true);
GetDlgItem(IDC_BUTTON_CAPUTER)->EnableWindow(false);
GetDlgItem(IDC_IPADDRESS)->EnableWindow(true);
m_AddMsgLIst.SendMessage(LB_SETHORIZONTALEXTENT,0,0);
/////////////////////////////////////////发送结束
if(m_nSockType==SOCK_TCP)
{
DWORD dwStatus;
if (pThreadLisen != NULL)
{
if(::GetExitCodeThread(pThreadLisen->m_hThread, &dwStatus)==0)
{
int errror=GetLastError();
return;
}
if (dwStatus == STILL_ACTIVE)
{
CSocket sockClient;
sockClient.Create();
CString ip,strError;
ip="127.0.0.1";
int conn=sockClient.Connect(ip, m_Potr);
if(conn==0) ///////////////////////////////////
{
AfxMessageBox("关闭错误!"+GetError(GetLastError()));
sockClient.ShutDown(2);
sockClient.Close();
return;
}
sockClient.Send("D",FLAG); //结束
}
else
{
delete pThreadLisen;
pThreadLisen = NULL;
}
}
}
else
{
CSocket sockClientUdp;
int succeedCreate=sockClientUdp.Create(m_type,SOCK_DGRAM);
CString strMsg="D";
int ret=0;
CString ipStr;
m_You_IP.GetWindowText(ipStr);
UINT port=PORT+m_server;
ret=sockClientUdp.SendTo(strMsg,1,port,ipStr);
}
}
void CMyQQDlg::OnButtonSendMsg() //发送消息
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(m_MsgSend.GetLength()==0) return;
if(m_nSockType==SOCK_TCP)
::AfxBeginThread(_SendMsgThread,this);
else
::AfxBeginThread(_SendMsgUdpThread,this);
}
void CMyQQDlg::OnButtonSendFile() //发送文件
{
// TODO: Add your control notification handler code here
CFileDialog dlg(true);
CString ip;
m_You_IP.GetWindowText(ip);
CString title="文件发往"+ip+"请选择";
dlg.m_ofn.lpstrTitle=title;//标题条
if(dlg.DoModal()==IDOK)
{
m_fileName=dlg.GetPathName();
GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_SHOW);
pThreadSendFile=::AfxBeginThread(_SendFileThread,this); //开始传送文件线程
}
}
void CMyQQDlg::OnButtonCaputer() //抓取屏幕
{
// TODO: Add your control notification handler code here
pThreadCaputre=::AfxBeginThread(_ThreadCapture,this); //开始传送文件线程
}
void CMyQQDlg::AddMsgList(CString IP,CString str) //添加信息于LISTBOX 控件中
{
SYSTEMTIME tm;
GetLocalTime(&tm);
CString time;
time.Format(_T(" %d:%02.2d"), tm.wHour, tm.wMinute);
m_AddMsgLIst.AddString(IP+"于"+time+"说道: "+str);
int numList=m_AddMsgLIst.GetCount()-1;
GetDlgItem(IDC_BUTTON_CLEAR)->EnableWindow(true);
m_AddMsgLIst.SetTopIndex(numList);
m_AddMsgLIst.SetCurSel(numList);
//水平滚动
int max_width=0;
CSize sz;
CClientDC dc(this);
for(int i=0;i<m_AddMsgLIst.GetCount();i++)
{
m_AddMsgLIst.GetText(i,str);
sz=dc.GetTextExtent(str);
if(max_width<sz.cx)
max_width=sz.cx;
}
m_AddMsgLIst.SendMessage(LB_SETHORIZONTALEXTENT,max_width,0);
}
void CMyQQDlg::OnButtonClear() //清除聊天内容
{
// TODO: Add your control notification handler code here
m_AddMsgLIst.ResetContent();
GetDlgItem(IDC_BUTTON_CLEAR)->EnableWindow(false);
m_AddMsgLIst.SendMessage(LB_SETHORIZONTALEXTENT,0,0);
}
void CMyQQDlg::OnRadioTcp() //选择TCP模式
{
// TODO: Add your control notification handler code here
m_nSockType=SOCK_TCP;
CString text;
if(m_WorkType==0) text="MyQQ服务器";
else if(m_WorkType==1) text="MyQQ客户端";
else text="MyQQ";
SetWindowText(text+" TCP方式");
}
void CMyQQDlg::OnRadioUdp() //选择UPD模式
{
// TODO: Add your control notification handler code here
m_nSockType=SOCK_UDP;
CString text;
if(m_WorkType==0) text="MyQQ服务器";
else if(m_WorkType==1) text="MyQQ客户端";
else text="MyQQ";
SetWindowText(text+" UDP方式");
}
CString CMyQQDlg::GetError(DWORD error) //返回错误信息
{
CString strError;
switch(error)
{
case WSANOTINITIALISED:
strError="初始化错误";
break;
case WSAENOTCONN:
strError="对方没有启动";
break;
case WSAEWOULDBLOCK :
strError="对方已经关闭";
break;
case WSAECONNREFUSED:
strError="连接的尝试被拒绝";
break;
case WSAENOTSOCK:
strError="在一个非套接字上尝试了一个操作";
break;
case WSAEADDRINUSE:
strError="特定的地址已在使用中";
break;
case WSAECONNRESET:
strError="与主机的连接被关闭";
break;
default:
strError="一般错误";
}
return strError;
}
int CMyQQDlg::GetLocalHostName(CString &sHostName) //获得本地计算机名称
{
char szHostName[256];
int nRetCode;
nRetCode=gethostname(szHostName,sizeof(szHostName));
if(nRetCode!=0)
{
//产生错误
sHostName=_T("没有取得");
return GetLastError();
}
sHostName=szHostName;
return 0;
}
int CMyQQDlg::GetIpAddress(const CString &sHostName, CString &sIpAddress)//获得本地IP
{
struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
if(lpHostEnt==NULL)
{
//产生错误
sIpAddress=_T("");
return GetLastError();
}
//获取IP
LPSTR lpAddr=lpHostEnt->h_addr_list[0];
if(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
//转换为标准格式
sIpAddress=inet_ntoa(inAddr);
if(sIpAddress.IsEmpty())
sIpAddress=_T("没有取得");
}
return 0;
}
int CMyQQDlg::GetNamebyAddress(const CString &IpAddress,CString &sYouName)//获得对方计算机名称
{
unsigned long addr;
addr=inet_addr(IpAddress);
struct hostent FAR * lpHostEnt=gethostbyaddr((char *)&addr,4,AF_INET);
if(lpHostEnt==NULL)
{
//产生错误
sYouName=_T("");
AfxMessageBox("连接不上");//应该取得其错误
return -1;
}
CString name=lpHostEnt->h_name;
sYouName=name;
return 0;
}
void CMyQQDlg::OnRadioServer()
{
// TODO: Add your control notification handler code here
CString text;
if(m_nSockType==SOCK_TCP) text="TCP方式";
else
text="UDP方式";
m_server=1;
m_client=2;
m_WorkType=0;
SetWindowText("MyQQ服务器 "+text);
GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("启动服务");
GetDlgItem(IDC_BUTTON_DISCONNECT)->SetWindowText("关闭服务");
}
void CMyQQDlg::OnRadioClient()
{
// TODO: Add your control notification handler code here
CString text;
if(m_nSockType==SOCK_TCP) text="TCP方式";
else
text="UDP方式";
m_server=2;
m_client=1;
m_WorkType=1;
SetWindowText("MyQQ客户端 "+text);
GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("连 接");
GetDlgItem(IDC_BUTTON_DISCONNECT)->SetWindowText("断开连接");
}
void CMyQQDlg::OnRadioBoth()
{
// TODO: Add your control notification handler code here
CString text;
if(m_nSockType==SOCK_TCP) text="TCP方式";
else
text="UDP方式";
m_server=m_client=0;
m_WorkType=2;
SetWindowText("MyQQ "+text);
GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("启 动");
GetDlgItem(IDC_BUTTON_DISCONNECT)->SetWindowText("关 闭");
}
void CMyQQDlg::OnButtonStopFile()
{
// TODO: Add your control notification handler code here
FileStop=true;
FileWork=false;
GetDlgItem(IDCANCEL)->EnableWindow(true);
GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(true);
}
LRESULT CMyQQDlg::OnKSend(WPARAM wParam,LPARAM lParam)
{
OnButtonSendMsg();
return 0;
}
BOOL CMyQQDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
if(GetFocus()->GetDlgCtrlID()==IDC_EDIT_SENDMSG ||GetFocus()->GetDlgCtrlID()==IDC_BUTTON_SEND_MSG)
{
AfxGetMainWnd()->SendMessage(WM_KSEND);
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
//////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -