?? talkdlg.cpp
字號:
// TalkDlg.cpp : implementation file
//
#include "Talk.h"
#include "TalkDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct DOSDate
{
short int dos_year;
char dos_day;
char dos_mon;
};
struct DOSTime
{
unsigned char DOS_min;
unsigned char DOS_hour;
unsigned char DOS_hund;
unsigned char DOS_sec;
};
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTalkDlg dialog
CTalkDlg::CTalkDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTalkDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTalkDlg)
m_strMsg = _T("");
m_strServName = _T("");
m_nServPort = 0;
ReceiveNum = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTalkDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTalkDlg)
DDX_Control(pDX, IDC_LIST_RECEIVED, m_listReceived);
DDX_Control(pDX, IDC_LIST_SENT, m_listSent);
DDX_Control(pDX, IDC_COMBO_TYPE, m_cmbType);
DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
DDX_Text(pDX, IDC_EDIT_SERVNAME, m_strServName);
DDX_Text(pDX, IDC_EDIT_SERVPORT, m_nServPort);
DDX_Control(pDX, IDC_BTN_CONNECT, m_btnConnect);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTalkDlg, CDialog)
//{{AFX_MSG_MAP(CTalkDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
ON_CBN_SELCHANGE(IDC_COMBO_TYPE, OnSelChangeComboType)
ON_BN_CLICKED(IDOK, OnSendMsg)
ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTalkDlg message handlers
BOOL CTalkDlg::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_cmbType.SetCurSel(1); //default: client mode
m_strServName = "192.168.201.22";
m_nServPort = 1001;
UpdateData(FALSE); //update dialog
m_sListenSocket.SetParent(this);
m_sConnectSocket.SetParent(this);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTalkDlg::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 CTalkDlg::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 CTalkDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTalkDlg::OnBtnConnect()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE); // get data from diaglog window
GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_SERVNAME)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_SERVPORT)->EnableWindow(FALSE);
GetDlgItem(IDC_STATIC_SERVNAME)->EnableWindow(FALSE);
GetDlgItem(IDC_STATIC_SERVPORT)->EnableWindow(FALSE);
GetDlgItem(IDC_COMBO_TYPE)->EnableWindow(FALSE);
if(m_cmbType.GetCurSel( ) == 0 ) // server mode
{
m_sListenSocket.Create(m_nServPort);
m_sListenSocket.Listen( );
}
else // client mode
{
m_sConnectSocket.Create( );
//m_sConnectSocket.Connect(m_strServName, m_nServPort);
SOCKADDR_IN Addr;
memset( &Addr, 0, sizeof(SOCKADDR_IN) );
Addr.sin_family = AF_INET;
Addr.sin_port = htons(m_nServPort);
Addr.sin_addr.S_un.S_addr = inet_addr(m_strServName);
m_sConnectSocket.Connect( (SOCKADDR*)&Addr, sizeof(SOCKADDR_IN) );
}
}
void CTalkDlg::OnSelChangeComboType()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE); // retrieve data from dialog
if(m_cmbType.GetCurSel() == 0 ) // server mode
{
m_btnConnect.SetWindowText("偵聽");
}
else // client mode
{
m_btnConnect.SetWindowText("連接");
}
}
void CTalkDlg::OnAccept()
{
// for server only
m_sListenSocket.Accept(m_sConnectSocket);
//active related control objects
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(TRUE);
GetDlgItem(IDOK)->EnableWindow(TRUE); // send button
GetDlgItem(IDC_STATIC_MSG)->EnableWindow(TRUE);
}
void CTalkDlg::OnConnect()
{
// for client only
//active related control objects
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(TRUE);
GetDlgItem(IDOK)->EnableWindow(TRUE); // send button
GetDlgItem(IDC_STATIC_MSG)->EnableWindow(TRUE);
GetDlgItem(IDC_BTN_CLOSE)->EnableWindow(TRUE);
}
struct NetBoxSetup
{
char CmdCode;
struct DOSDate Date;
struct DOSTime Time;
};
void CTalkDlg::OnSendMsg()
{
// TODO: Add your control notification handler code here
int nLen; // message length
int nSent; // sent data length
CTime MyTime;
NetBoxSetup SetupBlock;
SYSTEMTIME currentTime;
UpdateData(TRUE); //get data from diaglog window
//SetTimer( 1, 100, NULL );
if( m_strMsg.IsEmpty() || !m_strMsg.Compare("Set"))
{
SetupBlock.CmdCode = 0x10;
//GetSystemTime( ¤tTime );
GetLocalTime( ¤tTime );
SetupBlock.Date.dos_year = (short int)currentTime.wYear;
SetupBlock.Date.dos_mon = (char)currentTime.wMonth;
SetupBlock.Date.dos_day = (char)currentTime.wDay;
//SetupBlock.Time.DOS_hour = (unsigned char)currentTime.wHour + 8;
SetupBlock.Time.DOS_hour = (unsigned char)currentTime.wHour;
SetupBlock.Time.DOS_min = (unsigned char)currentTime.wMinute;
SetupBlock.Time.DOS_sec = (unsigned char)currentTime.wSecond;
SetupBlock.Time.DOS_hund = 0;
m_strMsg = "NetBox Set";
nLen = sizeof( NetBoxSetup );
nSent = m_sConnectSocket.Send( &SetupBlock, nLen );
}
else if( !m_strMsg.Compare("Time") )
{
SetupBlock.CmdCode = 0x11;
nLen = sizeof( NetBoxSetup );
nSent = m_sConnectSocket.Send( &SetupBlock, nLen );
// Add May20-2003 for test time
//GetLocalTime( ¤tTime );
//m_strMsg.Format( "Time: %02d:%02d:%02d",currentTime.wHour, currentTime.wMinute, currentTime.wSecond );
// Add end
}
else if( !m_strMsg.Compare("Exit") )
{
SetupBlock.CmdCode = 0x1a;
nLen = sizeof( NetBoxSetup );
nSent = m_sConnectSocket.Send( &SetupBlock, nLen );
}
else
{
nLen = m_strMsg.GetLength();
nSent = m_sConnectSocket.Send( m_strMsg, nLen );
// nSent = m_sConnectSocket.Send( m_strMsg, nLen );
}
if( nSent != SOCKET_ERROR )
{
// send successfully!
m_listSent.AddString(m_strMsg);
UpdateData(FALSE); // put data to diaglog window
}
else
{
AfxMessageBox("send fail!", MB_OK|MB_ICONSTOP);
}
//clean up current message
m_strMsg.Empty();
UpdateData(FALSE); // put data to diaglog window
}
void CTalkDlg::OnReceive()
{
char pBuf[1024];
char pBuf1[1024];
int nBufSize = 1024;
int nReceived;
int len,i, i1;
CString strReceived;
unsigned char abyte;
// receiving message
nReceived = m_sConnectSocket.Receive( pBuf, nBufSize );
if( nReceived != SOCKET_ERROR )
{
pBuf[nReceived] = '\0';
/* for( i=0, i1=0; i<nReceived; i++ )
{
abyte = pBuf[i];
sprintf( &pBuf1[i1], "%02x", (unsigned short int)abyte );
i1 += 2;
}
pBuf1[i1] = '\0'; */
//strReceived.Format("Num=%d", ReceiveNum );
strReceived.Format("%s", pBuf );
//nReceived = strReceived.GetLength();
m_listReceived.AddString(strReceived);
//UpdateData(FALSE); // put data to diaglog window
//ReceiveNum++;
//len = m_sConnectSocket.Send( strReceived, nReceived );
/*if( m_cmbType.GetCurSel()==0 )
{
len = m_sConnectSocket.Send( strReceived, nReceived );
}*/
}
else
{
AfxMessageBox( "Receive Fail!", MB_OK|MB_ICONSTOP );
}
}
void CTalkDlg::OnClose()
{
// close socket
m_sConnectSocket.Close();
// disable message send related control object
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(FALSE);
GetDlgItem(IDOK)->EnableWindow(FALSE);
GetDlgItem(IDC_STATIC_MSG)->EnableWindow(FALSE);
GetDlgItem(IDC_BTN_CLOSE)->EnableWindow(FALSE);
// cleanup list
while(m_listSent.GetCount() != 0 )
{
m_listSent.DeleteString(0);
}
while(m_listReceived.GetCount() != 0 )
{
m_listReceived.DeleteString(0);
}
// for client only
if( m_cmbType.GetCurSel() == 1 )
{
GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT_SERVNAME)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT_SERVPORT)->EnableWindow(TRUE);
GetDlgItem(IDC_STATIC_SERVNAME)->EnableWindow(TRUE);
GetDlgItem(IDC_STATIC_SERVPORT)->EnableWindow(TRUE);
GetDlgItem(IDC_COMBO_TYPE)->EnableWindow(TRUE);
}
}
void CTalkDlg::OnBtnClose()
{
// TODO: Add your control notification handler code here
OnClose();
}
void CTalkDlg::OnTimer(UINT nIDEvent)
{
int nLen; // message length
int nSent; // sent data length
// TODO: Add your message handler code here and/or call default
nLen = m_strMsg.GetLength();
nSent = m_sConnectSocket.Send( m_strMsg, nLen );
CDialog::OnTimer(nIDEvent);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -