📄 mfctalkdlg.cpp
字号:
// MFCTALKDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MFCTALK.h"
#include "MFCTALKDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
extern CMFCTALKApp theApp;
CMFCTALKDlg *pDlg;
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()
/////////////////////////////////////////////////////////////////////////////
// CMFCTALKDlg dialog
CMFCTALKDlg::CMFCTALKDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMFCTALKDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMFCTALKDlg)
m_addr = _T("127.0.0.1");
m_cport = 70;
m_sport = 70;
m_sendinfo = _T("");
m_showreceiveinfo = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCTALKDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMFCTALKDlg)
DDX_Control(pDX, IDC_CREATESREVER_BTN, m_createserver);
DDX_Control(pDX, IDC_CONNECTSERVER_BTN, m_connectionserver);
DDX_Text(pDX, IDC_ADDRESS_EDIT, m_addr);
DDX_Text(pDX, IDC_CPORT_EDIT, m_cport);
DDX_Text(pDX, IDC_SPORT_EDIT, m_sport);
DDX_Text(pDX, IDC_SEND_EDIT, m_sendinfo);
DDX_Text(pDX, IDC_SHOW_EDIT, m_showreceiveinfo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMFCTALKDlg, CDialog)
//{{AFX_MSG_MAP(CMFCTALKDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CREATESREVER_BTN, OnCreatesreverBtn)
ON_BN_CLICKED(IDC_CONNECTSERVER_BTN, OnConnectserverBtn)
ON_BN_CLICKED(IDC_SEND_BTN, OnSendBtn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMFCTALKDlg message handlers
BOOL CMFCTALKDlg::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
pDlg=(CMFCTALKDlg*)theApp.m_pMainWnd;
return TRUE; // return TRUE unless you set the focus to a control
}
void CMFCTALKDlg::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 CMFCTALKDlg::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 CMFCTALKDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMFCTALKDlg::OnOK() //发送测试连接
{
// TODO: Add extra validation here
CString buf="连接成功";
if(isServer)
m_serversocket.Send(buf,buf.GetLength());
else
m_clientsocket.Send(buf,buf.GetLength());
}
void CMFCTALKDlg::OnCreatesreverBtn()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(!m_listensocket.Create(m_sport))
{
MessageBox("Create server erorr!");
return ;
}
if(!m_listensocket.Listen())
{
MessageBox("Listen erorr!");
return ;
}
isServer=TRUE;
}
void CMFCTALKDlg::OnConnectserverBtn()
{
// TODO: Add your control notification handler code here
sockaddr_in addr;
char serverIP[16];
UpdateData();
strncpy(serverIP,LPCSTR(m_addr),16);
if(!inet_addr(serverIP))
{
MessageBox("Server IP erorr!");
return ;
}
if(!m_clientsocket.Create())
{
MessageBox("Create client erorr!");
return ;
}
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_addr=inet_addr(serverIP);
addr.sin_port=htons(m_cport);
m_clientsocket.Connect((sockaddr*)&addr,sizeof(addr));
isServer=FALSE;
}
void CMFCTALKDlg::acceptserver()
{
m_listensocket.Accept(m_serversocket);
m_connectionserver.EnableWindow(FALSE);
m_createserver.EnableWindow(FALSE);
}
void CMFCTALKDlg::receive()//接收测试连接
{
char buf[100]="";
if(isServer)
m_serversocket.Receive(buf,100);
else
m_clientsocket.Receive(buf,100);
//MessageBox(buf);
UpdateData(TRUE);
if(m_showreceiveinfo!="")
m_showreceiveinfo+="\r\n";
m_showreceiveinfo+=buf;
UpdateData(FALSE);
}
void CMFCTALKDlg::OnSendBtn()
{
// TODO: Add your control notification handler code here
DWORD nSize = MAX_COMPUTERNAME_LENGTH + 1;
char Buff[MAX_COMPUTERNAME_LENGTH + 1];
GetComputerName(Buff,&nSize);
UpdateData(TRUE);
CString info;
info+=Buff;
info+=":";
info+=m_sendinfo;
m_sendinfo="";
UpdateData(FALSE);
//开始发送数据
if(isServer)
m_serversocket.Send(info,info.GetLength());
else
m_clientsocket.Send(info,info.GetLength());
}
/*char buffer[101]="";
int a=recv(m_theclient,buffer,101,0);
if(a)
ShowWindow(SW_SHOWNORMAL);
UpdateData(TRUE);
if(m_show!="")
m_show+="\r\n";
m_show+=buffer;
//m_edit.
UpdateData(FALSE);*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -