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

📄 clientdlg.cpp

📁 c++编写的局域网群聊系统
💻 CPP
字号:
// ClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog

CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CClientDlg)
	m_strDisplay = _T("");
	m_strSend = _T("");
	m_bWhispering = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClientDlg)
	DDX_Control(pDX, IDC_LIST_USER, m_lbUser);
	DDX_Text(pDX, IDC_EDIT_DIAPLAY, m_strDisplay);
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
	DDX_Check(pDX, IDC_CHECK_WHISPERING, m_bWhispering);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
	//{{AFX_MSG_MAP(CClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BN_CONNECT, OnBnConnect)
	ON_BN_CLICKED(IDC_BN_CLOSE, OnBnClose)
	ON_BN_CLICKED(IDC_BN_SEND, OnBnSend)
	ON_MESSAGE(WM_RECEIVE, OnReceive)
	ON_MESSAGE(WM_SOCKETCLOSE, OnServerClose)
	ON_MESSAGE(WM_SEND, OnSend)
	ON_MESSAGE(WM_CONNCET, OnConnect)
	ON_LBN_DBLCLK(IDC_LIST_USER, OnDblclkListUser)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers

BOOL CClientDlg::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
	
	// 参数初始化
	m_Socket.Initialize(this);
	m_strNetIP = _T("127.0.0.1");
	m_iNetPort = 3127;
	m_bConnect = FALSE;

	GetDlgItem(IDC_BN_CLOSE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BN_SEND)->EnableWindow(FALSE);

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

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

void CClientDlg::OnBnConnect() 
{
	CString strConnectError;
	// 获取用户名字
	CInputDlg inputDlg;
	int iResult = inputDlg.DoModal();
	if(iResult==IDCANCEL){return;}

	m_strName = inputDlg.m_strName;

	// 创建套接字
	if(!m_Socket.Create())
	{
		strConnectError.LoadString(IDS_FAIL_CREATE_SOCKET);
		ShowInDisplay(strConnectError);

		return;
	}

	CString strInfo;
	strInfo.LoadString(IDS_CONNECTING);
	ShowInDisplay(strInfo);

	// 连接服务器
//	m_Socket.Connect(m_strNetIP, m_iNetPort);
	if(!m_Socket.Connect(m_strNetIP, m_iNetPort))
	{
		
		int n = m_Socket.GetLastError();
		strConnectError.LoadString(IDS_FAIL_CONNECT);
		ShowInDisplay(strConnectError);

		return;
	}
	
	strInfo.LoadString(IDS_SUCCEED_CONNECT);
	ShowInDisplay(strInfo);

	m_bConnect = TRUE;

	// 发送自已的名字
	NETMESSAGE netMessage(PTC_NEW, m_strName, _T(""), _T(""));
	m_Socket.SendMsg(netMessage);

	GetDlgItem(IDC_BN_CONNECT)->EnableWindow(FALSE);
	GetDlgItem(IDC_BN_CLOSE)->EnableWindow(TRUE);
	GetDlgItem(IDC_BN_SEND)->EnableWindow(TRUE);
}

void CClientDlg::OnBnClose() 
{
	// TODO: Add your control notification handler code here
	if(m_bConnect) 
	{
		m_Socket.Close();
		m_bConnect = FALSE;

		GetDlgItem(IDC_BN_CONNECT)->EnableWindow(TRUE);
		GetDlgItem(IDC_BN_CLOSE)->EnableWindow(FALSE);
		GetDlgItem(IDC_BN_SEND)->EnableWindow(FALSE);
		m_lbUser.ResetContent();

		CString strMsg;
		strMsg.LoadString(IDS_BREAK);
		ShowInDisplay(strMsg);
	}
}

void CClientDlg::OnBnSend() 
{
	// TODO: Add your control notification handler code here
	OnSend();

	m_strSend = _T("");
	UpdateData(FALSE);
}

void CClientDlg::OnReceive()
{
	NETMESSAGE netMessage;
	m_Socket.Receive((char *)&netMessage, sizeof(netMessage));

	CString strForm = netMessage.form;

	// 过滤自已发出的信息
	if(strForm==m_strName) return;

	CString strTo = netMessage.to;
	CString strMsg = netMessage.data;

	switch(netMessage.type)
	{
		case PTC_USER_LIST:				// 用户列表

			m_lbUser.AddString(strMsg);

			break;

		case PTC_NEW:					// 新用户进入
			
			m_lbUser.AddString(strForm);
			ShowInDisplay(strMsg);

			break;
		
		case PTC_SAY:

			ShowMessage(netMessage);

		case PTC_WHISPERING:			// 密语
			
			if(strTo!=m_strName) return;
			ShowMessage(netMessage);

			break;

		case PTC_CLIENT_QUIT:			// 有用户退出

			m_lbUser.DeleteString(m_lbUser.FindString(-1, strForm));
			ShowInDisplay(strMsg);

			break;
	}
}

void CClientDlg::OnServerClose()
{
	if(!m_bConnect) return;

	CString strMsg;
	strMsg.LoadString(IDS_SERVER_CLOSE);
	ShowInDisplay(strMsg);

	m_Socket.Close();
	m_bConnect = FALSE;

	GetDlgItem(IDC_BN_CONNECT)->EnableWindow(TRUE);
	GetDlgItem(IDC_BN_CLOSE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BN_SEND)->EnableWindow(FALSE);
	m_lbUser.ResetContent();
}

void CClientDlg::OnSend()
{
	UpdateData(TRUE);

	// 主要是填充NETMESSAGE结构体
	NETMESSAGE netMessage;
	strcpy(netMessage.form, m_strName);
	strcpy(netMessage.data, m_strSend);

	// 取得列表框中的用户
	if(m_lbUser.GetText(m_lbUser.GetCurSel(), netMessage.to)==LB_ERR)
	{
		strcpy(netMessage.to, _T(""));
	}

	if(m_bWhispering)
	{
		// 密语
		CString strTo = netMessage.to;
		if(strTo==_T("")) 
		{
			MessageBox("请选择对方!", "错误");
			return;
		}

		netMessage.type = PTC_WHISPERING;
	}
	else
	{
		strcpy(netMessage.to, _T("所有人"));
		netMessage.type = PTC_SAY;
	}

	// 发送该信息
	m_Socket.SendMsg(netMessage);

	// 显示发送信息
	CString strTo = netMessage.to;
	CString strMsg = netMessage.data;
	ShowInDisplay("你对" + strTo + "说:" + strMsg);
}

void CClientDlg::ShowInDisplay(CString str)
{
	m_strDisplay += str + "\r\n";
	UpdateData(FALSE);
}

void CClientDlg::ShowMessage(const NETMESSAGE &netMessage)
{
	CString strTo = netMessage.to;
	if(strTo==_T(""))
	{
		strTo = "所有人";
	}
	else if(strTo==m_strName)
	{
		strTo = "你";
	}
	
	CString strForm = netMessage.form;
	CString strMsg = netMessage.data;
	
	CString strOut = strForm + "对" + strTo + "说:" + strMsg;
	ShowInDisplay(strOut);
}

void CClientDlg::OnDblclkListUser() 
{
	// TODO: Add your control notification handler code here
	m_lbUser.SetCurSel(-1);
}

void CClientDlg::OnConnect()
{
	m_bConnect = TRUE;
}

⌨️ 快捷键说明

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