📄 chatdlg.cpp
字号:
// chatDlg.cpp : implementation file
//
#include "stdafx.h"
#include "chat.h"
#include "chatDlg.h"
#include "login.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()
/////////////////////////////////////////////////////////////////////////////
// CChatDlg dialog
CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChatDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChatDlg)
m_send = _T("");
m_read = _T("");
m_me = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChatDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChatDlg)
DDX_Control(pDX, IDC_LIST1, m_list1);
DDX_Control(pDX, IDC_COMBO1, m_list);
DDX_Text(pDX, IDC_EDIT_SEND, m_send);
DDX_Text(pDX, IDC_EDIT_RECV, m_read);
DDX_Text(pDX, IDC_EDIT3, m_me);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
//{{AFX_MSG_MAP(CChatDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_SOCKET_READ, OnRead)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatDlg message handlers
BOOL CChatDlg::OnInitDialog()
{ //初始化登陆过程
CDialog::OnInitDialog();
login dlg;
int nResponse=dlg.DoModal();
if (nResponse == IDOK)
{
unsigned long ulAddress;
SOCKADDR_IN sin;
ulAddress=inet_addr(dlg.m_ip);//字符型服务器地址
m_name=dlg.m_name;
//连接服务器
sockServer=socket(AF_INET,SOCK_STREAM,0);
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=ulAddress;
sin.sin_port=htons(4009);
int nConnect=connect(sockServer, (LPSOCKADDR)&sin, sizeof(struct sockaddr));
// 通知套接口有请求事件发生
WSAAsyncSelect(sockServer,m_hWnd,WM_SOCKET_READ,FD_READ);
if(nConnect== -1)
{
MessageBox("连接过程发生错误!\n请确保IP输入正确无误!",NULL,MB_OK);
CDialog::OnCancel();
}
else
{
MessageBox("与服务器建立连接成功!",NULL,MB_OK);
info myinfo;
myinfo.type=0;
LPCTSTR p=m_name;
memcpy(myinfo.name,p,m_name.GetLength()+1);
//发送数据
int nCharSend=send(sockServer,(char*)&myinfo,sizeof(myinfo),0);
if(nCharSend==SOCKET_ERROR)
MessageBox("发送过程中发生一个错误!",NULL,MB_OK);
}
}
//初始化取消过程
if (nResponse == IDCANCEL)
{
CDialog::OnCancel();
}
// 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
return TRUE; // return TRUE unless you set the focus to a control
}
void CChatDlg::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 CChatDlg::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 CChatDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChatDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CChatDlg::OnOK()
{ //显示登陆的时间
CTime t=CTime::GetCurrentTime();
CString strTime="(%m-%d %H:%M:%S)";
strTime=t.Format(strTime);
UpdateData();
info sendinfo;
//给XX发送信息
m_list.GetLBText(m_list.GetCurSel(),toname);
if(toname=="大家") //发送公共信息
{
sendinfo.type=1;
m_read=m_read+"\r\n"+strTime+"你对大家说:"+m_send;
m_send=strTime+m_name+"对大家说:"+m_send;
}
else
{
sendinfo.type=2; //给某人发信息
LPCTSTR q=toname;
memcpy(sendinfo.name,q,toname.GetLength()+1);
m_read=m_read+"\r\n"+strTime+"你对"+toname+"说:"+m_send;
m_send=strTime+m_name+"对你说:"+m_send;
}
LPCTSTR s=m_send;
memcpy(sendinfo.msg,s,m_send.GetLength()+1);
//发送信息
int nCharSend=send(sockServer,(char*)&sendinfo,sizeof(sendinfo),0);
if(nCharSend==SOCKET_ERROR)
MessageBox("发送过程中发生一个错误!",NULL,MB_OK);
m_send="";
UpdateData(FALSE);
GetDlgItem(IDC_EDIT_SEND)->SetFocus();
}
LRESULT CChatDlg::OnRead(WPARAM wParam, LPARAM lParam)
{
int BReads;
info reinfo;
//接受数据
BReads = recv(sockServer, (char*)&reinfo, sizeof(reinfo), 0);
if (BReads == SOCKET_ERROR)
MessageBox("接收到一个错误信息!");
switch(reinfo.type)
{
case 1: //对大家
m_read=m_read+"\r\n"+reinfo.msg;
UpdateData(FALSE);
break;
case 2: //对某人
m_read=m_read+"\r\n"+reinfo.msg;
UpdateData(FALSE);
break;
case 0: //添加新进入者
m_list.AddString(reinfo.name);
m_list1.AddString(reinfo.name);
m_read=m_read+"\r\n"+reinfo.msg;
UpdateData(FALSE);
break;
case 3: //删除退出者
m_list.DeleteString(m_list.FindString(1,reinfo.name));
m_list1.DeleteString(m_list1.FindString(1,reinfo.name));
break;
}
CEdit *pEdit;
pEdit=(CEdit *)GetDlgItem(IDC_EDIT_RECV);
int i=pEdit->GetLineCount();
pEdit->LineScroll(i,0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -