• 大小: 33KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: C/C++
  • 标签: MFC  TCP通信  聊天室  

资源简介

MFC程序开发之聊天室(客户端+服务器)源码 适合初学者参考

资源截图

代码片段和文件信息

// LogonDlg.cpp : 实现文件
//

#include “stdafx.h“
#include “Resource.h“
#include “LogonDlg.h“


// CLogonDlg 对话框

IMPLEMENT_DYNAMIC(CLogonDlg CDialog)

CLogonDlg::CLogonDlg(CWnd* pParent /*=NULL*/)
: CDialog(IDD_LOGON pParent)
{

}

CLogonDlg::~CLogonDlg()
{
}

void CLogonDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CLogonDlg CDialog)
ON_BN_CLICKED(IDC_LOGON &CLogonDlg::OnBnClickedLogon)
END_MESSAGE_MAP()


// CLogonDlg 消息处理程序

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

//限制字符
((CEdit*)GetDlgItem(IDC_USERNAME))->LimitText(32);
((CEdit*)GetDlgItem(IDC_PASSWORD))->LimitText(32);

//服务器地址
TCHAR szServerAddr[33] = TEXT(“127.0.0.1“);

CT2CA CurrentServer(szServerAddr);
DWORD dwServerAddrIP=inet_addr(CurrentServer);
if (dwServerAddrIP==INADDR_NONE)
{
LPHOSTENT lpHost=gethostbyname(CurrentServer);
if (lpHost!=NULL) dwServerAddrIP=((LPIN_ADDR)lpHost->h_addr)->s_addr;
}

CIPAddressCtrl * pServerIP=(CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS);
pServerIP->SetAddress(ntohl(dwServerAddrIP));

//默认端口
SetDlgItemInt(IDC_PORT1038);

//默认用户名
SetDlgItemInt(IDC_USERNAMEGetTickCount());
//默认密码
SetDlgItemText(IDC_PASSWORDTEXT(“123456“));

return TRUE;  // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}

void CLogonDlg::OnBnClickedLogon()
{
tagLogonInfo *pLogonInfo = new tagLogonInfo;

//设置ip
DWORD dwLogonIP=INADDR_NONE;
BYTE * pAddrByte=(BYTE *)&dwLogonIP;
((CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS))->GetAddress(dwLogonIP);
_sntprintf_s(pLogonInfo->szServerAddrCountArray(pLogonInfo->szServerAddr)TEXT(“%d.%d.%d.%d“)pAddrByte[3]pAddrByte[2]pAddrByte[1]pAddrByte[0]);

pLogonInfo->nPort = GetDlgItemInt(IDC_PORT);

GetDlgItemText(IDC_USERNAMEpLogonInfo->szUserNameCountArray(pLogonInfo->szUserName));
GetDlgItemText(IDC_PASSWORDpLogonInfo->szPassWordCountArray(pLogonInfo->szPassWord));

//发送消息
AfxGetMainWnd()->PostMessage(WM_USER_LOGON(WPARAM)pLogonInfo);
}


评论

共有 条评论