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

📄 myqqdlg.cpp

📁 聊天工具
💻 CPP
字号:
// 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

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

CMyQQDlg::CMyQQDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyQQDlg::IDD, pParent),m_constPort(3000)
{
	//{{AFX_DATA_INIT(CMyQQDlg)
	m_strHistory = _T("");
	m_strMessage = _T("");
	m_nCastType = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyQQDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyQQDlg)
	DDX_Control(pDX, IDC_IPADDRESS, m_remoteAddr);
	DDX_Text(pDX, IDC_EDIT_History, m_strHistory);
	DDX_Text(pDX, IDC_EDIT_Message, m_strMessage);
	DDX_Radio(pDX, IDC_RADIO_Broadcast, m_nCastType);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyQQDlg, CDialog)
	//{{AFX_MSG_MAP(CMyQQDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_CANCELMODE()
	ON_BN_CLICKED(IDC_RADIO_Unicast, OnRADIOUnicast)
	ON_BN_CLICKED(IDC_RADIO_Broadcast, OnRADIOBroadcast)
	ON_BN_CLICKED(IDC_BUTTON_Send, OnBUTTONSend)
	//}}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

	char hostname[100];
	gethostname(hostname,100);
	struct hostent* host=gethostbyname(hostname);
	if (host==NULL) 
		m_strLocalIP="0.0.0.0";
	else
			m_strLocalIP = inet_ntoa(*((in_addr*)host->h_addr));


	if (m_socket.Create(m_constPort,SOCK_DGRAM,m_strLocalIP)==FALSE)
	{
		AfxMessageBox("初始化socket失败");
		OnCancel();
	}

	BOOL bBroadcast=TRUE;
	m_socket.SetSockOpt(SO_BROADCAST,&bBroadcast,sizeof(BOOL));
	m_nCastType=0;
	m_remoteAddr.SetAddress(0);
	m_remoteAddr.EnableWindow(FALSE);
	((CEdit *)GetDlgItem(IDC_EDIT_Message))->SetFocus();
	SetTimer(1,1000,NULL);
	UpdateData(FALSE);
	
	// TODO: Add extra initialization here
	
	return FALSE;  // 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;
}

void CMyQQDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	UpdateData(TRUE);
	
	char buffer[1024]; // 接收数据缓冲区
	int recvSize = 0; // 接收字符计数器
	CString fromIP; // 对方IP
	UINT fromPort; // 对方端口
	CString strToAdd; // 加入到聊天记录中的字符串
	
	fd_set in_set = {0};
	FD_SET(m_socket.m_hSocket, &in_set);
	timeval t = {0,0};
	if(select(1, &in_set, NULL, NULL, &t) != 0)
	{
		
		recvSize = m_socket.ReceiveFrom(
			buffer, // 用于接收数据的缓冲区地址
			1024, // 缓冲区大小
			fromIP, // 源地址
			fromPort); // 源端口
		
		if( recvSize > 0 && fromIP != m_strLocalIP )
		{
			buffer[recvSize] = 0;

			// 组成一行记录
			strToAdd = "收到来自 ";
			strToAdd += fromIP;
			strToAdd += " 的消息:";
			strToAdd += buffer;
			strToAdd += "\r\n";

			// 加入到聊天记录中
			m_strHistory = strToAdd + m_strHistory;
			UpdateData(FALSE);
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CMyQQDlg::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}

void CMyQQDlg::OnRADIOUnicast() 
{
	// TODO: Add your control notification handler code here
	m_remoteAddr.EnableWindow(TRUE);
}

void CMyQQDlg::OnRADIOBroadcast() 
{
	// TODO: Add your control notification handler code here
	m_remoteAddr.EnableWindow(FALSE);
}

void CMyQQDlg::OnBUTTONSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_strMessage.IsEmpty())
		;
	CString strIP;
	if (m_nCastType==0)
	   m_socket.SendTo(LPCTSTR(m_strMessage),m_strMessage.GetLength(),m_constPort,NULL);
	else
	{
		BYTE addr[4]={0};
		m_remoteAddr.GetAddress(addr[0],addr[1],addr[2],addr[3]);
		strIP.Format("%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
		m_socket.SendTo(LPCTSTR(m_strMessage),m_strMessage.GetLength(),m_constPort,strIP);
	}
	CString strToAdd;
	if (m_nCastType==0) 
		strToAdd="你的广播:";
	else
	{
		strToAdd="你对 ";
		strToAdd+=strIP;
		strToAdd+="说:";
	}
	strToAdd+=m_strMessage;
	strToAdd+="\r\n";
	m_strHistory=strToAdd+m_strHistory;

	m_strMessage.Empty();
	UpdateData(FALSE);

}

⌨️ 快捷键说明

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