⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monitorclientappdlg.cpp

📁 监控软件的客户端: 1.采用select模型; 2.采用多线程进行网络的接收和发送; 3.网络断线自动断开;
💻 CPP
字号:
// MonitorClientAppDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MonitorClientApp.h"
#include "MonitorClientAppDlg.h"

#include "DlgServerSetting.h"
#include "Common.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


//////////////////////////////////////////////////////////////////////////
const CHAR CONST_STR_TRAY_TITLE[64]	= "怡和交通报警监控软件\0";

//////////////////////////////////////////////////////////////////////////
HWND OnRecvAlarmOnRecvAlarm = NULL;
HWND g_hRecvWnd = NULL;
/////////////////////////////////////////////////////////////////////////////
// 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()

//////////////////////////////////////////////////////////////////////////
CDataLog m_oDataLog;

/////////////////////////////////////////////////////////////////////////////
// CMonitorClientAppDlg dialog
CMonitorClientAppDlg::CMonitorClientAppDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMonitorClientAppDlg::IDD, pParent), m_eCurStatus( STATUS_NORMAL )
{
	//{{AFX_DATA_INIT(CMonitorClientAppDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CMonitorClientAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMonitorClientAppDlg)
	DDX_Control(pDX, IDC_LIST_ALARM, m_ctrlLstAlarm);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMonitorClientAppDlg, CDialog)
	//{{AFX_MSG_MAP(CMonitorClientAppDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_COMMAND(ID_TRAY_CLOSE, OnTrayClose)
	ON_WM_TIMER()
	ON_MESSAGE( WM_RECVDATA , OnRecvMsg )	
	ON_MESSAGE( WM_RECVDATA_ALARM , OnRecvAlarm )
	ON_MESSAGE( WM_RECVDATA_MODIFY_TRAY , OnRecvModifyTray )
	ON_BN_CLICKED(IDC_BUTTON_SERVERSETTING, OnButtonServersetting)
	ON_MESSAGE(WM_SYSTEMTRAY,OnSystemTray)
	ON_BN_CLICKED(IDC_BUTTON_OPENIE, OnButtonOpenie)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMonitorClientAppDlg message handlers


LRESULT CMonitorClientAppDlg::OnSystemTray(WPARAM wParam, LPARAM lParam)
{
	if(wParam=ID_SYSTEMTRAY)
	{
		switch(lParam)
		{
		case  WM_LBUTTONDBLCLK:
			{
				::ShowWindow(m_hWnd,SW_SHOWDEFAULT);
				DestroyTray();

				/*KillTimer(0);
				NOTIFYICONDATA nid;
	            nid.cbSize=sizeof(NOTIFYICONDATA);
	            nid.hWnd=m_hWnd;
	            nid.uID=ID_SYSTEMTRAY;
	            nid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
	            nid.uCallbackMessage=WM_SYSTEMTRAY;
	            nid.hIcon=AfxGetApp()->LoadIcon(sel);
	            strcpy(nid.szTip,CONST_STR_TRAY_TITLE);
	            ::Shell_NotifyIcon(NIM_MODIFY,&nid);
				::ShowWindow(m_hWnd,SW_SHOWDEFAULT);				
		        sndPlaySound(NULL,SND_ASYNC);
				m_tubiao.SetIcon(AfxGetApp()->LoadIcon(m_netsel));*/
			}
			break;
		case WM_RBUTTONDOWN:
			{
				CMenu pMenu;
	            pMenu.LoadMenu(IDR_MENU1);
	            CMenu* pMenu1=pMenu.GetSubMenu(0);
	            ASSERT(pMenu1!=NULL);
                CPoint point;
				GetCursorPos(&point);
	            pMenu1->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
				break;
			}
		default:
			break;
		}
	}
	return 1;
}

void CMonitorClientAppDlg::OnRecvModifyTray(WPARAM wParam, LPARAM lParam )
{
	E_STATUS eStatus = (E_STATUS)wParam;
	m_eCurStatus = eStatus;

	if( STATUS_ALARM == wParam )
	{
		SetTimer( 1, 500, NULL );
	}
	else
	{
		ModifyTray( m_eCurStatus );
	}
		
}

void CMonitorClientAppDlg::OnRecvAlarm(WPARAM wParam, LPARAM lParam )
{
	CListCtrl_Ex * pLstCtrl = (CListCtrl_Ex*)GetDlgItem( IDC_LIST_ALARM );
	if( NULL == pLstCtrl )
		return;

	if( NULL == lParam ) return;
	ST_SEND_RCV_INFO oInfo = *(ST_SEND_RCV_INFO*)lParam;
	int nStringCount = 3;
	CString pStrCnt[3];
	pStrCnt[0] = oInfo.uSendData.m_AlarmInfo.cIP;
	pStrCnt[1] = oInfo.uSendData.m_AlarmInfo.cTime;
	pStrCnt[2] = oInfo.uSendData.m_AlarmInfo.cInfo;

	int nItem = pLstCtrl->AddRowString( pStrCnt, 3 );
	
	ST_ALARM_COLOR oColor;
	if( oInfo.uSendData.m_AlarmInfo.nType == 0 )
	{	
		int nLevel = oInfo.uSendData.m_AlarmInfo.nAlarmIn;
		if( nLevel >= 0 )
		{				
			pLstCtrl->SetRowColor( nItem, oColor.stLvClr_BK[nLevel], oColor.stLvClr_TXT[nLevel] );
		}
	}
	else if( 1 == oInfo.uSendData.m_AlarmInfo.nType )
	{
		pLstCtrl->SetRowColor( nItem, oColor.clr_LineOff, RGB(0,0,0) );
	}
	
}

void CMonitorClientAppDlg::OnRecvMsg(WPARAM wParam, LPARAM lParam )
{
	CEdit* pEdit = (CEdit*)GetDlgItem( IDC_EDIT_RECV );
	if( NULL == pEdit )
		return;

	CString strCurTxt;
	pEdit->GetWindowText( strCurTxt );
	CString strRcv = (char*)(lParam);
	CString strWndTxt = strCurTxt + strRcv;
	
	strWndTxt += "\r\n";
	pEdit->SetWindowText( (LPCTSTR)strWndTxt );	
}

BOOL CMonitorClientAppDlg::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
	//线程处理用到
	g_hRecvWnd = this->m_hWnd;
	CEdit* pEdit = (CEdit*)GetDlgItem( IDC_EDIT_IP );
	if( NULL != pEdit )
		pEdit->SetWindowText( m_Client.m_strServerIP );

	//初始化报警显示控件
	CListCtrl_Ex * pLstCtrl = (CListCtrl_Ex*)GetDlgItem( IDC_LIST_ALARM );
	if( NULL != pLstCtrl )
	{
		CString str[3];
		str[0] = "发出报警IP地址"; 
		str[1] = "报警时间";
		str[2] = "其它相关信息";
		pLstCtrl->InitListCtrl( str, 3 );

//		CString strR[3];
//		strR[0] = "TTXX";
//		strR[1] = "TTXX";		
//		int nRowIndex = pLstCtrl->AddRowString( strR, 3 );
//		pLstCtrl->SetRowColor( nRowIndex, RGB(160,96,96), RGB(0,0,0) );
//
//		strR[0] = "TT22";
//		nRowIndex = pLstCtrl->AddRowString( strR, 3 );
//		pLstCtrl->SetRowColor( nRowIndex, RGB(192,64,64), RGB(0,0,0) );
//
//		strR[0] = "TT33";
//		nRowIndex = pLstCtrl->AddRowString( strR, 3 );
//		pLstCtrl->SetRowColor( nRowIndex, RGB(222,32,32), RGB(0,0,0) );
//
//		strR[0] = "TT33";
//		nRowIndex = pLstCtrl->AddRowString( strR, 3 );
//		pLstCtrl->SetRowColor( nRowIndex, RGB(255,1,1), RGB(0,0,0) );
	}

	//自动连接
	//m_Client.m_strServerIP = strIP;	//默认192.168.254.168
//	m_Client.Init_net();	
//	m_Client.BuildLink();

	


	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMonitorClientAppDlg::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 CMonitorClientAppDlg::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();
	}
}

void CMonitorClientAppDlg::MininumWnd()
{	
	::ShowWindow(m_hWnd,SW_HIDE);
}

void CMonitorClientAppDlg::LoadTray( )
{
	NOTIFYICONDATA nid;
	nid.cbSize=sizeof(NOTIFYICONDATA);
	nid.hWnd=m_hWnd;
	nid.uID=ID_SYSTEMTRAY;
	nid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
	nid.uCallbackMessage=WM_SYSTEMTRAY;
	nid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON_NORMAL);
	strcpy(nid.szTip,CONST_STR_TRAY_TITLE);
	::Shell_NotifyIcon(NIM_ADD,&nid);
}

void CMonitorClientAppDlg::ModifyTray( E_STATUS eStatus )
{
	NOTIFYICONDATA nid;
	nid.cbSize=sizeof(NOTIFYICONDATA);
	nid.hWnd=m_hWnd;
	nid.uID=ID_SYSTEMTRAY;
	nid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
	nid.uCallbackMessage=WM_SYSTEMTRAY;
	if( STATUS_NORMAL == eStatus )
	{
		nid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON_NORMAL);
	}
	else if( STATUS_EXCEPTION == eStatus )
	{
		nid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON_EXCEPTION);
	}
	else if( STATUS_ALARM == eStatus )
	{
		nid.hIcon = AfxGetApp()->LoadIcon(IDI_ICON_ALARM );
	}
	strcpy(nid.szTip,CONST_STR_TRAY_TITLE);
	::Shell_NotifyIcon(NIM_MODIFY,&nid);
}

void CMonitorClientAppDlg::DestroyTray()
{
	NOTIFYICONDATA nid;
	nid.cbSize=sizeof(NOTIFYICONDATA);
	nid.hWnd=m_hWnd;
	nid.uID=ID_SYSTEMTRAY;
	nid.uFlags=0;
	::Shell_NotifyIcon(NIM_DELETE,&nid);
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMonitorClientAppDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMonitorClientAppDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here//

	m_Client.Init_net();	
	if( !m_Client.BuildLink() )
		return;
	
	CString strName = m_Client.GetCurComputerName();
	m_Client.SendDataToServer( GET_LAST_ALARM_LOG, strName, strName.GetLength() );
}

//发送数据
void CMonitorClientAppDlg::OnButton2() 
{

	CEdit* pEdit = (CEdit*)GetDlgItem( IDC_EDIT_SEND );
	if( NULL == pEdit )
		return;

	CString strTxtSend,strWndTxt;
	pEdit->GetWindowText( strWndTxt );


	char szhostname[128];   	
	gethostname(szhostname,128);
	CString strName( szhostname );
	strTxtSend = strName + CString(":") + strWndTxt;


	m_Client.SendDataToServer( SEND_ALL_CLIENT, (LPCTSTR)strTxtSend, strTxtSend.GetLength() );
	
}

void CMonitorClientAppDlg::OnButton3() 
{
	CEdit* pEdit = (CEdit*)GetDlgItem( IDC_EDIT_IP );
	if( NULL == pEdit ) return ;

	CString strCmp("127.0.0.1");
	if( strCmp.Compare( (LPCTSTR)m_Client.m_strServerIP ) == 0 )	
		m_Client.m_strServerIP = "192.168.254.168";
	else
		m_Client.m_strServerIP = "127.0.0.1";

	pEdit->SetWindowText( m_Client.m_strServerIP );
}

void CMonitorClientAppDlg::OnButton4() 
{

	m_Client.DisconnectLink();
	m_oDataLog.SaveLogToFile();
	
}

BOOL CMonitorClientAppDlg::DestroyWindow() 
{
	return CDialog::DestroyWindow();
}

void CMonitorClientAppDlg::OnButton5() 
{
	//清空
	CEdit* pEdit = (CEdit*)GetDlgItem( IDC_EDIT_RECV );
	if( NULL == pEdit )
		return;

	pEdit->SetWindowText("");
}

void CMonitorClientAppDlg::CheckedAlarmInfo()
{
	//关闭报警
	m_Client.StopBeep();//关闭线程 
	if( STATUS_EXCEPTION != m_eCurStatus )
		m_eCurStatus = STATUS_NORMAL;

	//清空当前显示的报警信息	
	//m_ctrlLstAlarm.DeleteAllItems();

	//改变颜色
	//::PostMessage(m_hWnd,WM_RECVDATA_MODIFY_TRAY,WPARAM(STATUS_NORMAL),NULL );

	KillTimer( 1 );
}

//确认已查看报警信息
void CMonitorClientAppDlg::OnButton6() 
{
	CheckedAlarmInfo();
}

void CMonitorClientAppDlg::PostNcDestroy() 
{
	// TODO: Add your specialized code here and/or call the base class	
	CDialog::PostNcDestroy();
}

BOOL CMonitorClientAppDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
	// TODO: Add your specialized code here and/or call the base class//
	
	return CDialog::OnNotify(wParam, lParam, pResult);
}

void CMonitorClientAppDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	m_Client.DisconnectLink();

	CheckedAlarmInfo();

	DestroyTray();	

	m_oDataLog.SaveLogToFile();

}

void CMonitorClientAppDlg::OnButton7() 
{
	MininumWnd();
	//加裁托盘
	LoadTray();
	//显示当前状态
	ModifyTray( m_eCurStatus );

}

void CMonitorClientAppDlg::OnTrayClose() 
{	
	CMonitorClientAppDlg::OnCancel();	
	
}

void CMonitorClientAppDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

	static nS = 1;
	if( 1 == nIDEvent ) //专门用于报警的线程
	{
		if( 1 == nS )
		{
			nS = 0;
			ModifyTray( STATUS_NORMAL );
		}
		else
		{
			nS = 1;
			ModifyTray( STATUS_ALARM );
		}		

	}
	
	CDialog::OnTimer(nIDEvent);
}

void CMonitorClientAppDlg::OnButtonServersetting() 
{
	CDlgServerSetting oDlg;
	oDlg.m_strIP = m_Client.m_strServerIP;
	oDlg.m_nPort = m_Client.m_nListenPort;

	if( IDOK != oDlg.DoModal() )
		return;

	m_Client.m_strServerIP = oDlg.m_strIP;
	m_Client.m_nListenPort = oDlg.m_nPort;
	
}

void CMonitorClientAppDlg::OnButtonOpenie() 
{
	//WinExec("C:\\Program Files\\Internet Explorer\\IEXPLORE.exe   http://www.baidu.com/",SW_SHOW);
	
	CString strURLParameter;   //http://www.baidu.com/s?wd=%BC%BE%C7%EF%B2%A8
	strURLParameter = "s?wd=%BC%BE%C7%EF%B2%A8"; //"?strType=Modify";   
	WinExec("C:\\Program Files\\Internet Explorer\\IEXPLORE.exe   http://www.baidu.com/"+strURLParameter,SW_SHOW);   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -