chatdlg.cpp

来自「仿造QQ聊天界面 超级相向 第一次作品 不足之处见谅」· C++ 代码 · 共 1,568 行 · 第 1/3 页

CPP
1,568
字号
// ChatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Chat.h"
#include "ChatDlg.h"
#include "iostream.h"
#include "Windows.h"
#include "stdio.h"

#include <fstream>
#include <string>
using   namespace   std;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static	CString getin2;
static	CString record;
CWinThread	*pThreadSendFile;	//发送文件线程-->_SendFileThread
CWinThread	*pReceiveThread;		//接受线程-->_ReceiveThread
CWinThread	*pThreadLisen;		//监听线程-->_ListenTcpThread
CWinThread  *pThreadCaputre;		//抓取屏幕线程
#define PORT		34567
#define FLAG		2
#define SIZEFILE	1024

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CString time1;
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()

/////////////////////////////////////////////////////////////////////////////
// CChatDlg dialog

CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChatDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChatDlg)
	m_keep = _T("");
	m_keep1 = _T("");
//	m_gifboy = _T("");
//	m_gifgirl = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	FileWork=false;
	FileStop=false;
	StopServer=false;
}

void CChatDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChatDlg)
	DDX_Control(pDX, IDC_EDIT_SEND, m_editsend);
	DDX_Control(pDX, IDC_PROGRESS_SEND_FILE, m_Progress);
	DDX_Control(pDX, IDC_IPADDRESS1, m_ip);
	DDX_Text(pDX, IDC_EDIT_RECV, m_keep);
	DDX_Text(pDX, IDC_RECORD, m_keep1);
	DDX_Control(pDX, IDC_GIFBOY, m_gifboy);
	DDX_Control(pDX, IDC_GIFGIRL, m_gifgirl);
	DDX_Control(pDX, IDC_VOICE, m_voice);
	DDX_Control(pDX, IDC_BTN_SEND, m_send);
	DDX_Control(pDX, IDC_BUTTON_CAPUTER, m_capture);
	DDX_Control(pDX, IDC_BUTTON_DISCONNECT, m_disconnect);
	DDX_Control(pDX, IDC_COLOR, m_color);
	DDX_Control(pDX, IDC_CONNECT, m_connect);
	DDX_Control(pDX, IDC_FONT, m_fontcol);
	DDX_Control(pDX, IDC_KEEP, m_keepcol);
	DDX_Control(pDX, IDC_MY_CAPUTER, m_mycapture);
	DDX_Control(pDX, IDC_SEND_FILE, m_sendfile);
	DDX_Control(pDX, IDC_STOP_FILESEND, m_stopfilesend);
	DDX_Control(pDX, IDCANCEL, m_cancle);
	DDX_Control(pDX, IDOK, m_ok);
	DDX_Control(pDX, IDC_BKCOLOR, m_bkcolor);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
	//{{AFX_MSG_MAP(CChatDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
	ON_BN_CLICKED(IDC_FONT, OnFont)
	ON_BN_CLICKED(IDC_COLOR, OnColor)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BKCOLOR, OnBkcolor)
	ON_BN_CLICKED(IDC_KEEP, OnKeep)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_SEND_FILE, OnSendFile)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect)
	ON_BN_CLICKED(IDC_BUTTON_CAPUTER, OnButtonCaputer)
	ON_BN_CLICKED(IDC_MY_CAPUTER, OnMyCaputer)
	ON_BN_CLICKED(IDC_STOP_FILESEND, OnStopFilesend)
	ON_MESSAGE(MM_WIM_DATA,OnMM_WIM_DATA)
	ON_MESSAGE(MM_WIM_CLOSE,OnMM_WIM_CLOSE)
	ON_MESSAGE(MM_WOM_OPEN,OnMM_WOM_OPEN)
	ON_MESSAGE(MM_WOM_DONE,OnMM_WOM_DONE)
	ON_MESSAGE(MM_WOM_CLOSE,OnMM_WOM_CLOSE)
	ON_BN_CLICKED(IDC_VOICE, OnVoice)
	ON_WM_SIZE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_RECVDATA,OnRecvData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatDlg message handlers
UINT _ListenTcpThread(LPVOID lparam);
BOOL CChatDlg::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
	InitSocket();
	// TODO: Add extra initialization here


	if (m_gifgirl.Load(MAKEINTRESOURCE(IDR_GIFGIRL),_T("gif")))
	m_gifgirl.Draw();
	if (m_gifboy.Load(MAKEINTRESOURCE(IDR_GIFBOY),_T("gif")))
	m_gifboy.Draw();

	RECVPARAM *pRecvParam=new RECVPARAM;
	pRecvParam->sock=m_socket;
	pRecvParam->hwnd=m_hWnd;
	HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);
	CloseHandle(hThread);
	CString strIP="127.0.0.1";
	DWORD dwAddress= ntohl( inet_addr(strIP));
    m_ip.SetAddress(dwAddress);
	rgb1=RGB(255,0,0);
	SetWindowText("QQ阉割版--by Mr.an");
	GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(false);
	GetDlgItem(IDC_STOP_FILESEND)->EnableWindow(false);
	GetDlgItem(IDC_SEND_FILE)->EnableWindow(false); 
	GetDlgItem(IDC_BUTTON_CAPUTER)->EnableWindow(false);
	GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_RECORD)->ShowWindow(SW_HIDE);
	GetDlgItem(IDOK)->EnableWindow(false);
	//allocate memory for wave header
    pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	pWaveHdr2=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	pWaveHdrOut=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	//***reinterpret_cast ****  operation is used to convert any type of
	//pointer to other type
	//allocate memory for save buffer
	for(int i=0;i<InBlocks;i++)
	{
		m_AudioDataIn[i].dwLength = 0;
		m_AudioDataIn[i].lpdata = reinterpret_cast<PBYTE>(malloc(1));
		
	}
	
	for(i =0;i<OutBlocks;i++)
	{
		m_AudioDataOut[i].dwLength = 0;
		m_AudioDataOut[i].lpdata = reinterpret_cast<PBYTE>(malloc(1));
	}
	nAudioIn = 0;
	nAudioOut = 0;
	nSend = 0;
	nReceive = 0;
	
	 
   
    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CChatDlg::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 CChatDlg::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 CChatDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CChatDlg::InitSocket()                      //套接字初始化
{
	m_socket=socket(AF_INET,SOCK_DGRAM,0);
	if(INVALID_SOCKET==m_socket)
	{
		MessageBox("套接字创建失败!");
		return FALSE;
	}
	SOCKADDR_IN addrSock;
	addrSock.sin_family=AF_INET;
	addrSock.sin_port=htons(6060);
	addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

	int retval;
	retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
	if(SOCKET_ERROR==retval)
	{
		closesocket(m_socket);
		MessageBox("绑定失败!");
		return FALSE;
	}
	return TRUE;

}

DWORD WINAPI CChatDlg::RecvProc(LPVOID lpParameter)          //接收MESG回调函数
{
	SOCKET sock=((RECVPARAM*)lpParameter)->sock;
	HWND hwnd=((RECVPARAM*)lpParameter)->hwnd;
	delete lpParameter;	//视频讲述时,遗忘了释放内存的操作。sunxin

	SOCKADDR_IN addrFrom;
	int len=sizeof(SOCKADDR);

	char recvBuf[200];
	int retval;
	while(TRUE)
	{
		retval=recvfrom(sock,recvBuf,200,0,(SOCKADDR*)&addrFrom,&len);
		if(SOCKET_ERROR==retval)
			break;
		::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)recvBuf);
	}
	
	return 0;
}

void CChatDlg::OnRecvData(WPARAM wParam,LPARAM lParam)                     //接收MESG函数
{
	CString str=(char*)lParam;
	HANDLE hFile = CreateFile("keep.txt",           // open KEEP.TXT 
								GENERIC_ALL,              // open for reading 
								FILE_SHARE_READ,           // share for reading 
								NULL,                      // no security 
								OPEN_ALWAYS,             // existing file only 
								FILE_ATTRIBUTE_NORMAL,     // normal file 
								NULL);					// no attr. template 
	CWnd hwnd3;
	if (hFile == INVALID_HANDLE_VALUE) 
	{ 
		hwnd3.MessageBox("process error!");   // process error 
	} 
	if (SetFilePointer(hFile,0,NULL,FILE_END)==-1)
	{
		hwnd3.MessageBox("SetFilePointer error!");
	}
	DWORD dwWrite;
	if (!WriteFile(hFile,str,str.GetLength(),&dwWrite,NULL))
	{
		MessageBox("writefile error!");
	}
	m_keep+=str;
	UpdateData(FALSE);//////////////////
	GetDlgItem(IDC_EDIT_RECV)->SendMessage(WM_VSCROLL, MAKEWPARAM(SB_BOTTOM,0),0); ///////////////////
	GetDlgItem(IDC_RECORD)->SendMessage(WM_VSCROLL, MAKEWPARAM(SB_BOTTOM,0),0);
	CloseHandle(hFile);
}

void CChatDlg::OnBtnSend()                               //发送MESG 缺省回车
{
	// TODO: Add your control notification handler code here
	((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);
	addrTo.sin_family=AF_INET;
	addrTo.sin_port=htons(6060);
	addrTo.sin_addr.S_un.S_addr=htonl(dwIP);
	str2=inet_ntoa(addrTo.sin_addr);

	
	CString strSend;
	GetDlgItemText(IDC_EDIT_SEND,strSend);
	SetDlgItemText(IDC_EDIT_SEND,"");
	CString str;
	CString strTemp;
	CString strTemp1;
	m_time = CTime::GetCurrentTime();
	time1=m_time.Format( "%H:%M:%S" );
	getin2.Format("%s于%s说:%s",inet_ntoa(addrTo.sin_addr),time1,strSend);
	getin2+="\r\n";

	
	len=sendto(m_socket,getin2,getin2.GetLength()+1,0,
		(SOCKADDR*)&addrTo,sizeof(SOCKADDR));	

	HANDLE hFile = CreateFile("keep.txt",           // open KEEP.TXT 
								GENERIC_ALL,              // open for reading 
								FILE_SHARE_READ,           // share for reading 
								NULL,                      // no security 
								OPEN_ALWAYS,             // existing file only 
								FILE_ATTRIBUTE_NORMAL,     // normal file 
								NULL);                     // no attr. template 
	CWnd hwnd3;
	if (hFile == INVALID_HANDLE_VALUE) 
	{ 
		hwnd3.MessageBox("process error!");   // process error 
	} 
	if (SetFilePointer(hFile,0,NULL,FILE_END)==-1)
	{
		hwnd3.MessageBox("SetFilePointer error!");
	}
	DWORD dwWrite;
	
		if (!WriteFile(hFile,getin2,getin2.GetLength(),&dwWrite,NULL))
				{
					MessageBox("writefile error!");
				}
		m_keep+=getin2;
		GetDlgItem(IDC_EDIT_RECV)->SendMessage(WM_VSCROLL, MAKEWPARAM(SB_BOTTOM,0),0); 
		UpdateData(FALSE);
		CloseHandle(hFile);
		GotoDlgCtrl(GetDlgItem(IDC_EDIT_SEND));   
	
}

void CChatDlg::OnFont()                            //改换字体
{
	// TODO: Add your control notification handler code here
	CFontDialog dlg;
	if (dlg.DoModal() == IDOK)
	{
		LOGFONT lf;
		memcpy(&lf, dlg.m_cf.lpLogFont, sizeof(LOGFONT));
		
		CFont font;
		VERIFY(font.CreateFontIndirect(&lf));
		m_font=&font;
		CClientDC dc(this);
		CFont* def_font = dc.SelectObject(m_font);
		CEdit*pEdt=(CEdit*)GetDlgItem(IDC_EDIT_RECV);
		pEdt->SetFont(m_font);
		dc.SelectObject(def_font);
		::DeleteObject(m_font);
		
	}
}



void CChatDlg::OnColor()                                      //改换颜色
{
	// TODO: Add your control notification handler code here
	CColorDialog dlg;
	dlg.m_cc.Flags |= CC_RGBINIT;
	dlg.m_cc.rgbResult = RGB(255, 0, 0);

	if (dlg.DoModal()==IDOK)
	{
		rgb1=dlg.GetColor();	
	}
}



HBRUSH CChatDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if(pWnd->GetDlgCtrlID()==IDC_EDIT_RECV)
	{
		pDC->SetTextColor(rgb1);
		pDC->SetBkMode(TRANSPARENT);
		return m_brush;
	}
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CChatDlg::OnBkcolor()										//改换背景颜色
{
	// TODO: Add your control notification handler code here
	CColorDialog dlg;
	dlg.m_cc.Flags |= CC_RGBINIT;
	dlg.m_cc.rgbResult = RGB(255, 255, 255);
	
	if (dlg.DoModal()==IDOK)
	{
		rgb2=dlg.GetColor();
		m_brush.CreateSolidBrush(rgb2);
	}
Invalidate();
	//DeleteObject(m_brush);
}

void CChatDlg::OnKeep()												//打开聊天记录
{
	// TODO: Add your control notification handler code here
	

			CString strpath;
			GetDlgItemText(IDC_KEEP,strpath);
			if(strpath==" 聊天记录>>")
			{
				SetWindowPos(NULL,0,0,m_large.Width(),m_large.Height(),
						SWP_NOMOVE | SWP_NOZORDER);
				SetDlgItemText(IDC_KEEP," 聊天记录<<");
				GetDlgItem(IDC_RECORD)->ShowWindow(SW_SHOW);
				UpdateData(TRUE); 
				ifstream txt("keep.txt"); 
				if(txt.fail()) 
				{ 
					MessageBox("Open file failed."); 
					return; 
				} 
				string str; 
				while(!txt.eof()) 
				{ 
					getline(txt, str); 
					m_keep1 += str.c_str(); 
					m_keep1+= "\xd\xa"; 
				} 
				txt.close();
				UpdateData(FALSE); 
				GetDlgItem(IDC_GIFGIRL)->ShowWindow(false);
				GetDlgItem(IDC_GIFBOY)->ShowWindow(false);
				GetDlgItem(IDC_VSTATIC1)->ShowWindow(false);
				GetDlgItem(IDC_VSTATIC2)->ShowWindow(false);
				GetDlgItem(IDC_VSTATIC3)->ShowWindow(false);
				GetDlgItem(IDC_VSTATIC4)->ShowWindow(false);

⌨️ 快捷键说明

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