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

📄 myqqdlg.cpp

📁 实现了qq的基本功能!buaasunjian
💻 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_nCastType = -1;
	m_strHistory = _T("");
	m_strMessage = _T("");
	//}}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_Radio(pDX, IDC_RADIO_Broadcast, m_nCastType);
	DDX_Text(pDX, IDC_EDIT_History, m_strHistory);
	DDX_Text(pDX, IDC_EDIT_Message, m_strMessage);
	//}}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_Send, OnBUTTONSend)
	ON_BN_CLICKED(IDC_RADIO_Broadcast, OnRADIOBroadcast)
	ON_BN_CLICKED(IDC_RADIO_Unicast, OnRADIOUnicast)
	ON_WM_TIMER()
	//}}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
	// 获得本机的IP
	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));

	// 初始化socket
	if( m_socket.Create(m_constPort, SOCK_DGRAM, m_strLocalIP) == FALSE)
	{
		// 如果初始化失败
		AfxMessageBox("初始化socket失败"); // 显示提示对话框
		OnCancel(); // 退出程序
	}
	
	// 设置m_socket属性,允许广播
	BOOL bBroadcast = TRUE;
	m_socket.SetSockOpt(SO_BROADCAST, &bBroadcast, sizeof(BOOL));

	// 初始化单选按钮,设置为广播
	m_nCastType = 0;

	// 初始化目标主机的IP
	m_remoteAddr.SetAddress(0);

	// 由于初始化为广播方式,所以将IP控件设为不可用
	m_remoteAddr.EnableWindow(FALSE);

	// 将输入焦点设置到消息编辑框中
	((CEdit*)GetDlgItem(IDC_EDIT_Message))->SetFocus();

	// 设置定时器,每1000毫秒发生一次定时器事件
	SetTimer(1, 1000, NULL);

	// 刷新各个控件的外观
	UpdateData(FALSE);

	return FALSE; // 返回FALSE表示焦点在某一个控件上
}

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::OnBUTTONSend() 
{
	UpdateData(TRUE); // 将控件的状态保存到各自相关联的变量中

	// 如果消息为空,立刻返回
	if(m_strMessage.IsEmpty())
		return;

	// 发送消息
	CString strIP;
	if(m_nCastType == 0)
	{
		// 广播
		m_socket.SendTo(
			LPCTSTR(m_strMessage), // 数据报内容
			m_strMessage.GetLength(), // 数据报长度
			m_constPort, // 对方的端口,本程序使用统一的端口
			NULL); // 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);
}

void CMyQQDlg::OnRADIOBroadcast()
{
	// 广播方式,不需要IP地址控件
	m_remoteAddr.EnableWindow(FALSE);
}

void CMyQQDlg::OnRADIOUnicast()
{
	// 单播方式,需要使用IP地址控件给出地址
	m_remoteAddr.EnableWindow(TRUE);
}

void CMyQQDlg::OnTimer(UINT nIDEvent) 
{
	// 得到控件中的内容
	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)
	{
		// 从socket上接收数据
		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);
}

⌨️ 快捷键说明

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