📄 clientdlg.cpp
字号:
// ClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
HANDLE hMutex;
bool CClientDlg::m_isconn = false;
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()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
m_command = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
for(int i = 0; i < 3; i++)
{
m_status[i]=0;
m_commands[i]="";
m_strmoni[i] = "";
}
m_strAlarm = "";
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Control(pDX, IDC_CONN, m_connbtn);
DDX_Control(pDX, IDC_MONITOR3, m_monitor3);
DDX_Control(pDX, IDC_MONITOR2, m_monitor2);
DDX_Control(pDX, IDC_MONITOR1, m_monitor1);
DDX_Text(pDX, IDC_COMMAND, m_command);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_SET, OnSet)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_CONN, OnConn)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_RECVDATA,OnRecvData)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::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_monitor1.SetCurSel(0);
m_monitor2.SetCurSel(1);
m_monitor3.SetCurSel(2);
*/
iniSet.iniopen("set.ini",4);
if(iniSet.iniread("HOSTADDR") != NULL)
{
//((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->SetAddress()
SetDlgItemText(IDC_IPADDRESS1,iniSet.iniread("HOSTADDR"));
SetDlgItemText(IDC_MONITOR1,iniSet.iniread("MONITOR1"));
SetDlgItemText(IDC_MONITOR2,iniSet.iniread("MONITOR2"));
SetDlgItemText(IDC_MONITOR3,iniSet.iniread("MONITOR3"));
}
if(!m_comMonitor.Open(1, 9600))
{
AfxMessageBox("port 1 cannot open~");//默认9600
//exit(0);
}
m_comMonitor.SetWnd(m_hWnd);
InitSocket();
HANDLESTR *pRecvParam = new HANDLESTR;
pRecvParam->hwnd = m_hWnd;
pRecvParam->sock = m_socket;
pRecvParam->addrSock = addrSock;
hMutex = CreateMutex(NULL,FALSE,NULL);
HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);
CloseHandle(hThread);
return TRUE; // return TRUE unless you set the focus to a control
}
void CClientDlg::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 CClientDlg::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 CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*void CClientDlg::OnConn()
{
// TODO: Add your control notification handler code here
/*UpdateData(TRUE);
while(!pSocket->Connect(m_addr,m_port))
{
if(AfxMessageBox("连接错误",MB_YESNO)==IDNO)
{
delete pSocket;
pSocket=NULL;
return ;
}
pSocket->Receive(buf,100,0);
if(connect(m_socket,(SOCKADDR*)&addrSock,sizeof(addrSock)) == SOCKET_ERROR)
{
MessageBox("failed to connect!");
return;
}
recv(m_socket,buf,100,0);
m_command=buf;
UpdateData(FALSE);
}*/
BOOL CClientDlg::InitSocket()
{
m_socket=socket(AF_INET,SOCK_STREAM,0);
if(INVALID_SOCKET==m_socket)
{
MessageBox("套接字创建失败!");
return FALSE;
}
DWORD dwIP;
((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);
addrSock.sin_family=AF_INET;
addrSock.sin_port=htons(8888);
addrSock.sin_addr.S_un.S_addr=htonl(dwIP);
return true;
}
DWORD WINAPI CClientDlg::RecvProc(LPVOID lpParameter)
{
SOCKET sock = ((HANDLESTR*)lpParameter)->sock;
SOCKADDR_IN addrSock = ((HANDLESTR*)lpParameter)->addrSock;
HWND hwnd = ((HANDLESTR*)lpParameter)->hwnd;
delete lpParameter;
char tempbuf[100];
int retval;
while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(!m_isconn)
{
if(SOCKET_ERROR == connect(sock,(SOCKADDR*)&addrSock,sizeof(addrSock)))
{
int erro = WSAGetLastError();
::AfxMessageBox("failed to connect!!");
closesocket(sock);
ExitThread(0);
ReleaseMutex(hMutex);
return -1;
}
m_isconn = true;
}
retval = recv(sock,tempbuf,100,0);
if(tempbuf[0]=='$')
{
::AfxMessageBox("lost connect!");
closesocket(sock);
ExitThread(0);
return -1;
}
if(SOCKET_ERROR == retval)
{
ReleaseMutex(hMutex);
continue;
}
if(FALSE == ::SendMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempbuf))
{
::AfxMessageBox("error in postmessage");
ReleaseMutex(hMutex);
continue;
}
ReleaseMutex(hMutex);
}
return 0;
}
void CClientDlg::OnRecvData(WPARAM wParam,LPARAM lParam)
{
DWORD dw;
// HOSTENT* pHost;
// char buf[32];
// char *bufip = buf;
// gethostname(bufip,strlen(bufip));
// pHost = gethostbyname(bufip);
UpdateData();
m_strAlarm = (char*)lParam;
/* if(m_strAlarm.Left(1)=="?")
{
CString strip = "!";
strip += pHost->h_addr_list[0];
send(m_socket,strip.GetBuffer(strip.GetLength()),strip.GetLength(),0);
}
*/
for(int i = 0; i<3 ;i++)
{
if(m_status[i] == 0) //可用
{
GetDlgItemText(IDC_MONITOR1+i,m_strmoni[i]);
m_commands[i] = m_strAlarm;
m_command.Format("SWITCH %s/%s",m_commands[i],m_strmoni[i]);
m_status[i] = 1;
UpdateData(FALSE);
dw = m_comMonitor.Write(m_command);
if(dw == 0)
{
MessageBox("没打开无法操作!");
//continue;
}
SetTimer(i+1,10000,NULL);
break;
}
}
if(3 == i)
{
}
}
void CClientDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
if(m_socket)
closesocket(m_socket);
CDialog::OnClose();
}
void CClientDlg::OnSet()
{
// TODO: Add your control notification handler code here
UpdateData();
CString strMoni;
if(iniSet.iniread("MONITOR1") == NULL)
{
iniSet.iniopen("set.ini",4);
GetDlgItemText(IDC_IPADDRESS1,strMoni);
iniSet.iniwrite("HOSTADDR",strMoni.GetBuffer(strMoni.GetLength()));
GetDlgItemText(IDC_MONITOR1,strMoni);
iniSet.iniwrite("MONITOR1",strMoni.GetBuffer(strMoni.GetLength()));
GetDlgItemText(IDC_MONITOR2,strMoni);
iniSet.iniwrite("MONITOR2",strMoni.GetBuffer(strMoni.GetLength()));
GetDlgItemText(IDC_MONITOR3,strMoni);
iniSet.iniwrite("MONITOR3",strMoni.GetBuffer(strMoni.GetLength()));
}
else
{
GetDlgItemText(IDC_IPADDRESS1,strMoni);
iniSet.inimodify("HOSTADDR",strMoni.GetBuffer(strMoni.GetLength()));
GetDlgItemText(IDC_MONITOR1,strMoni);
iniSet.inimodify("MONITOR1",strMoni.GetBuffer(strMoni.GetLength()));
GetDlgItemText(IDC_MONITOR2,strMoni);
iniSet.inimodify("MONITOR2",strMoni.GetBuffer(strMoni.GetLength()));
GetDlgItemText(IDC_MONITOR3,strMoni);
iniSet.inimodify("MONITOR3",strMoni.GetBuffer(strMoni.GetLength()));
}
}
void CClientDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
DWORD dw;
for(int i = 0; i < 3; i++)
{
if(m_status[i] == 1 && nIDEvent == (UINT)(i+1))
{
m_command.Format("SWITCH %s\\%s",m_strmoni[i],m_commands[i]);
UpdateData(FALSE);
dw = m_comMonitor.Write(m_command);
if(dw == 0)
{
KillTimer(i+1);
MessageBox("没打开无法操作!!");
return;
}
m_status[i] = 0;
KillTimer(i+1);
}
}
CDialog::OnTimer(nIDEvent);
}
void CClientDlg::OnConn()
{
// TODO: Add your control notification handler code here
int error;
//SOCKET sock;
//sock = m_socket;
InitSocket();
error = connect(m_socket,(SOCKADDR*)&addrSock,sizeof(addrSock));
CClientDlg::m_isconn = true;
if(SOCKET_ERROR == error)
{
error = WSAGetLastError();
if(error == WSAEISCONN)
{
MessageBox("已经连接!");
return ;
}
::AfxMessageBox("failed to connect!");
//closesocket(sock);
return ;
}
MessageBox("success connect!");
HANDLESTR *pRecvParam = new HANDLESTR;
pRecvParam->hwnd = m_hWnd;
pRecvParam->sock = m_socket;
pRecvParam->addrSock = addrSock;
HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);
CloseHandle(hThread);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -