📄 chatdlg.cpp
字号:
// CHATDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CHAT.h"
#include "CHATDlg.h"
#include<afxsock.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)
count=0; // NOTE: the ClassWizard will add member initialization here
//}}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, IDSEND, m_send);
DDX_Control(pDX, IDOK3, m_disconnect);
DDX_Control(pDX, IDCONNECT, m_connect);
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Control(pDX, IDC_EDIT1, m_edit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCHATDlg, CDialog)
//{{AFX_MSG_MAP(CCHATDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDSEND, OnSend)
ON_BN_CLICKED(IDCONNECT, OnConnect)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCHATDlg message handlers
BOOL CCHATDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
UINT CCHATDlg::thread(LPVOID v)
{
char buff[100];
char array[25][30]={
"219.245.25.16"
};
CSize size;
size.cx=0;
size.cy=30;
int s=1,addcount=0;
CCHATDlg *dlg=(CCHATDlg *) AfxGetApp()->GetMainWnd();
dlg->m_connect.EnableWindow(FALSE);
dlg->m_disconnect.EnableWindow(TRUE);
while(connect(dlg->clisock,(sockaddr*)&(dlg->cli),sizeof(dlg->cli))&&dlg->ee!=0)
{
dlg->m_edit.SetWindowText("等待.....");
for(int i=0;i<=65000;i++)
for(int j=0;j<=200;j++)
if(addcount==25)
addcount=0;
dlg->cli.sin_addr.s_addr=inet_addr(array[addcount++]);
}
if(dlg->ee==1)
dlg->m_list.InsertItem(dlg->count++,"连接成功");
dlg->m_edit.SetWindowText("");
dlg->m_send.EnableWindow(TRUE);
dlg->SetForegroundWindow();
while(s!=SOCKET_ERROR&&dlg->ee!=0)
{
s=recv(dlg->clisock,buff,100,0);
dlg->SetForegroundWindow();
if(s!=SOCKET_ERROR&&dlg->ee!=0)
{
dlg->m_list.InsertItem(dlg->count++,"服务器:");
dlg->m_list.InsertItem(dlg->count++,buff);
}
dlg->m_list.Scroll(size);
}
send(dlg->clisock,"Disconnected",100,0);
dlg->m_send.EnableWindow(FALSE);
dlg->m_connect.EnableWindow(TRUE);
dlg->m_disconnect.EnableWindow(FALSE);
closesocket(dlg->clisock);
AfxEndThread(0);
return 0;
}
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::OnSend()
{
char buff[100];
CSize size;
size.cx=0;
size.cy=0;
m_edit.GetWindowText(buff,99);
m_edit.SetWindowText("");
m_list.InsertItem(count++,"本人:");
m_list.InsertItem(count++,buff);
m_list.Scroll(size);
send(clisock,buff,100,0);// TODO: Add your control notification handler code here
}
void CCHATDlg::OnConnect()
{
char ipaddress[35];
m_edit.GetWindowText(ipaddress,30);
cli.sin_addr.s_addr=inet_addr(ipaddress);
cli.sin_family=AF_INET;
cli.sin_port=5000;
clisock=socket(AF_INET,SOCK_STREAM,0);
ee=1;
AfxBeginThread(thread,0); // TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -