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

📄 chatroomdlg.cpp

📁 chat程序
💻 CPP
字号:
// ChatRoomDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ChatRoom.h"
#include "ChatRoomDlg.h"
#include "DlgProxy.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CChatRoomDlg dialog

IMPLEMENT_DYNAMIC(CChatRoomDlg, CDialog);

CChatRoomDlg::CChatRoomDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChatRoomDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChatRoomDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pAutoProxy = NULL;
}

CChatRoomDlg::~CChatRoomDlg()
{
	// If there is an automation proxy for this dialog, set
	//  its back pointer to this dialog to NULL, so it knows
	//  the dialog has been deleted.
	if (m_pAutoProxy != NULL)
		m_pAutoProxy->m_pDialog = NULL;
}

void CChatRoomDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChatRoomDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CChatRoomDlg, CDialog)
	//{{AFX_MSG_MAP(CChatRoomDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_Start, OnBUTTONStart)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_RECVDATA,OnRecvData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatRoomDlg message handlers

BOOL CChatRoomDlg::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

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

// Automation servers should not exit when a user closes the UI
//  if a controller still holds on to one of its objects.  These
//  message handlers make sure that if the proxy is still in use,
//  then the UI is hidden but the dialog remains around if it
//  is dismissed.

void CChatRoomDlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CChatRoomDlg::OnOK() 
{
	//获取对方IP
	DWORD dwIP;
	((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS_RevIP))->GetAddress(dwIP);

	CString tempport;
	int revport;
	GetDlgItemText(IDC_EDIT_RevPort,tempport);
	revport=atoi(tempport);

	SOCKADDR_IN addrTo;
	addrTo.sin_family=AF_INET;
	addrTo.sin_port=htons(revport);
	addrTo.sin_addr.S_un.S_addr=htonl(dwIP);

	//获取昵称和待发送消息
//	CString str_name;
	CString strSend;
	GetDlgItemText(IDC_EDIT_Send,strSend);
	//发送数据
	sendto(m_socket,strSend,strSend.GetLength()+1,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR));

	CString strRev;
	GetDlgItemText(IDC_EDIT_Rev,strRev);
	strSend="我说:"+strSend;
	strSend+="\r\n";
	strSend+=strRev;
	
	SetDlgItemText(IDC_EDIT_Rev,strSend);
	SetDlgItemText(IDC_EDIT_Send,"");

}

void CChatRoomDlg::OnCancel() 
{
	if (CanExit())
		CDialog::OnCancel();
}

BOOL CChatRoomDlg::CanExit()
{
	// If the proxy object is still around, then the automation
	//  controller is still holding on to this application.  Leave
	//  the dialog around, but hide its UI.
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	return TRUE;
}

void CChatRoomDlg::OnBUTTONStart() 
{
	// TODO: Add your control notification handler code here
	InitSocket();

	RECVPARAM* pRecvParam=new RECVPARAM;
	pRecvParam->sock=m_socket;
	pRecvParam->hwnd=m_hWnd;
	//创建接收线程
	HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);
	CloseHandle(hThread);
	GetDlgItem(IDC_BUTTON_Start)->EnableWindow(false);
	
}

bool CChatRoomDlg::InitSocket()
{
	//创建套接字
	m_socket=socket(AF_INET,SOCK_DGRAM,0);
	CString temp_port;
	GetDlgItemText(IDC_EDIT_LocalPort,temp_port);
	m_port=atoi(temp_port);
	if(INVALID_SOCKET==m_socket)
	{
		MessageBox("套接字创建失败!");
		return false;
	}
	SOCKADDR_IN addrSock;
	addrSock.sin_family=AF_INET;
	addrSock.sin_port=htons(m_port);
	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 CChatRoomDlg::RecvProc(LPVOID lpParameter)
{
	//获取主线程传递得套接字和窗口句柄
	SOCKET sock=((RECVPARAM*)lpParameter)->sock;
	HWND hwnd=((RECVPARAM*)lpParameter)->hwnd;
	delete lpParameter;

	SOCKADDR_IN addrFrom;
	int len=sizeof(SOCKADDR);

	char revBuf[200];
	char tempBuf[300];
	int retval;

	while(true)
	{
		//接收数据
		retval=recvfrom(sock,revBuf,200,0,(SOCKADDR*)&addrFrom,&len);
		if(SOCKET_ERROR==retval)
			break;
		sprintf(tempBuf,"%s说:%s",inet_ntoa(addrFrom.sin_addr),revBuf);
		::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempBuf);
		
	}
	return 0;
		/*revBuff=revBuf;
		GetDlgItemText(IDC_EDIT_Rev,m_rev);
		m_rev+="\r\n";
		m_rev+=revBuff.Left(10);
		revRight=revBuf+10;
		m_rev+="\r\n";
		m_rev+=revRight;
		SetDlgItemText(IDC_EDIT_Rev,m_rev);*/
}

void CChatRoomDlg::OnRecvData(WPARAM wParam,LPARAM lParam)
{
//	CString str_name;
//	CString str_rest;
	CString strTemp;

//	char* strchar=(char*)lParam;
	CString str=(char*)lParam;

//	str_name=str.Left(10);
//	str_rest=strchar+10;

	GetDlgItemText(IDC_EDIT_Rev,strTemp);
//	str+=strTemp;
//	str.Format("%s\r\n%s:\r\n%s",str,str_name,str_rest);
//	SetDlgItemText(IDC_EDIT_Rev,str);

	str+="\r\n";
	str+=strTemp;
	SetDlgItemText(IDC_EDIT_Rev,str);
}

⌨️ 快捷键说明

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