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

📄 document.cpp

📁 VisualC OPC Client Example C 程序开发
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			// Loop over servers:
			for (DWORD dwIndex = 0; dwIndex < cdwServers; dwIndex++)
				{
				// Create a reusable pointer to a CKServer object:
				CKServer *pServer = NULL;

				// Instantiate a new CKServer object:
				pServer = new CKServer ();

				// Load it's settings from archive:
				pServer->Serialize (ar);

				// Add the server to the project:
				AddServer (pServer);
				}
			}
		}

	// Catch archive exceptions.  Any object exceptions thrown will be
	// self-deleted by the object so we do not have to worry about memory
	// leaks at this outer-level.
	catch (CArchiveException *ae)
		{
		// Make a copy of the cause of the exception before we delete it:
		int nCause = ae->m_cause;

		// Delete the exception:
		ae->Delete ();
		
		// Re-throw the exception to be processed by the framework:
		AfxThrowArchiveException (nCause);
		}

	// Catch any other kind of excetion and re-throw it to be processed
	// by the framework:
	catch (...)
		{
		AfxThrowArchiveException (CArchiveException::generic);		
		}
	}


/////////////////////////////////////////////////////////////////////////////
// CKDocument diagnostics
/////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
void CKDocument::AssertValid () const
	{
	CDocument::AssertValid ();
	}

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


/////////////////////////////////////////////////////////////////////////////
// CKDocument commands
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
// RouteCmdMsg ()
//
// Description:
//	Routes a message onto all inactive views or until one of the views 
//	processes the message.
//
// Parameters:
//	CView				*pActiveView	Pointer to the active view.
//	UINT				nID				Specifies the container for the 
//										  command identifier. 
//	int					nCode			Identifies the command notification
//										  code.
//	void				*pExtra			Used according to the value of 
//										  nCode.
//	AFX_CMDHANDLERINFO	*pHandlerInfo	If not NULL, OnCmdMsg fills in the
//										  pTarget and pmf members of the 
//										  pHandlerInfo structure instead of
//										  dispatching the command. Typically, 
//										  this parameter should be NULL.
//
// Returns:
//  BOOL - TRUE if message was processed.
// **************************************************************************
BOOL CKDocument::RouteCmdMsg (CView *pActiveView, UINT nID, int nCode, void *pExtra, AFX_CMDHANDLERINFO *pHandlerInfo)
	{
	// Get the position of the first view in the list of views associated
	// with this document:
	POSITION pos = GetFirstViewPosition ();

	// Exhaust all views.  When we get to end of list of views, pos will be NULL:
	while (pos != NULL)
		{
		// Get pointer to view at present POSITION (pos).  Function will
		// automatically reset pos to POSITION of next view, or NULL if 
		// end of list:
		CView *pNextView = GetNextView (pos);

		// By definition, we do no send message to active view:
		// (We shouldn't get called if active view processes the message.)
		if (pNextView != pActiveView)
			{
			// If the view processes the message then we are a success.  Return TRUE
			// to indicate message was processed:
			if (((CCmdTarget *) pNextView)->OnCmdMsg (nID, nCode, pExtra, pHandlerInfo))
				return (TRUE);
			}
		}

	// If we make it here, then message not processed.  Return FALSE to
	// indicate that message was not processed.
	return (FALSE);
	}

// **************************************************************************
// AddServer ()
//
// Description:
//	Allows for the addition of a new server item to the document.  Posts a 
//	dialog to enter server attributes and attaches it to the document list.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKDocument::AddServer ()
	{
	// Create a server property sheet object:
	CKServerPropertySheet psh;

	// Show as modal property sheet.  If user hits "OK" then add a server
	// to the project with the settings made in property sheet:
	if (psh.DoModal () == IDOK)
		{
		// Get pointer to server object created and configured in property
		// sheet:
		CKServer *pServer = psh.GetServer ();

		// Add the server to our list:
		AddServer (pServer);

		// Notify all views that new server has been addeded:
		UpdateAllViews (NULL, HINT_ADD_SERVER, pServer);

		// Set modified flag:
		SetModified ();
		}
	}

// **************************************************************************
// AddServer ()
//
// Description:
//	Adds a server that was created during document load from disk or during a
//	cut and paste operation to the document.  
//
// Parameters:
//	CKServer	*pServer		Pointer to server object to add.
//	bool		bConnect		Set to true to connect to server after add.
//
// Returns:
//  void
// **************************************************************************
void CKDocument::AddServer (CKServer *pServer, bool bConnect /* = false */)
	{
	// Check that pointer to server is not NULL (for debug only):
	ASSERT (pServer != NULL);

	// Add the server to head of linked list list.

	// Next item will be previous head of linked list:
	pServer->SetNext (m_pServerHead);

	// If we had a head to the linked list, it's previous item will be
	// new server:
	if (m_pServerHead)	
		m_pServerHead->SetPrev (pServer);
		
	// New server is now the head of the linked list:
	m_pServerHead = pServer;

	// Increment the server count:
	++m_cdwServers;			
				
	// Connect to the OPC Server if asked:
	if (bConnect)
		{
		// Start the server:
		pServer->Start ();

		// Update views to indicate new server connection status.
		// Architecture will assue that new server will get added
		// to views if we don't connect at this point.
		UpdateAllViews (NULL, HINT_ADD_SERVER_AND_GROUPS, pServer);

		// Set document modified flag:
		SetModified ();
		}
	}

// **************************************************************************
// EditServer ()
//
// Description:
//	Allows for the properties of a server item to modified.
//
// Parameters:
//	CKServer	*pServer		Pointer to server object to edit.
//
// Returns:
//  void
// **************************************************************************
void CKDocument::EditServer (CKServer *pServer)
	{
	// Check that pointer to server is not NULL (for debug only):
	ASSERT (pServer != NULL);

	// Create a server property sheet object, and give it pointer to the 
	// CKServer object to edit:
	CKServerPropertySheet psh (pServer);
	
	// Show as modal property sheet.  If user hits "OK", we need to set
	// document modified flag.  Property sheet object will apply edits to
	// server object if "OK" is hit.
	if (psh.DoModal () == IDOK)
		SetModified ();
	}

// **************************************************************************
// RemoveServer ()
//
// Description:
//	Allows for a server item to be removed from the document.
//
// Parameters:
//	CKServer	*pServer		Pointer to server object to remove.
//
// Returns:
//  void
// **************************************************************************
void CKDocument::RemoveServer (CKServer *pServer)
	{
	// Check that pointer to server is not NULL (for debug only):
	ASSERT (pServer != NULL);

	// Remove the server from our linked list:

	// Will need to save next and previous items in list before we delete 
	// server object:
	CKServer *pPrev = pServer->GetPrev ();
	CKServer *pNext = pServer->GetNext ();
	
	// If server had previous item, it's next will be server's next:
	if (pPrev)
		pPrev->SetNext (pNext);

	// If server had a next item, it's previous will be server's previous:
	if (pNext)
		pNext->SetPrev (pPrev);

	// If server was head of linked list, new head will be server's next:
	if (pServer == m_pServerHead)
		m_pServerHead = pNext;

	// Decrement the server count:
	--m_cdwServers;

	// If there are no servers left, invalidate current server and group
	// selections:
	if (!m_cdwServers)
		{
		m_pCurSelServer = NULL;
		m_pCurSelGroup = NULL;
		}

	// Notify all views that server has been removed:
	UpdateAllViews (NULL, HINT_REMOVE_SERVER, pServer);

	// Set the document modified flag:
	SetModified ();

	// We are now free to delete the server object:
	delete pServer;
	}

// **************************************************************************
// ConnectServer ()
//
// Description:
//	Connects to a server, adds all groups and items.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKDocument::ConnectServer (CKServer *pServer)
	{
	// Check that pointer to server is not NULL (for debug only):
	ASSERT (pServer != NULL);

	// Update status bar text to indicate that we are connecting::
	CKStatusBarText cText (IDS_CONNECTING);

	// Create and fill a structure to pass along to worker thread.  Structure
	// will contain pointer to server to connect to and specify a "start
	// single server" task:
	WORKERTHREADARG tArg;
	tArg.eTask = WORKERTHREADARG::START_SINGLE_SERVER;
	tArg.pvObjectA = (void *)pServer;

	// Run a worker thread to start the server:
	RunWorkerThread (&tArg);

	// Update group view to indicate new server status:
	UpdateAllViews (NULL, HINT_REFRESH_GROUPVIEW, NULL);
	}

// **************************************************************************
// DisconnectServer ()
//
// Description:
//	Disconnects a server, removes all groups and items.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKDocument::DisconnectServer (CKServer *pServer)
	{
	// Check that pointer to server is not NULL (for debug only):
	ASSERT (pServer != NULL);

	// Update status bar text to indicate that we are disconnecting:
	CKStatusBarText cText (IDS_DISCONNECTING);

	// Create and fill a structure to pass along to worker thread.  Structure
	// will contain pointer to server to disconnect to and specify a "stop
	// single server" task
	WORKERTHREADARG tArg;
	tArg.eTask = WORKERTHREADARG::STOP_SINGLE_SERVER;
	tArg.pvObjectA = (void *)pServer;

	// Run a worker thread to disconnect the server:
	RunWorkerThread (&tArg);

	// Update group view to indicate new server status:
	UpdateAllViews (NULL, HINT_REFRESH_GROUPVIEW, NULL);
	}

// **************************************************************************
// AddGroup ()
//
// Description:
//	Allows for the addition of a new group to a server item.  The group will
//	be attached the server's group list.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKDocument::AddGroup ()
	{
	// Get the currently selected server object:
	CKServer *pServer = GetSelectedServer ();
	ASSERT (pServer != NULL);

	// Create a group property sheet.  Pass it a pointer to the currently
	// selected server so that it can add the new group to it.
	CKGroupPropertySheet psh (pServer);

	// Show as modal property sheet.  If user hits "OK", we need to set
	// document modified flag.  Property sheet object will add new group to
	// server object if "OK" is hit.  
	if (psh.DoModal () == IDOK)
		{
		// Notify all views that new group has been added:
		UpdateAllViews (NULL, HINT_ADD_GROUP, psh.GetGroup ());

		// Set document modified flag:
		SetModified ();
		}
	}

// **************************************************************************
// AddGroup ()
//
// Description:
//	Adds a group that was created during document load from disk or during a
//	cut and paste operation to the document.
//
// Parameters:
//	CKGroup		*pGroup			Pointer to group object
//
// Returns:
//  void
// **************************************************************************
void CKDocument::AddGroup (CKGroup *pGroup)
	{
	// Get the currently selected server object:
	CKServer *pServer = GetSelectedServer ();
	ASSERT (pGroup != NULL);
	ASSERT (pServer != NULL);

	// Get current name of group:

⌨️ 快捷键说明

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