⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myqqdlg.cpp

📁 用vc实现的聊天程序!功能强大!大家不妨看看!模仿实现
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// MyQQDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyQQ.h"
#include "MyQQDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include <dlgs.h>
/////////////////////////////////////////////////////////////////////////////
// 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()


/////////////////////////////////////////////////////////////////////////////
// CMyQQDlg dialog



#define PORT		34567
#define FLAG		2
#define SIZEFILE	1024

const int SOCK_TCP	= 0;	//TCP模式
const int SOCK_UDP  = 1;	//UPD模式

CWinThread	*pThreadSendFile;	//发送文件线程-->_SendFileThread
CWinThread	*pThreadSendMsg;		//发送消息线程
CWinThread	*pThreadLisen;		//监听线程-->_ListenTcpThread
CWinThread	*pReceiveThread;		//接受线程-->_ReceiveThread
CWinThread  *pThreadCaputre;		//抓取屏幕线程


//////////////////////////////////////////////////////////////////////////////


CMyQQDlg::CMyQQDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyQQDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyQQDlg)
	m_MsgSend = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_nSockType=0;//TCP
	m_WorkType=2;//两者
	m_client=0;
	m_server=0;
	FileWork=false;
	FileStop=false;
	StopServer=false;
}

void CMyQQDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyQQDlg)
	DDX_Control(pDX, IDC_PROGRESS_SEND_FILE, m_Progress);
	DDX_Control(pDX, IDC_LIST_BOX_ADDMSG, m_AddMsgLIst);
	DDX_Control(pDX, IDC_IPADDRESS, m_You_IP);
	DDX_Text(pDX, IDC_EDIT_SENDMSG, m_MsgSend);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyQQDlg, CDialog)
//{{AFX_MSG_MAP(CMyQQDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect)
ON_BN_CLICKED(IDC_BUTTON_SEND_MSG, OnButtonSendMsg)
ON_BN_CLICKED(IDC_BUTTON_SEND_FILE, OnButtonSendFile)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
ON_BN_CLICKED(IDC_RADIO_TCP, OnRadioTcp)
ON_BN_CLICKED(IDC_RADIO_UDP, OnRadioUdp)
ON_BN_CLICKED(IDC_RADIO_SERVER, OnRadioServer)
ON_BN_CLICKED(IDC_RADIO_CLIENT, OnRadioClient)
ON_BN_CLICKED(IDC_RADIO_BOTH, OnRadioBoth)
ON_BN_CLICKED(IDC_BUTTON_STOP_FILE, OnButtonStopFile)
ON_MESSAGE(WM_KSEND,OnKSend)
ON_BN_CLICKED(IDC_BUTTON_CAPUTER, OnButtonCaputer)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyQQDlg message handlers

BOOL CMyQQDlg::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
	
	// TODO: Add extra initialization here
	/////////////////////////////////////////////////////////
	CString strLocalName;
	GetLocalHostName(strLocalName);
	CString strLocalIP;
	GetIpAddress(strLocalName,strLocalIP);
	m_You_IP.SetWindowText(strLocalIP);	//设置默认IP为本机
	/////////////////////////////////////////////////////////
	((CButton*)GetDlgItem(IDC_RADIO_BOTH))->SetCheck(BST_CHECKED);//默认为服务器、客户端一体
	SetWindowText("MyQQ TCP方式");

	GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("启    动");
	GetDlgItem(IDC_BUTTON_DISCONNECT)->SetWindowText("关    闭");
	
	GetDlgItem(IDC_BUTTON_CAPUTER)->EnableWindow(false);//默认为不可勇
	((CButton*)GetDlgItem(IDC_RADIO_TCP))->SetCheck(BST_CHECKED);//默认为TCP
	GetDlgItem(IDC_BUTTON_SEND_MSG)->EnableWindow(false);//发送消息不可用
	GetDlgItem(IDC_BUTTON_SEND_FILE)->EnableWindow(false);//发送文件不可用
	GetDlgItem(IDC_BUTTON_CLEAR)->EnableWindow(false);//清除不可用
	GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(false);//断开连接不可用
	GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_HIDE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyQQDlg::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 CMyQQDlg::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 CMyQQDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//////////////////////////////////线程///////////////////////////////////////////
//////////////////////////////////线程///////////////////////////////////////////



//////////////////////////////////服务器线程开始///////////////////////////////////////////

