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

📄 netqqdlg.cpp

📁 类似于QQ的聊天工具,分为客户端和服务器端,有共享空间,能发布公告,可传输文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// NetQQDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NetQQ.h"
#include "NetQQDlg.h"
#include "MainDlg.h"
#include "InitRegDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CNetQQDlg dialog

CNetQQDlg::CNetQQDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNetQQDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNetQQDlg)
	m_strUserId = _T("");
	m_strPassword = _T("");
	m_strPort = _T("");
	m_hmainWndDlg = NULL;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_MAIN);

}

void CNetQQDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNetQQDlg)
	DDX_Control(pDX, IDC_STC_SHOWFACE, m_stcShowFace);
	DDX_Control(pDX, IDC_CB_FACE, m_cbFace);
	DDX_Text(pDX, IDC_EDIT_UID, m_strUserId);
	DDV_MaxChars(pDX, m_strUserId, 9);
	DDX_Text(pDX, IDC_EDIT_PSW, m_strPassword);
	DDV_MaxChars(pDX, m_strPassword, 9);
	DDX_Text(pDX, IDC_EDIT_PORT, m_strPort);
	DDV_MaxChars(pDX, m_strPort, 5);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNetQQDlg, CDialog)
	//{{AFX_MSG_MAP(CNetQQDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_CBN_SELCHANGE(IDC_CB_FACE, OnSelchangeCbFace)
	ON_BN_CLICKED(IDC_BTN_LOAD, OnBtnLoad)
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_LOGIN, OnBtnLogin)
	ON_BN_CLICKED(IDC_BTN_EXIT, OnBtnExit)
	ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
	ON_BN_CLICKED(IDC_BTN_MIN, OnBtnMin)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_BN_CLICKED(IDC_BTN_L, OnBtnL)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_NETEVENT, OnNetEvent)
	ON_MESSAGE(WM_LOADINMAINDLG, OnLoadInMainDlg)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetQQDlg message handlers

BOOL CNetQQDlg::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
	m_cbFace.SetCurSel(0);
	m_stcShowFace.SetIcon(AfxGetApp()->LoadIcon(IDI_FACE0));
	
	DWORD dwIp = htonl(inet_addr("127.0.0.1"));
	((CIPAddressCtrl*)GetDlgItem(IDC_IPADDR_SVR))->SetAddress(dwIp);
	GetDlgItem(IDC_EDIT_UID)->SetFocus();
	m_myButtonLoad.AutoLoad(IDC_BTN_LOAD,this);
	m_myButtonLogin.AutoLoad(IDC_BTN_LOGIN,this);
	m_myButtonExit.AutoLoad(IDC_BTN_EXIT,this);
	m_myButtonClose.AutoLoad(IDC_BTN_CLOSE,this);
	m_myButtonMin.AutoLoad(IDC_BTN_MIN,this);

	//////////////////////////////////////////
	//创建一个匿名互斥对象来保证线程添加、删除
	//用户链表结点时的同步 [Add] dong 12-17
	m_hMutex = CreateMutex(NULL, FALSE, NULL);

	m_ttToolTip.CreateToolTip(this);
	return FALSE;  // return TRUE  unless you set the focus to a control
}

void CNetQQDlg::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 CNetQQDlg::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
	{
		CRgn  m_rgn;
		CRect rcWnd;
		GetWindowRect(rcWnd);
		m_rgn.m_hObject = CreateRoundRectRgn(0,0,rcWnd.Width()+1, rcWnd.Height()+1,16, 16 );
		this->SetWindowRgn(m_rgn,true);
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNetQQDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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

void CNetQQDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	//CDialog::OnCancel();
}


void CNetQQDlg::OnSelchangeCbFace() 
{
	// TODO: Add your control notification handler code here
	UINT nFaceID[] = {IDI_FACE0, IDI_FACE1, IDI_FACE2, IDI_FACE3, IDI_FACE4};
	int nIndex = m_cbFace.GetCurSel();
	if(nIndex == CB_ERR)
	{
		m_stcShowFace.SetIcon(AfxGetApp()->LoadIcon(nFaceID[0]));
	}
	else
	{
		m_stcShowFace.SetIcon(AfxGetApp()->LoadIcon(nFaceID[nIndex]));
	}
}


