📄 cdlg.cpp
字号:
// cDlg.cpp : implementation file
//
#include "stdafx.h"
#include "c.h"
#include "cDlg.h"
#include "sock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCDlg dialog
CCDlg::CCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCDlg)
m_txt = _T("");
m_ip = _T("");
m_pot = 0;
m_send = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCDlg)
DDX_Text(pDX, IDC_EDIT1, m_txt);
DDX_Text(pDX, IDC_EDIT2, m_ip);
DDX_Text(pDX, IDC_EDIT3, m_pot);
DDV_MinMaxUInt(pDX, m_pot, 1, 65534);
DDX_Text(pDX, IDC_EDIT4, m_send);
DDV_MaxChars(pDX, m_send, 1000);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCDlg, CDialog)
//{{AFX_MSG_MAP(CCDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCDlg message handlers
BOOL CCDlg::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
m_ip="127.0.0.1";
m_pot=6000;
UpdateData(false);
this->GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
this->GetDlgItem(IDC_BUTTON3)->EnableWindow(false);
this->GetDlgItem(IDC_EDIT4)->EnableWindow(false);
m_buf[0]='\0';
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CCDlg::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 CCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCDlg::OnButton1()
{
// TODO: Add your control notification handler code here
this->UpdateData(true);
m_sock =new Csock(this);
if(!m_sock->Create())
{
m_txt+="*套接字建立失败!\r\n";
delete m_sock;
m_sock=NULL;
this->UpdateData(false);
return ;
}
if(!m_sock->Connect(m_ip,m_pot))
{
m_txt+="*连接服务器失败!\r\n";
delete m_sock;
m_sock=NULL;
this->UpdateData(false);
return ;
}
m_txt+="-连接服务器成功!\r\n";
this->UpdateData(false);
this->GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
this->GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
this->GetDlgItem(IDC_BUTTON3)->EnableWindow(true);
this->GetDlgItem(IDC_EDIT4)->EnableWindow(true);
this->GetDlgItem(IDC_EDIT2)->EnableWindow(false);
this->GetDlgItem(IDC_EDIT3)->EnableWindow(false);
}
void CCDlg::OnButton2()
{
// TODO: Add your control notification handler code here
m_sock->Close();
m_txt+="-断开连接!\r\n";
delete m_sock;
m_sock=NULL;
this->UpdateData(false);
this->GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
this->GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
this->GetDlgItem(IDC_BUTTON3)->EnableWindow(false);
this->GetDlgItem(IDC_EDIT4)->EnableWindow(false);
this->GetDlgItem(IDC_EDIT2)->EnableWindow(true);
this->GetDlgItem(IDC_EDIT3)->EnableWindow(true);
}
void CCDlg::OnButton3()
{
// TODO: Add your control notification handler code here
this->UpdateData(true);
char m_tbuf[1024];
m_tbuf[0]='\0';
strcat(m_tbuf,m_send.GetBuffer(m_send.GetLength()));
m_send.ReleaseBuffer();
m_send.Empty();
this->UpdateData(false);
m_sock->Send(m_tbuf,1024);
}
CCDlg::OnClose()
{
m_sock->Close();
m_txt+="-服务已关闭!\r\n";
delete m_sock;
m_sock=NULL;
this->UpdateData(false);
this->GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
this->GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
this->GetDlgItem(IDC_BUTTON3)->EnableWindow(false);
this->GetDlgItem(IDC_EDIT4)->EnableWindow(false);
this->GetDlgItem(IDC_EDIT2)->EnableWindow(true);
this->GetDlgItem(IDC_EDIT3)->EnableWindow(true);
}
CCDlg::OnReceive()
{
m_sock->Receive(m_buf,1024);
CString temp;
temp.Format("%s",m_buf);
m_txt+=temp;
temp.Format("%s","\r\n");
m_txt+=temp;
this->UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -