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

📄 opceventview.cpp

📁 OPC Client 源代码
💻 CPP
字号:
// OPCEventView.cpp : implementation file
//

#include "stdafx.h"
#include "TYOPCClient.h"
#include "OPCEventView.h"
#include "OPCData.h"

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


//
#define NUMCOLUMNS			4
#define DEFAULTCOLUMNWIDTH	120
#define GROWEVENTS			1024
#define MAXEVENTS			GROWEVENTS

static LPCTSTR lpszRegSection = _T("Event View");
static LPCTSTR lpszLogErrorsOnly = _T("Log Errors Only");

// event pane update timer
#define UPDATE_EVENTPANE_EVENT	1

/////////////////////////////////////////////////////////////////////////////
// CViewOPCEvent

IMPLEMENT_DYNCREATE(CViewOPCEvent, CListView)

CViewOPCEvent::CViewOPCEvent()
{
	m_bLogErrorsOnly=false;

	m_bLoatProject=false;
}

CViewOPCEvent::~CViewOPCEvent()
{
}


BEGIN_MESSAGE_MAP(CViewOPCEvent, CListView)
	//{{AFX_MSG_MAP(CViewOPCEvent)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CViewOPCEvent drawing

void CViewOPCEvent::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CViewOPCEvent diagnostics

#ifdef _DEBUG
void CViewOPCEvent::AssertValid() const
{
	CListView::AssertValid();
}

void CViewOPCEvent::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CViewOPCEvent message handlers

int CViewOPCEvent::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CListView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// Initialize the image list for the items.  These are the images that
	// show up to the left of each item in list.  The bitmap must use a 
	// purple background color, RGB (255, 0, 255), so that the CImageList
	// object can construct a mask.  The images are 16x16 pixels.  Set the
	// image list background color to CLR_NONE so masked pixels will be
	// transparent. 
	//
	// Image number		Use
	//	0				Information event
	//	1				Error event
	m_cImageList.Create (IDB_EVENTIMAGES, 16, 4, RGB (255, 0, 255));
	m_cImageList.SetBkColor (CLR_NONE);
	GetListCtrl ().SetImageList (&m_cImageList, LVSIL_SMALL);

	// Create columns.  Column width used in last session were saved in
	// registry. Use these widths for the present session.
	CString strColumnTitle;
	int nFormat=LVCFMT_CENTER;
	for (int i = 0; i < NUMCOLUMNS; i++)
		{
		// Create a string that will identify the registry entry for each column:
		TCHAR szNum [8];
		wsprintf (szNum, _T("%d"), i);

		// Read the column width from the registry:
		int nWidth = AfxGetApp ()->GetProfileInt (lpszRegSection, szNum, DEFAULTCOLUMNWIDTH);

		// Make sure it is valid:
		if (nWidth < 0)
			nWidth = DEFAULTCOLUMNWIDTH;

		// Load the column title string resources:
		switch (i)
			{
			case 0: // Date
				strColumnTitle.LoadString (IDS_DATE);
				break;

			case 1: // Time
				strColumnTitle.LoadString (IDS_TIME);
				break;

			case 2:	// Event
				strColumnTitle.LoadString (IDS_EVENT);
				break;

			case 3:	// NOTE
				strColumnTitle.LoadString (IDS_NOTE);
				nFormat=LVCFMT_LEFT;
				nWidth*=3;
				break;
			default:
				ASSERT (FALSE);
				break;
			}

		// Insert the column:
		GetListCtrl ().InsertColumn (i, strColumnTitle, nFormat, nWidth);
		}

	GetListCtrl ().ModifyStyle (NULL,CCS_TOP|LVS_SHOWSELALWAYS);	//LVS_EDITLABELS|允许就地编辑
	GetListCtrl ().SetExtendedStyle (LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);

	// Get the "log errors only" flag saved in registry last time:
	m_bLogErrorsOnly = AfxGetApp ()->GetProfileInt (lpszRegSection, lpszLogErrorsOnly, FALSE) ? true : false;

	// Start update view timer:
//	SetTimer (UPDATE_EVENTPANE_EVENT, 100, NULL);
	
	ShowWindow(TRUE);
	return 0;
}

BOOL CViewOPCEvent::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	// Modify the window style so that list view has report format and
	// column headers that do not sort rows:
	cs.style |= LVS_REPORT | LVS_NOSORTHEADER; 
//	cs.style &= ~(LVS_ICON | LVS_SMALLICON | LVS_LIST);
//	cs.style |= (LVS_REPORT | LVS_OWNERDATA);

	return CListView::PreCreateWindow(cs);
}

void CViewOPCEvent::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
//	return;
	if(NULL==pHint)
		return;
	CString strEvent;
	CString strTip;
	try{
		if(lHint>HINT_EVENT_OFFSET)
		{
			strEvent=_T("操作");
			strTip=*(CString*)pHint;
			InsertItem(strEvent,strTip);
		}
		else if(lHint>=HINT_APP_SYSTEM)
		{
			if(HINT_NEW_DOCUMENT==lHint
			 ||HINT_OPEN_DOCUMENT==lHint)
				GetListCtrl ().DeleteAllItems ();

			strEvent=_T("系统");
			strTip=*(CString*)pHint;
			InsertItem(strEvent,strTip);
		}
		else
		{
		TRACE(_T("CViewOPCEvent::OnUpdate 错误!"));
		}
	}catch(...)
	{
		AfxMessageBox(_T("CViewOPCEvent::OnUpdate 错误!"), MB_OK| MB_ICONQUESTION);
	}
	return;
	//
	switch (lHint)
	{
	case HINT_REMPC_ADD:		//增加计算机
		{	
			ASSERT (pHint != NULL);
			COPCRemoteMachine* pOPCRM=(COPCRemoteMachine*)pHint;
			strEvent=_T("操作");
			strTip.Format (_T("增加计算机----%s"),pOPCRM->GetName ());
			InsertItem(strEvent,strTip);
			break;
		}
	case HINT_REMPC_REMOVE:	//删除计算机
		{
		ASSERT (pHint != NULL);		
		COPCRemoteMachine* pOPCRM=(COPCRemoteMachine*)pHint;

		strEvent=_T("操作");
		strTip.Format (_T("删除计算机----%s"),pOPCRM->GetName ());
		InsertItem(strEvent,strTip);
		break;
		}
	case HINT_SERVER_ADD:		//增加服务器
		{
			ASSERT (pHint != NULL);		// pHint contains COPCServer *
			COPCServer* pOPCServer=(COPCServer*)pHint;
			strEvent=_T("操作");
			strTip.Format (_T("增加服务器----%s"),pOPCServer->GetName ());
			InsertItem(strEvent,strTip);
			break;
		}
		case HINT_REMOVE_SERVER:	//删除服务器
			{
			ASSERT (pHint != NULL);		// pHint contains COPCServer *
			COPCServer* pOPCServer=(COPCServer*)pHint;
			strEvent=_T("操作");
			strTip.Format (_T("删除服务器----%s"),pOPCServer->GetName ());
			InsertItem(strEvent,strTip);
			break;
			}
			
		case HINT_GROUP_ADD:		//增加组
			{
			ASSERT (pHint != NULL);		// pHint contains COPCGroup *
			COPCGroup *pGroup = (COPCGroup *)pHint;
			strEvent=_T("操作");
			strTip.Format (_T("增加组----%s"),pGroup->GetName ());
			InsertItem(strEvent,strTip);

			break;
			}

		case HINT_GROUP_REMOVE:		//删除组
			{
			ASSERT (pHint != NULL);		// pHint contains COPCGroup *
			COPCGroup *pGroup = (COPCGroup *)pHint;
			strEvent=_T("操作");
			strTip.Format (_T("删除组----%s"),pGroup->GetName ());
			InsertItem(strEvent,strTip);
			break;
			}
	case HINT_ITEM_ADD:	
		{
			ASSERT (pHint != NULL);
			
			// Get pointer to the item list:
			CObArray *pAddItems = (CObArray *)pHint;

			COPCItem *pItem = NULL;

			strEvent=_T("操作");
			for(int nIndex=0;nIndex<=pAddItems->GetUpperBound ();nIndex++)
			{
				pItem = (COPCItem *) pAddItems->GetAt (nIndex);
				strTip.Format (_T("增加项----%s"),pItem->GetItemID ());
				InsertItem(strEvent,strTip);
			}

			break;
		}
	case HINT_ITEM_DELETE:
		{
			ASSERT (pHint != NULL);
			
			// Get pointer to the item list:
			CStringArray *pAddItems = (CStringArray *)pHint;

			strEvent=_T("操作");
			for(int nIndex=0;nIndex<=pAddItems->GetUpperBound ();nIndex++)
			{
				strTip.Format (_T("删除项----%s"),pAddItems->GetAt (nIndex));
				InsertItem(strEvent,strTip);
			}
			break;
		}
		case HINT_NEW_DOCUMENT:
		case HINT_OPEN_DOCUMENT:
			{
				CListCtrl &cList = GetListCtrl ();
				cList.DeleteAllItems ();
				break;
			}
//	case HINT_EVENT_TIP:		//信息提示
//		{
//			CString* pStrTip=(CString*)pHint;
//			GetListCtrl ().InsertItem (1,*pStrTip,0);

//			break;
//		}
	default:
		;
	}

}

void CViewOPCEvent::InsertItem(CString &strEvent,CString &strTip)
{	
	SYSTEMTIME systime;
	::GetSystemTime (&systime);
	CString strDate;
	CString strTime;

	strDate.Format (_T("%4d-%2d-%2d"),
		systime.wYear ,systime.wMonth ,systime.wDay );

	strTime.Format (_T("%02d:%02d:%02d:%03d"), 
		systime.wHour, systime.wMinute, systime.wSecond, systime.wMilliseconds);			
				
	GetListCtrl ().InsertItem (0,strDate,0);	// Date
	GetListCtrl ().SetItemText (0,1,strTime);	// Time
	GetListCtrl ().SetItemText (0,2,strEvent);	// Event
	GetListCtrl ().SetItemText (0,3,strTip);	// 描述
}

void CViewOPCEvent::LogMsg(UINT unEventID, CString &strInfor)
{
	TRACE(_T("%s\n"),strInfor);
	CString strEvent;
	switch(unEventID)
	{
	//加载事件
	case HINT_LOAD_PROJECT:
		m_bLoatProject=true;
	case HINT_LOAD_PROJECT_END:
		m_bLoatProject=false;
	case HINT_CLOSE_PROJECT:
		strEvent="加载";
		break;
	//系统事件
	case HINT_SYS_EVENT:
		strEvent="系统";
		break;
	
	//操作事件
	default:
		if(m_bLoatProject)
			strEvent="加载";
		else
			strEvent="操作";
	}
	//
	InsertItem(strEvent,strInfor);

}

⌨️ 快捷键说明

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