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

📄 groupview.cpp

📁 KepWare的OPC Client 示例.面向C
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnReconnect ()
	{
	// Get reference to our tree control:
	CTreeCtrl &cTree = GetTreeCtrl ();

	// Get handle of selected item.  Handle will be NULL if no item is 
	// selected.  We should only get called if a server is selected.
	HTREEITEM hItem = cTree.GetSelectedItem ();

	if (hItem)
		{
		// A server had better be selected.  This will be the case if handle
		// of parent is NULL.  Check this (debug only).
		ASSERT (cTree.GetParentItem (hItem) == NULL);

		// Get pointer to our document object.  (This could be NULL if
		// we did not get attached to document properly.)
		CKDocument *pDoc = (CKDocument *) GetDocument ();
		
		if (pDoc)
			{
			// Pointer to CKServer object is saved in tree item data
			// parameter:
			CKServer *pServer = (CKServer *) cTree.GetItemData (hItem);

			// Tell document to disconnect server first,
			pDoc->DisconnectServer (pServer);

			// then connect again:
			pDoc->ConnectServer (pServer);
			}
		}
	}

// **************************************************************************
// OnGetErrorString ()
//
// Description:
//	Get Error String menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnGetErrorString () 
	{
	// Get reference to our tree control:
	CTreeCtrl &cTree = GetTreeCtrl ();

	// Get handle of selected item.  Handle will be NULL if no item is 
	// selected.  We should only get called if a server is selected.
	HTREEITEM hItem = cTree.GetSelectedItem ();

	// A server had better be selected.  This will be the case if handle
	// of parent is NULL.  Check this (debug only).
	ASSERT (cTree.GetParentItem (hItem) == NULL);

	// Create a "Get error string" dialog.  Give it a pointer to the server
	// object (saved as tree item data parameter).
	CKServerGetErrorStringDlg dlg ((CKServer *)cTree.GetItemData (hItem));

	// Show as a modal dialog:
	dlg.DoModal ();
	}

// **************************************************************************
// OnGetGroupByName ()
//
// Description:
//	Get Group By Name menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnGetGroupByName () 
	{
	// Get reference to our tree control:
	CTreeCtrl &cTree = GetTreeCtrl ();

	// Get handle of selected item.  Handle will be NULL if no item is 
	// selected.  We should only get called if a server is selected.
	HTREEITEM hItem = cTree.GetSelectedItem ();

	// A server had better be selected.  This will be the case if handle
	// of parent is NULL.  Check this (debug only).
	ASSERT (cTree.GetParentItem (hItem) == NULL);

	// Pointer to CKServer object is saved in tree item data
	// parameter:
	CKServer *pServer = (CKServer *)cTree.GetItemData (hItem);
	ASSERT (pServer != NULL);

	// Create a "Get group by name" dialog.  Give it a pointer to the
	// server's IOPCServer COM interface:
	CKServerGroupByNameDlg dlg (pServer->GetIServer ());

	// Show as a modal dialog:
	dlg.DoModal ();
	}

// **************************************************************************
// OnEnumerateGroups ()
//
// Description:
//	Enumerate Groups menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnEnumerateGroups () 
	{
	// Get reference to our tree control:
	CTreeCtrl &cTree = GetTreeCtrl ();

	// Get handle of selected item.  Handle will be NULL if no item is 
	// selected.  We should only get called if a server is selected.
	HTREEITEM hItem = cTree.GetSelectedItem ();

	// A server had better be selected.  This will be the case if handle
	// of parent is NULL.  Check this (debug only).
	ASSERT (cTree.GetParentItem (hItem) == NULL);

	// Pointer to CKServer object is saved in tree item data
	// parameter:
	CKServer *pServer = (CKServer *)cTree.GetItemData (hItem);
	ASSERT (pServer != NULL);

	// Create an "Enumerate groups" dialog.  Give it a pointer to the
	// server's IOPCServer COM interface:
	CKServerEnumerateGroupsDlg dlg (pServer->GetIServer ());

	// Show as a modal dialog:
	dlg.DoModal ();
	}

// **************************************************************************
// OnCloneGroup ()
//
// Description:
//	Clone Group menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnCloneGroup () 
	{
	// Get pointer to our document.  (Pointer could be NULL if we did not
	// get attached to document properly.)
	CKDocument *pDoc = (CKDocument *) GetDocument ();

	// Tell document to clone selected group.  Document should have been
	// advised of selected group before we get called.
	if (pDoc)
		pDoc->CloneGroup ();
	}

// **************************************************************************
// OnChar ()
//
// Description:
//	Handle keyboard input.  Switch view on tab, etc.
//
// Parameters:
//  UINT		nChar		Character code
//	UINT		nRepCnt		repeat count
//	UINT		nFlags		Flags
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags) 
	{
	// Switch views on TAB:
	if (nChar == VK_TAB)
		{
		// Post a change view message to main window:
		AfxGetMainWnd ()->PostMessage (UM_CHANGEVIEW, 0, (LPARAM)this);

		// Return now so default processing does not occur:
		return;
		}
	
	// Show context menu on SPACEBAR:
	else if (nChar == VK_SPACE)
		{
		// Get reference to our tree control:
		CTreeCtrl &cTree = GetTreeCtrl ();

		// Get handle of selected item.
		HTREEITEM hItem = cTree.GetSelectedItem ();
		
		// Get rectange of selected item:
		CRect rc;
		cTree.GetItemRect (hItem, &rc, true);

		// Compute the coordinates of rectangle center:
 		rc.left = (rc.left + rc.right) / 2;
		rc.top = (rc.top + rc.bottom) / 2;

		// Use code in OnRButtonDown() to construct the context menu.  Pass
		// it the coordinates of rectangle's center.  (That's where it will
		// place the context menu.)
		OnRButtonDown (0, CPoint (rc.left, rc.top)); 

		// Return now so default processing will not occur:
		return;
		}

	// Show properties on return:
	else if (nChar == VK_RETURN)
		{
		// Use code in OnProperties() to display properties of selected item:
		if (GetTreeCtrl ().GetSelectedItem ())
			OnProperties ();

		// Return now so default processing will not occur:
		return;
		}
	
	// Perform default processing for all other characters:
	CTreeView::OnChar (nChar, nRepCnt, nFlags);
	}

// **************************************************************************
// OnExportCsv ()
//
// Description:
//	Export CSV menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnExportCsv () 
	{
	// Get reference to our tree control:
	CTreeCtrl &cTree = GetTreeCtrl ();

	// Get handle of selected item.  (We shouldn't get called unless a group
	// is selected.)
	HTREEITEM hItem = cTree.GetSelectedItem ();
	ASSERT (hItem);

	// Pointer to group object is saved in tree item data parameter:
	CKGroup *pGroup = (CKGroup *) cTree.GetItemData (hItem);
	ASSERT (pGroup && (cTree.GetParentItem (hItem) != NULL));

	// Declare some strings we will need for common file save dialog:
	CString strInitialPath;
	CString strFilter;
	CString strTitle;

	// Load the common file save dialog filter string from string resources:
	strFilter.LoadString (IDS_CSVFILEFILTER);
	
	// Load the common file save dialog title from string resources:
	strTitle.LoadString (IDS_CSVEXPORT);

	// Define the initial path string (always to project directory):
	strInitialPath = AfxGetApp ()->GetProfileString (lpszPaths, lpszOpenFile, _T(""));

	// Create the common file save dialog:
	CFileDialog ofn (
		FALSE,									// Not an open dialog
		lpszCSV, 								// Default extension (CSV)
		pGroup->GetName (),						// Suggested file name is group name
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, // Flags (hide read only files, prompt if attemt to overwrite)
		strFilter);								// Filter set

	// Set initial path and title:
	ofn.m_ofn.lpstrInitialDir = strInitialPath;
	ofn.m_ofn.lpstrTitle = strTitle;

	// Show as modal dialog.  Unless user hits "OK", return:
	if (ofn.DoModal () != IDOK)
		return;

	// If we make it here, user specified all necessary data in common file save
	// dialog, and hit "OK".  Create a CStdioFile to make file IO easy for us.
	CStdioFile csv;

	// Create a file exception object, just in case:
	CFileException fe;

	// Open the file.  Get file name from common file save dialog.  Open file
	// as readable// writeable, create if necessary, as text.  If failure, 
	// report error and return:
	if (!csv.Open (ofn.GetPathName (), CFile::modeReadWrite | CFile::modeCreate | CFile::typeText, &fe))
		{
		// Let MFC report the open failure:
		fe.ReportError ();
		return;
		}

	// Perform the export:
	try
		{
		// Create a wait cursor object.  This will cause the wait cursor, 
		// usually an hourglass, to be displayed.  When this object goes
		// out of scope, its destructor will restore the previous cursor
		// type.
		CWaitCursor wc;

		// Call the group's ExportCsv function to save properties to file:
		pGroup->ExportCsv (csv);
		}
	
	// If anything happens, delete the file:
	catch (...)
		{
		TRACE (_T("OTC: Exception thrown during CSV export -> deleting the output file\n"));
		csv.Close ();
		CFile::Remove (ofn.GetPathName ());
		}
	}

// **************************************************************************
// OnImportCsv ()
//
// Description:
//	Import CSV menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnImportCsv () 
	{
	// Get reference to our tree control:
	CTreeCtrl &cTree = GetTreeCtrl ();

	// Get handle of selected item.  (We shouldn't get called unless a group
	// is selected.)
	HTREEITEM hItem = cTree.GetSelectedItem ();
	ASSERT (hItem);

	// Pointer to group object is saved in tree item data parameter:
	CKGroup *pGroup = (CKGroup *) cTree.GetItemData (hItem);
	ASSERT (pGroup && (cTree.GetParentItem (hItem) != NULL));

	// Declare some strings we will need for common file save dialog:
	CString strInitialPath;
	CString strFilter;
	CString strTitle;

	// Load the common file open dialog filter string from string resources:
	strFilter.LoadString (IDS_CSVFILEFILTER);
	
	// Load the common file open dialog title from string resources:
	strTitle.LoadString (IDS_CSVIMPORT);

	// Define the initial path string (always to project directory):
	strInitialPath = AfxGetApp ()->GetProfileString (lpszPaths, lpszOpenFile, _T(""));

	// Create the common file open dialog:
	CFileDialog ofn (
		TRUE,									// open dialog
		lpszCSV, 								// Default extension (CSV)
		pGroup->GetName (),						// Suggested file name is group name
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, // Flags (hide read only files, prompt if attemt to overwrite)
		strFilter);								// Filter set

	// Set initial path and title:
	ofn.m_ofn.lpstrInitialDir = strInitialPath;
	ofn.m_ofn.lpstrTitle = strTitle;

	// Show as modal dialog.  Unless user hits "OK", return:
	if (ofn.DoModal () != IDOK)
		return;

	// If we make it here, user specified all necessary data in common file open
	// dialog, and hit "OK".  Create a CStdioFile to make file IO easy for us.
	CStdioFile csv;

	// Create a file exception object, just in case:
	CFileException fe;

	// Open the file.  Get file name from common file open dialog.  Open file
	// as read only, text, and deny other applications simultaneous write 
	// access.  If failure, report error and return:
	if (!csv.Open (ofn.GetPathName (), CFile::modeRead | CFile::typeText | CFile::shareDenyWrite, &fe))
		{
		// Let MFC report the open failure:
		fe.ReportError ();
		return;
		}

	// Perform the import:
	try
		{
		// Create a wait cursor object.  This will cause the wait cursor, 
		// usually an hourglass, to be displayed.  When this object goes
		// out of scope, its destructor will restore the previous cursor
		// type.
		CWaitCursor wc;

		// Declare an object array to hold a list of the group's items to
		// import:
		CObArray cItemList;
		DWORD cdwItems = 0;

		// Call the group's ImportCsv function to read the group and item
		// properties from the file.  The object array cItemList will be
		// loaded by this function.
		pGroup->ImportCsv (csv, cItemList, cdwItems);

		// If the group has items, indicated by a non-zero cdwItems, tell
		// the document to add these items:
		if (cdwItems)
			((CKDocument *) GetDocument ())->AddItems (cItemList, cdwItems);
		}
	
	catch (...)
		{
		TRACE (_T("OTC: Exception thrown during CSV import\n"));
		}
	}

⌨️ 快捷键说明

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