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

📄 myqqclientdlg.cpp

📁 实现了一个聊天系统的客户端
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MyQQClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyQQClient.h"
#include "MyQQClientDlg.h"
#include "RegisterDlg.h"
#include "FindDlg.h"
#include "SendRecvDlg.h"
#include "SetServerDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyQQClientDlg dialog
static CONTENT_ITEMS ContentInfo[64] =
{
    {IDB_BITMAP1, "nickname"},
    {IDB_BITMAP2, "nickname"},
    {IDB_BITMAP3, "nickname"},
    {IDB_BITMAP4, "nickname"},
    {IDB_BITMAP5, "nickname"},
    {IDB_BITMAP6, "nickname"},
    {IDB_BITMAP7, "nickname"},
    {IDB_BITMAP8, "nickname"},
};

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

	m_IsConnected = FALSE;
	m_IsRegister = 0;
	m_IsDownUserInfo = 0;
	m_iCount = 0;

	sprintf(serverAddr,"%s","59.64.7.48");
	serverPort = 8000;
}

void CMyQQClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyQQClientDlg)
	DDX_Control(pDX, IDC_STRANGERLIST_BTN, m_btnStrangerList);
	DDX_Control(pDX, IDC_FRIENDLIST_BTN, m_btnFriendList);
	DDX_Control(pDX, IDC_BLACKLIST_BTN, m_btnBlackList);
	DDX_Control(pDX, IDC_MENU, m_btnMenu);
	DDX_Control(pDX, IDC_MESSAGE, m_btnMessage);
	DDX_Control(pDX, IDC_FIND, m_btnFind);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyQQClientDlg, CDialog)
	//{{AFX_MSG_MAP(CMyQQClientDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_FIND, OnFind)
	ON_BN_CLICKED(IDC_MESSAGE, OnMessage)
	ON_BN_CLICKED(IDC_FRIENDLIST_BTN, OnFriendlistBtn)
	ON_BN_CLICKED(IDC_STRANGERLIST_BTN, OnStrangerlistBtn)
	ON_BN_CLICKED(IDC_BLACKLIST_BTN, OnBlacklistBtn)
	ON_BN_CLICKED(IDC_MENU, OnMenu)
	ON_COMMAND(ID_SETSERVER, OnSetserver)
	ON_COMMAND(IDC_LOGOUT, OnLogout)
	ON_COMMAND(IDC_REVISEINFO, OnReviseinfo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyQQClientDlg message handlers

BOOL CMyQQClientDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	memset(m_userInfo.m_infoFriendList,0,sizeof(UINT)*64);
	memset(m_userInfo.m_infoBlackList,0,sizeof(UINT)*64);

	m_lstBlackListCtrl.SubclassDlgItem(IDC_BLACKLIST_CTRL, this);
	m_lstBlackListCtrl.SetItems(NULL, sizeof(ContentInfo) / sizeof(CONTENT_ITEMS));
	m_lstBlackListCtrl.SetParent(this);

    m_lstFriendListCtrl.SubclassDlgItem(IDC_FRIENDLIST_CTRL, this);
	m_lstFriendListCtrl.SetItems(NULL, sizeof(ContentInfo) / sizeof(CONTENT_ITEMS));
	m_lstFriendListCtrl.SetParent(this);

    m_lstStrangerListCtrl.SubclassDlgItem(IDC_STRANGERLIST_CTRL, this);
	m_lstStrangerListCtrl.SetItems(NULL, sizeof(ContentInfo) / sizeof(CONTENT_ITEMS));
	m_lstStrangerListCtrl.SetParent(this);

	ReadServerInfo();

	ChangeFolder(0);

	m_clientSocket.SetParent(this);

	if(m_IsRegister) {
		SetWindowText("正在连接...");

		SetListEnable(FALSE);

		if(ConnectToServer()) {
			SetWindowText("连接成功!");
		}
		else {
			SetWindowText("连接失败!");
			//CDialog::OnOK();
		}
	}
	else {
		SetWindowText("正在登陆...");

		SetListEnable(FALSE);

		if(ConnectToServer()) {
			SetWindowText("连接成功!");
		}
		else {
			SetWindowText("连接失败!");
			//CDialog::OnOK();
		}
	}

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

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

void CMyQQClientDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CMyQQClientDlg::OnCancel() 
{
	if(m_IsConnected) {
		OnLogout();
	}
	
	CDialog::OnCancel();
}

void CMyQQClientDlg::ChangeFolder(int piFolder)
{
	CRect rectDlg,rectBtn,rectNew;

	GetClientRect(&rectDlg);
	m_btnFriendList.GetClientRect(&rectBtn);

	switch (piFolder)
	{
		case 0:
			rectNew.left  = rectDlg.left;
			rectNew.top   = rectDlg.top + rectBtn.Height();
			rectNew.right = rectDlg.right;
			rectNew.bottom= rectDlg.bottom - rectBtn.Height() * 3;
			m_lstFriendListCtrl.MoveWindow(rectNew,TRUE);

			rectNew.left  = rectDlg.left;
			rectNew.top   = rectDlg.bottom - rectBtn.Height() * 3;
			rectNew.right = rectDlg.right;
			rectNew.bottom= rectDlg.bottom - rectBtn.Height() * 2;
			m_btnStrangerList.MoveWindow(rectNew,TRUE);

			rectNew.left  = rectDlg.left;
			rectNew.top   = rectDlg.bottom - rectBtn.Height() * 2;
			rectNew.right = rectDlg.right;
			rectNew.bottom= rectDlg.bottom - rectBtn.Height() * 1;
			m_btnBlackList.MoveWindow(rectNew,TRUE);

			m_lstFriendListCtrl.ShowWindow(SW_SHOW);
			m_lstStrangerListCtrl.ShowWindow(SW_HIDE);
			m_lstBlackListCtrl.ShowWindow(SW_HIDE);

			break;
		case 1:
			rectNew.left  = rectDlg.left;
			rectNew.top   = rectDlg.top + rectBtn.Height();
			rectNew.right = rectDlg.right;
			rectNew.bottom= rectDlg.top + rectBtn.Height() * 2;
			m_btnStrangerList.MoveWindow(rectNew,TRUE);

			rectNew.left  = rectDlg.left;
			rectNew.top   = rectDlg.top + rectBtn.Height() * 2;
			rectNew.right = rectDlg.right;
			rectNew.bottom= rectDlg.bottom - rectBtn.Height() * 2;
			m_lstStrangerListCtrl.MoveWindow(rectNew,TRUE);

			rectNew.left  = rectDlg.left;
			rectNew.top   = rectDlg.bottom - rectBtn.Height() * 2;
			rectNew.right = rectDlg.right;
			rectNew.bottom= rectDlg.bottom - rectBtn.Height() * 1;
			m_btnBlackList.MoveWindow(rectNew,TRUE);

			m_lstFriendListCtrl.ShowWindow(SW_HIDE);
			m_lstStrangerListCtrl.ShowWindow(SW_SHOW);
			m_lstBlackListCtrl.ShowWindow(SW_HIDE);

			break;
		case 2:
			rectNew.left  = rectDlg.left;
			rectNew.top   = rectDlg.top + rectBtn.Height();
			rectNew.right = rectDlg.right;
			rectNew.bottom= rectDlg.top + rectBtn.Height() * 2;
			m_btnStrangerList.MoveWindow(rectNew,TRUE);

			rectNew.left  = rectDlg.left;
			rectNew.top   = rectDlg.top + rectBtn.Height() * 2;
			rectNew.right = rectDlg.right;
			rectNew.bottom= rectDlg.top + rectBtn.Height() * 3;
			m_btnBlackList.MoveWindow(rectNew,TRUE);

			rectNew.left  = rectDlg.left;
			rectNew.top   = rectDlg.top + rectBtn.Height() * 3;
			rectNew.right = rectDlg.right;
			rectNew.bottom= rectDlg.bottom - rectBtn.Height() * 1;
			m_lstBlackListCtrl.MoveWindow(rectNew,TRUE);

			m_lstFriendListCtrl.ShowWindow(SW_HIDE);
			m_lstStrangerListCtrl.ShowWindow(SW_HIDE);
			m_lstBlackListCtrl.ShowWindow(SW_SHOW);

			break;
	}

}

void CMyQQClientDlg::OnMessage() 
{
	// TODO: Add your control notification handler code here

}

void CMyQQClientDlg::OnFind() 
{
	// TODO: Add your control notification handler code here
	CFindDlg findDlg;
	if(findDlg.DoModal() == IDOK) {
		GetUserInfo(findDlg.m_nID);
		m_IsDownUserInfo = 1;
	}
}

void CMyQQClientDlg::OnReceive()
{
	m_clientSocket.Receive(&m_msg,sizeof(SClientMsg));

	switch (m_msg.m_msgType)
	{
		case 0:		//服务器关闭
			OnServerClose();
			break;
		case 1:		//连线成功
			if(!m_IsRegister) {	//用户登陆
				LogIn();
			}
			else	//用户注册
			{
				RegisterUserInfo();
			}
			break;
		case 2:		//下线成功
			m_IsConnected = FALSE;
			m_clientSocket.Close();

			SetWindowText("下线成功!");
			break;
		case 3:		//保持连接
			//
			break;
		case 4:		//系统消息
			//
			break;
		case 5:		//注册成功
			OnRegisterFinished();
			break;
		case 6:
			MessageBox("查无此ID!");
			break;
		case 7:
			MessageBox("密码错误!");
			break;
		case 8:		//登陆成功
			memcpy(&m_userInfo,m_msg.m_msgBuff,1024);
			OnLogInFinished();

			DownLoadList();

			break;
		case 9:		//查询用户信息
			if(m_IsDownUserInfo == 1) {
				SUserInfo * info = (SUserInfo*)(m_msg.m_msgBuff);
				OnGetUserInfo(*info);
			}
			else if(m_IsDownUserInfo == 2) {
				m_IsDownUserInfo = 3;
				DownLoadList();

⌨️ 快捷键说明

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