void CNetQQDlg::OnBtnLoad() 
{
	// TODO: Add your control notification handler code here
	CStatic* pTmp = (CStatic*)GetDlgItem(IDC_STC_LOADMSG);
	/////////////////////////////////////////////////////
	//获取用户输入的帐号并检验
	GetDlgItem(IDC_EDIT_UID)->GetWindowText(m_strUserId);
	if(m_strUserId.IsEmpty())
	{
		pTmp->SetWindowText("请输入登陆帐号!");
		GetDlgItem(IDC_EDIT_UID)->SetFocus();
		return;
	}

	///////////////////////////////////////////////////////
	//获取用户输入的密码并检验
	GetDlgItem(IDC_EDIT_PSW)->GetWindowText(m_strPassword);
	if(m_strPassword.IsEmpty())
	{
		pTmp->SetWindowText("请输入密码!");
		GetDlgItem(IDC_EDIT_PSW)->SetFocus();
		return;
	}
	
	///////////////////////////////////////////////////////////////////
	//获取用户输入的IP地址并检验
	((CIPAddressCtrl*)GetDlgItem(IDC_IPADDR_SVR))->GetAddress(m_dwIp);
	if(m_dwIp == 0)
	{
		pTmp->SetWindowText("请输入服务器IP地址!");
		((CIPAddressCtrl*)GetDlgItem(IDC_IPADDR_SVR))->SetFieldFocus(0);
		return;
	}
	///////////////////////////////////////////////////////////////////
	//获取用户输入端口号
	GetDlgItem(IDC_EDIT_PORT)->GetWindowText(m_strPort);
	if(m_strPort.IsEmpty())
	{
		pTmp->SetWindowText("请设置端口!");
		GetDlgItem(IDC_EDIT_PORT)->SetFocus();
		return;
	}
	
	////////////////////////////////////////////////////////////////////
	//获取头像编号
	int nFaceIndex       = m_cbFace.GetCurSel();
	CString strFaceIndex;
	strFaceIndex.Format("%d", nFaceIndex);

	///////////////////////////////////////////////////////////////////
	//开始创建客户端
	short snPort = atoi(m_strPort.GetBuffer(0));
	if(!m_ClntSock.CreateClient(m_dwIp, snPort))
	{
		pTmp->SetWindowText("连接服务器失败!");
		return;
	}

	///////////////////////////////////////////////////////////////////
	//注册网络事件
	int ret = WSAAsyncSelect(m_ClntSock.GetSrvSocket(), m_hWnd, 
                              WM_NETEVENT, FD_CLOSE | FD_READ); 
	if(SOCKET_ERROR == ret)
	{
		pTmp->SetWindowText("网络事件注册失败!");
		m_ClntSock.CloseClient();
		return;
	}

	CString strFTLPort;
	// 开始监听端口 
	m_FT.InitFlieListenSock();
	WORD wPort = m_FT.GetFileListenPort();
	strFTLPort.Format("%u", wPort);

	////////////////////////////////////////
	//打包数据
	CMsgBag mbTool;
	CString strBuff;
	tagPacketParam pp;
	pp.order    = "1002";
	pp.UserId   = m_strUserId;
	pp.UserPsw  = m_strPassword;
	pp.UserSex  = strFaceIndex;
	pp.UserChar = strFTLPort;
	pp.paramNum = 4;
	mbTool.DataPackage(strBuff, &pp);

	
	////////////////////////////////////////
	//发送数据
	m_ClntSock.OnSend(strBuff);

	//AfxMessageBox("OK");
}


/*---------------------------------------------------------------------------
 网络事件回调函数
 --------------------------------------------------------------------------*/
void CNetQQDlg::OnNetEvent(WPARAM wParam, LPARAM lParam)
{
	//调用API函数,得到网络事件类型
    int iEvent = WSAGETSELECTEVENT(lParam); 

	switch(iEvent)
	{
		case FD_CLOSE:  //服务器端断开事件:
			{
				if(m_hmainWndDlg)
				{
					::PostMessage(m_hmainWndDlg, WM_MIDMSG, 0, 9998);
				}
				return;
			}
		case FD_READ:   //网络数据包到达事件
			{
				/*[Add] dong 12-17*/

⌨️ 快捷键说明

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