//TCP监听线程
UINT _ListenTcpThread(LPVOID lparam)
{
	CMyQQDlg *pDlg=(CMyQQDlg *)lparam;
	if(pDlg->StopServer==true)	return -1;
	
	CSocket sockSrvr;
	pDlg->m_Potr=PORT+pDlg->m_server;//保存当前使用端口,用于关闭
	int createSucceed=sockSrvr.Create(pDlg->m_Potr);
	if(createSucceed==0)
	{
		AfxMessageBox("_ListenTcpThread Create错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	
	int listenSucceed=sockSrvr.Listen();	//开始监听
	if(listenSucceed==0)
	{
		AfxMessageBox("_ListenTcpThread Listen错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	
	CSocket recSo;
	SOCKADDR_IN client;
	int iAddrSize=sizeof(client);
	
	int acceptSucceed=sockSrvr.Accept(recSo,(SOCKADDR *)&client,&iAddrSize);	//接受连接并取得对方IP
	if(acceptSucceed==0)
	{
		AfxMessageBox("_ListenTcpThread Accept错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	sockSrvr.Close();
	char flag[FLAG]={0};		
	if(recSo.Receive(flag,FLAG)!=2) 
	{
		return -1;	
	}
	pDlg->m_type=flag[0];
	if(pDlg->m_type=='D') return 0;
	
	pThreadLisen=::AfxBeginThread(_ListenTcpThread,pDlg);
	pDlg->ReceiveFileMsg(recSo,client);
	return 0;
	
}




UINT _UDPThread(LPVOID lparam)	/////UDP接受信息线程开始
{
	
	CMyQQDlg *pDlg=(CMyQQDlg *)lparam;
	if(pDlg->StopServer==true)	return -1;
	
	CSocket sockSrvrUdp;
	sockSrvrUdp.Create(PORT+pDlg->m_client,SOCK_DGRAM);
	char buff[100]={0};
	int ret=0;
	CString ipStr;
	CString msg;
	UINT port;
	for(;;)
	{
		
		ret=sockSrvrUdp.ReceiveFrom(buff,100,ipStr,port);//IP和port均为返回值
		
		if(buff[0]=='D') return 0;
		if(ret==SOCKET_ERROR) 
		{
			break;
		}
		msg.Format(buff);
		pDlg->AddMsgList(ipStr,msg);
	}
	sockSrvrUdp.Close();
	return 0;
}





////////////////////////////////////服务器线程结束//////////////////////////////////////


////////////////////////////////////客户端线程开始//////////////////////////////////////

//发送文件线程
UINT _SendFileThread(LPVOID lparam)
{
	
	CMyQQDlg *pDlg=(CMyQQDlg *)lparam;
	if(pDlg->StopServer==true)	return -1;
	
	CSocket sockClient;
	sockClient.Create();
	CString ip;
	pDlg->m_You_IP.GetWindowText(ip);
	sockClient.Connect(ip, PORT+pDlg->m_client); 
	//首先发送标记F为文件,2
	int end=0;
	end=sockClient.Send("F",FLAG);
	///////////////////////////////////////////////////////////////////发送标志是否成功
	if(end==SOCKET_ERROR)										
	{
		AfxMessageBox("_SendFileThread Send错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	
	else if(end!=2) 
	{
		AfxMessageBox("文件头错误");
		return -1;
	}
	///////////////////////////////////////////////////////////////////
	CFile myFile;
	FILEINFO myFileInfo;
	if(!myFile.Open(pDlg->m_fileName, CFile::modeRead | CFile::typeBinary))
		return -1;
	myFileInfo.fileLength=myFile.GetLength();		//得到文件大小
	strcpy(myFileInfo.fileName,myFile.GetFileName());//得到文件名称
	
	sockClient.Send(&myFileInfo,sizeof(myFileInfo));	//发送文件信息
	
	pDlg->m_Progress.SetRange32(0,myFileInfo.fileLength);
	
	myFile.Seek(0,CFile::begin);
	char m_buf[SIZEFILE]={0};
	CString strError;
	int num=0;
	end=0;
	int temp=0;
	pDlg->GetDlgItem(IDC_BUTTON_STOP_FILE)->EnableWindow(true);
	
	for(;;)
	{
		if(pDlg->FileWork==false)
		{
			pDlg->FileWork=true;
			pDlg->GetDlgItem(IDCANCEL)->EnableWindow(false);
			pDlg->GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(false);
		}
		num=myFile.Read(m_buf, SIZEFILE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -