📄 cmychatdlg.cpp
字号:
// CmyChatDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CmyChat.h"
#include "CmyChatDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CCmyChatDlg dialog
CCmyChatDlg::CCmyChatDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCmyChatDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCmyChatDlg)
m_strInfo = _T("");
m_strConnect = _T("");
m_strInput = _T("");
m_strOutput = _T("");
m_nStatus = 1;
// m_strIpAddress = _T("");
m_strIpAddress = "";
m_nPort = 8000;
m_strBps = _T("");
m_strCom = _T("");
m_strComInput = _T("");
m_strComOutput = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCmyChatDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCmyChatDlg)
DDX_Control(pDX, IDC_LIST_SOCKET, m_cListSocket);
DDX_Control(pDX, IDC_LIST_COM, m_cListCom);
DDX_Control(pDX, IDC_BUTTON_CLEARRECIEVE, m_cClearRecieve);
DDX_Control(pDX, IDC_CHECK_SOC_HEXSEND, m_cSocHexSend);
DDX_Control(pDX, IDC_CHECK_SOC_HEXRECIEVE, m_cSocHexRecieve);
DDX_Control(pDX, IDC_CHECK_COM_HEXRECIEVE, m_cComHexRecieve);
DDX_Control(pDX, IDC_CHECK_COM_HEXSEND, m_cComHexSend);
DDX_Control(pDX, IDC_COM_INPUTEDIT, m_cComInput);
DDX_Control(pDX, IDC_COM_OUTPUTEDIT, m_cComOutput);
DDX_Control(pDX, IDC_BUTTON_COM_CLEARRECIEVE, m_cComClearRecieve);
DDX_Control(pDX, IDC_BUTTON_COM_CLEARSEND, m_cComClearSend);
DDX_Control(pDX, IDC_BUTTON_COM_SEND, m_cComSend);
DDX_Control(pDX, IDC_BUTTON_COMSET, m_cComSet);
DDX_Control(pDX, IDC_SERVERIPADDRESS, m_cServerIp);
DDX_Control(pDX, IDC_PORT, m_cPort);
DDX_Control(pDX, IDC_BUTTON_DISCONNECT, m_cDisconnect);
DDX_Control(pDX, IDC_BUTTON_CONNECT, m_cConnect);
DDX_Control(pDX, IDCANCEL, m_cCancel);
DDX_Control(pDX, IDC_BUTTON_CLEARSEND, m_cClear);
DDX_Control(pDX, IDC_BUTTON_SEND, m_cSend);
DDX_Control(pDX, IDC_OUTPUTEDIT, m_cOutput);
DDX_Control(pDX, IDC_INPUTEDIT, m_cInput);
DDX_Text(pDX, IDC_CONNECTEDIT, m_strConnect);
DDX_Text(pDX, IDC_INPUTEDIT, m_strInput);
DDX_Text(pDX, IDC_OUTPUTEDIT, m_strOutput);
DDX_Radio(pDX, IDC_RADIO_SERVER, m_nStatus);
DDX_Text(pDX, IDC_SERVERIPADDRESS, m_strIpAddress);
DDV_MaxChars(pDX, m_strIpAddress, 15);
DDX_Text(pDX, IDC_PORT, m_nPort);
DDV_MinMaxUInt(pDX, m_nPort, 1, 30000);
DDX_Control(pDX, IDC_MSCOMM1, m_cComm);
DDX_Text(pDX, IDC_STATIC_BPS, m_strBps);
DDX_Text(pDX, IDC_STATIC_COM, m_strCom);
DDX_Text(pDX, IDC_COM_INPUTEDIT, m_strComInput);
DDX_Text(pDX, IDC_COM_OUTPUTEDIT, m_strComOutput);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCmyChatDlg, CDialog)
//{{AFX_MSG_MAP(CCmyChatDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO_SERVER, OnRadioServer)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect)
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_CLEARSEND, OnButtonClearsend)
ON_BN_CLICKED(IDC_BUTTON_CLEARRECIEVE, OnButtonClearrecieve)
ON_BN_CLICKED(IDC_RADIO_CLIENT, OnRadioClient)
ON_BN_CLICKED(IDC_BUTTON_REG, OnButtonReg)
ON_BN_CLICKED(IDC_BUTTON_HEART, OnButtonHeart)
ON_BN_CLICKED(IDC_BUTTON_COM_SEND, OnButtonComSend)
ON_BN_CLICKED(IDC_BUTTON_COM_CLEARSEND, OnButtonComClearsend)
ON_BN_CLICKED(IDC_BUTTON_COM_CLEARRECIEVE, OnButtonComClearrecieve)
ON_BN_CLICKED(IDC_BUTTON_COMSET, OnButtonComset)
ON_BN_CLICKED(IDC_BUTTON_COMOPEN, OnButtonComopen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCmyChatDlg message handlers
BOOL CCmyChatDlg::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
// m_nStatus=-1; //单选服务器或客户端
// m_nStatus=1; //默认客户端
m_ServerSocket=NULL;
m_ClientSocket=NULL;
m_arIn=NULL;
m_arOut=NULL;
m_file=NULL;
m_strConnect="";
m_strIpAddress="192.168.0.204"; //服务器IP
// m_nPort=8000; //服务器端口
// GetDlgItem(IDC_SERVERIPADDRESS)->EnableWindow(FALSE);
// GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_REG)->EnableWindow(TRUE);//设置REG_OK按钮无效
GetDlgItem(IDC_BUTTON_HEART)->EnableWindow(TRUE);//设置HEART按钮无效
UpdateData(FALSE);
//初始化串口
if(m_cComm.GetPortOpen())
m_cComm.SetPortOpen(FALSE);
/*
m_cComm.SetCommPort(1); //选择com1
if( !m_cComm.GetPortOpen())
{
m_cComm.SetPortOpen(TRUE);//打开串口
m_cComm.SetDTREnable(TRUE);
}
else
AfxMessageBox("cannot open serial port,不能打开串口!");
m_cComm.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位
m_cComm.SetInputMode(1); //1:表示以二进制方式检取数据
m_cComm.SetRThreshold(1);
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
m_cComm.SetInputLen(0); //设置当前接收区数据长度为0
m_cComm.GetInput();//先预读缓冲区以清除残留数据
*/
m_cListCom.InsertColumn(0,"收发",LVCFMT_LEFT,40,-1);
m_cListCom.InsertColumn(1," 时 间",LVCFMT_LEFT,80,-1);
m_cListCom.InsertColumn(2,"内 容",LVCFMT_CENTER,150,-1);
m_cListSocket.InsertColumn(0,"收发",LVCFMT_LEFT,40,-1);
m_cListSocket.InsertColumn(1," 时 间",LVCFMT_LEFT,80,-1);
m_cListSocket.InsertColumn(2,"内 容",LVCFMT_CENTER,150,-1);
m_cServerIp.SetFocus();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CCmyChatDlg::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 CCmyChatDlg::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 CCmyChatDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCmyChatDlg::OnRadioServer()
{
UpdateData(TRUE);
GetDlgItem(IDC_SERVERIPADDRESS)->EnableWindow(FALSE);
GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_REG)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_HEART)->EnableWindow(FALSE);//设置REG_OK按钮无效
UpdateData(FALSE);
m_cPort.SetFocus();
// TODO: Add your control notification handler code here
}
void CCmyChatDlg::OnRadioClient()
{
UpdateData(TRUE);
GetDlgItem(IDC_SERVERIPADDRESS)->EnableWindow(TRUE);
GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_REG)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_HEART)->EnableWindow(TRUE);//设置HEART按钮有效
UpdateData(FALSE);
m_cServerIp.SetFocus();
// TODO: Add your control notification handler code here
}
void CCmyChatDlg::OnButtonSend()
{
m_strOutput.TrimLeft(" ");
if(m_arOut)
{
if(m_nStatus==0) //若为服务器时
{
UpdateData(TRUE);
// char m_str="G";
// m_strOutput=ConvertHexChar(m_str);
// m_strOutput="\x47\x32";
*m_arOut<<m_strOutput;
m_arOut->Flush();
}
else
{
UpdateData(TRUE);
*m_arOut<<m_strOutput;
m_arOut->Flush();
}
}
else
AfxMessageBox("没有建立连接!");
m_cOutput.SetFocus();
// TODO: Add your control notification handler code here
}
void CCmyChatDlg::OnButtonDisconnect()
{
if(m_arOut!=NULL)
{
SocketReset();
m_strConnect="断开连接!";
UpdateData(FALSE);
}
// TODO: Add your control notification handler code here
}
//当单击连接按钮时,进行连接
void CCmyChatDlg::OnButtonConnect()
{
CString msg;
UpdateData(TRUE);
if(m_nStatus==0)//若为服务器时
{
if(m_ServerSocket!=NULL)//连接不上时
{
m_strConnect="请单击断开按钮";
UpdateData(FALSE);
}
else
{
m_strConnect="正在等待客户端连接......";
UpdateData(FALSE);
if(!AfxSocketInit())
{
MessageBox("初始化失败","Send",MB_ICONSTOP);
return;
}
m_ServerSocket=new CNewSocket;
m_ServerSocket->m_nStatus=m_nStatus;
m_ServerSocket->GetDlg(this);
if(!m_ServerSocket->Create(m_nPort))
MessageBox("发送创建失败","Send",MB_ICONSTOP);
else //若连接上,就可以聊天了
{
m_ServerSocket->Listen(); //监听端口
}
}
}
else
{
if(m_nStatus==1)//若为客户端时
{
// GetDlgItem(IDC_BUTTON_REG)->EnableWindow(TRUE);//REG_OK按钮失效
if(m_ClientSocket!=NULL)//连接不上时
{
m_strConnect="请单击断开按钮";
UpdateData(FALSE);
}
else
{
m_strConnect="正在请求连接服务器......";
UpdateData(FALSE);
if(!AfxSocketInit())
{
MessageBox("初始化失败","Receive",MB_ICONSTOP);
return;
}
m_ClientSocket=new CNewSocket;
m_ClientSocket->GetDlg(this);
m_ClientSocket->m_nStatus=m_nStatus;
if(!m_ClientSocket->Create())
{ MessageBox("接收创建失败","Receive",MB_ICONSTOP);
return;
}
else
{
if(!m_ClientSocket->Connect(m_strIpAddress,m_nPort))
{
CString str=m_strConnect;
SocketReset();
m_strConnect=str;
m_strConnect+="连接错误!";
UpdateData(FALSE);
}
else
{
m_strConnect+="连接成功!";
m_file=new CSocketFile(m_ClientSocket);
m_arIn=new CArchive(m_file,CArchive::load);
m_arOut=new CArchive(m_file,CArchive::store);
}
UpdateData(FALSE);
}
}
}
}
if(m_nStatus==-1)
{
msg="请选择登录状态";
AfxMessageBox(msg);
}
m_cOutput.SetFocus();
// TODO: Add your control notification handler code here
}
void CCmyChatDlg::OnButtonClearsend()
{
m_strOutput=_T("");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -