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

📄

📁 一个聊天的程序不错
💻
字号:
// 聊天程序Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "聊天程序.h"
#include "聊天程序Dlg.h"
//#include "winsock2.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

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

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_BUTTON3, m_send);
	DDX_Control(pDX, IDC_BUTTON2, m_connect);
	DDX_Control(pDX, IDC_BUTTON1, m_listen);
	DDX_Text(pDX, IDC_EDIT1, m_ip);
	DDX_Text(pDX, IDC_EDIT3, m_message);
	DDX_LBString(pDX, IDC_LIST1, m_listAll);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnListen)
	ON_BN_CLICKED(IDC_BUTTON2, OnConnect)
	ON_BN_CLICKED(IDC_BUTTON3, OnSend)
	ON_EN_SETFOCUS(IDC_EDIT1, OnSetfocusEdit1)
	ON_EN_SETFOCUS(IDC_EDIT3, OnSetfocusEdit3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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
	CWnd *listenButton;
	listenButton=GetDlgItem(IDC_BUTTON1);
	listenButton->SetActiveWindow();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMyDlg::OnListen() 
{
	// TODO: Add your control notification handler code here

	NowPCis=0;//server
	//初始化server SOCKET 类
	//in_addr addLocal;
    //addLocal.S_un.S_addr=htonl(INADDR_ANY);//本地IP地址,把整形的INADDR_ANY 变换成字符串
	BOOL Flag=ServerListenSock.Create(5150,SOCK_STREAM,FD_ACCEPT);//,inet_ntoa(addLocal));
	if(!Flag) 
	{//create failure    	
		 AfxMessageBox("Socket Error!");
		 ServerListenSock.Close();
		 PostQuitMessage(0);
		 return;
	}
    if(!ServerListenSock.Listen(1))        //listen
	{//listen failure
		AfxMessageBox("Socket Error!"); 
		ServerListenSock.Close();
		PostQuitMessage(0);
		return;
	}
//	AfxMessageBox("listen is ok",MB_OK);
	m_connect.EnableWindow(FALSE);
	m_listen.EnableWindow(FALSE);
	CWnd *edit1;
	edit1=GetDlgItem(IDC_EDIT1); 
	edit1->EnableWindow(FALSE);
}

void CMyDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_ip!="")
	{
      NowPCis=1;   //client
	  //interested in connect is ok message
//	  AfxMessageBox(m_ip,MB_OK);
      ClientSock.Create(5151,SOCK_STREAM,FD_CONNECT);//|FD_READ|FD_WRITE);
      ClientSock.Connect(m_ip,5150);
	  m_listen.EnableWindow(FALSE);
	  m_connect.EnableWindow(FALSE);
	  CWnd *edit1;
    	edit1=GetDlgItem(IDC_EDIT1); 
	  edit1->EnableWindow(FALSE);
	}
}

void CMyDlg::OnSend() 
{
	UpdateData(TRUE);   //get data from control
	// TODO: Add your control notification handler code here
  if(m_message!="")
  {
	  
	if(NowPCis==0) // is server
	{
		ServerListenSock.pWorkSock->AsyncSelect(FD_WRITE);//WILL send info
	}
    if(NowPCis==1) // is CLIENT
	{
		ClientSock.AsyncSelect(FD_WRITE);
	}
	
		CListBox *allmes;
	allmes=(CListBox *)GetDlgItem(IDC_LIST1); //获得LIST BOX 句柄
 	allmes->AddString(m_message);
    //m_message="";
	     //display in the screen
  }
}

void CMyDlg::OnSetfocusEdit1() 
{
	// TODO: Add your control notification handler code here
	CButton *connectButton;
	connectButton=(CButton *)GetDlgItem(IDC_BUTTON2);
    connectButton->SetActiveWindow();
    //connectButton->SetState(1); 
	m_connect.SetCheck(2);

}

void CMyDlg::OnSetfocusEdit3() 
{
	// TODO: Add your control notification handler code here
	CWnd *sendButton;
	sendButton=GetDlgItem(IDC_BUTTON3);
	sendButton->SetActiveWindow();
}

⌨️ 快捷键说明

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