📄 mymessengerdlg.cpp
字号:
// MyMessengerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "afxsock.h"
#include "MyMessenger.h"
#include "MyMessengerDlg.h"
#include "MySocket.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()
/////////////////////////////////////////////////////////////////////////////
// CMyMessengerDlg dialog
CMyMessengerDlg::CMyMessengerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyMessengerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyMessengerDlg)
m_emsg = _T("");
m_address = _T("");
m_port = 0;
m_itype = -1;
m_ipaddress = _T("");
m_ftype = -1;
m_sendfilename = _T("");
m_receivefilename = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyMessengerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyMessengerDlg)
DDX_Control(pDX, IDC_BRECEIVE, m_breceive);
DDX_Control(pDX, IDC_BCLOSE, m_bclose);
DDX_Control(pDX, IDC_LRECEIVED, m_lreceived);
DDX_Control(pDX, IDC_LSENT, m_lsent);
DDX_Control(pDX, IDC_BCONNECT, m_bconnect);
DDX_Text(pDX, IDC_EMSG, m_emsg);
DDX_Text(pDX, IDC_ADDRESS, m_address);
DDX_Text(pDX, IDC_PORT, m_port);
DDX_Radio(pDX, IDC_CLIENT, m_itype);
DDX_Text(pDX, IDC_IPADDRESS, m_ipaddress);
DDX_Radio(pDX, IDC_MESSAGETYPE, m_ftype);
DDX_Text(pDX, IDC_SENDFILENAME, m_sendfilename);
DDX_Text(pDX, IDC_RECEIVEFILENAME, m_receivefilename);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyMessengerDlg, CDialog)
//{{AFX_MSG_MAP(CMyMessengerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CLIENT, OnRType)
ON_BN_CLICKED(IDC_BCONNECT, OnBconnect)
ON_BN_CLICKED(IDC_BSEND, OnBsend)
ON_BN_CLICKED(IDC_BCLOSE, OnBclose)
ON_BN_CLICKED(IDC_SERVER, OnServer)
ON_BN_CLICKED(IDC_BRECEIVE, OnBreceive)
ON_BN_CLICKED(IDC_BADDSENDFILE, OnBaddsendfile)
ON_BN_CLICKED(IDC_ADDRECEIVEFILE, OnAddreceivefile)
ON_BN_CLICKED(IDC_MESSAGETYPE, OnMessagetype)
ON_BN_CLICKED(IDC_BSENDFILE, OnBsendfile)
ON_BN_CLICKED(IDC_FILETYPE, OnFiletype)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyMessengerDlg message handlers
BOOL CMyMessengerDlg::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
m_itype=0;
m_ftype=0;
GetIPAddress();
m_ipaddress=LocalIPAddress;
m_address=LocalIPAddress;
m_port=40021;
m_sendfilename="";
m_receivefilename="";
UpdateData(FALSE);
m_ConnectSocket.SetParent(this);
m_ListenSocket.SetParent(this);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyMessengerDlg::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 CMyMessengerDlg::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 CMyMessengerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyMessengerDlg::OnRType() //服务器or客户端
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if (m_itype==0)
{
m_bclose.SetWindowText("断开连接");
m_bconnect.SetWindowText("连接");
GetDlgItem(IDC_ADDRESS)->EnableWindow(TRUE);
}
else
{
m_bconnect.SetWindowText("开启服务");
m_bclose.SetWindowText("关闭服务");
GetDlgItem(IDC_ADDRESS)->EnableWindow(FALSE);
}
}
void CMyMessengerDlg::OnBconnect() //连接按钮被按下后
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
GetDlgItem(IDC_BCONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_ADDRESS)->EnableWindow(FALSE);
GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
GetDlgItem(IDC_CLIENT)->EnableWindow(FALSE);
GetDlgItem(IDC_SERVER)->EnableWindow(FALSE);
GetDlgItem(IDC_BCLOSE)->EnableWindow(TRUE);
if (m_itype==0) //如果是客户端
{
m_ConnectSocket.Create();
m_ConnectSocket.Connect(m_address,m_port);
}
else //如果是服务器
{
MessageBox("服务器已开启,现等待连接……");
m_ListenSocket.Create(m_port);
m_ListenSocket.Listen();
}
}
void CMyMessengerDlg::OnAccept()
//报路事件:侦听到有连接到本服务器的连接
{
m_ListenSocket.Accept(m_ConnectSocket);
//GetDlgItem(IDC_EMSG)->EnableWindow(TRUE);
//GetDlgItem(IDC_BSEND)->EnableWindow(TRUE);
//GetDlgItem(IDC_BCLOSE)->EnableWindow(TRUE);
//MessageBox("成功建立连接!");
}
void CMyMessengerDlg::OnSend() //报路事件:成功连接上了
{
UpdateData(TRUE);
GetDlgItem(IDC_MESSAGETYPE)->EnableWindow(TRUE);
GetDlgItem(IDC_FILETYPE)->EnableWindow(TRUE);
if (m_ftype==0)
{
GetDlgItem(IDC_EMSG)->EnableWindow(TRUE);
GetDlgItem(IDC_BSEND)->EnableWindow(TRUE);
GetDlgItem(IDC_SENDFILENAME)->EnableWindow(FALSE);
GetDlgItem(IDC_BADDSENDFILE)->EnableWindow(FALSE);
GetDlgItem(IDC_ADDRECEIVEFILE)->EnableWindow(FALSE);
GetDlgItem(IDC_BSENDFILE)->EnableWindow(FALSE);
GetDlgItem(IDC_BRECEIVE)->EnableWindow(FALSE);
GetDlgItem(IDC_RECEIVEFILENAME)->EnableWindow(FALSE);
GetDlgItem(IDC_BRECEIVE)->EnableWindow(FALSE);
}
else
{
GetDlgItem(IDC_EMSG)->EnableWindow(FALSE);
GetDlgItem(IDC_BSEND)->EnableWindow(FALSE);
GetDlgItem(IDC_SENDFILENAME)->EnableWindow(TRUE);
GetDlgItem(IDC_BADDSENDFILE)->EnableWindow(TRUE);
GetDlgItem(IDC_ADDRECEIVEFILE)->EnableWindow(TRUE);
GetDlgItem(IDC_BSENDFILE)->EnableWindow(TRUE);
GetDlgItem(IDC_BRECEIVE)->EnableWindow(TRUE);
GetDlgItem(IDC_RECEIVEFILENAME)->EnableWindow(TRUE);
GetDlgItem(IDC_BRECEIVE)->EnableWindow(TRUE);
}
MessageBox("成功建立连接!");
}
void CMyMessengerDlg::OnBsend() //发送消息
{
// TODO: Add your control notification handler code here
int len;
UpdateData(TRUE);
if (m_emsg!="")
{
len=m_emsg.GetLength();
if (m_ConnectSocket.Send(LPCTSTR(m_emsg),len)==SOCKET_ERROR)
{
}
else
{
m_lsent.AddString(m_emsg); //在列表框sent中显示消息
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -