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

📄 groupview.cpp

📁 KepWare的OPC Client 示例.面向C
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				// Add Get Error String menu item:
				pMainMenu->GetMenuString (ID_TOOLS_GET_ERROR_STRING, strMenuText, MF_BYCOMMAND);
				cMenu.AppendMenu (nFlags, ID_TOOLS_GET_ERROR_STRING, strMenuText);

				// Add separator:
				cMenu.AppendMenu (MF_SEPARATOR);

				// Add Enumerate Groups menu item:
				pMainMenu->GetMenuString (ID_TOOLS_ENUMERATE_GROUPS, strMenuText, MF_BYCOMMAND);
				cMenu.AppendMenu (nFlags, ID_TOOLS_ENUMERATE_GROUPS, strMenuText);
				
				// Add Get Group By Name menu item:
				pMainMenu->GetMenuString (ID_TOOLS_GET_GROUP_BY_NAME, strMenuText, MF_BYCOMMAND);
				cMenu.AppendMenu (nFlags, ID_TOOLS_GET_GROUP_BY_NAME, strMenuText);
				}

			// Else click was on a group:
			else 
				{
				// Add Add Item menu item:
				pMainMenu->GetMenuString (ID_EDIT_NEWITEM, strMenuText, MF_BYCOMMAND);
				cMenu.AppendMenu (nFlags, ID_EDIT_NEWITEM, strMenuText);

				// Add separator:
				cMenu.AppendMenu (MF_SEPARATOR);

				// Add Clone Group menu item:
				pMainMenu->GetMenuString (ID_TOOLS_CLONE_GROUP, strMenuText, MF_BYCOMMAND);
				cMenu.AppendMenu (nFlags, ID_TOOLS_CLONE_GROUP, strMenuText);

				// Add separator:
				cMenu.AppendMenu (MF_SEPARATOR);

				// Add Export CSV menu item:
				pMainMenu->GetMenuString (ID_TOOLS_EXPORT_CSV, strMenuText, MF_BYCOMMAND);
				cMenu.AppendMenu (nFlags, ID_TOOLS_EXPORT_CSV, strMenuText);

				// Add Import CSV menu item:
				pMainMenu->GetMenuString (ID_TOOLS_IMPORT_CSV, strMenuText, MF_BYCOMMAND);
				cMenu.AppendMenu (nFlags, ID_TOOLS_IMPORT_CSV, strMenuText);
				}
			
			// Add separator:
			cMenu.AppendMenu (MF_SEPARATOR);
	
			// Add Cut menu item:
			pMainMenu->GetMenuString (ID_EDIT_CUT, strMenuText, MF_BYCOMMAND);
			cMenu.AppendMenu (nFlags, ID_EDIT_CUT, strMenuText);

			// Add Copy menu item:
			pMainMenu->GetMenuString (ID_EDIT_COPY, strMenuText, MF_BYCOMMAND);
			cMenu.AppendMenu (nFlags, ID_EDIT_COPY, strMenuText);

			// Add Paste menu item:
			pMainMenu->GetMenuString (ID_EDIT_PASTE, strMenuText, MF_BYCOMMAND);
			cMenu.AppendMenu (nFlags, ID_EDIT_PASTE, strMenuText);

			// Add Delete menu item:
			pMainMenu->GetMenuString (ID_EDIT_DELETE, strMenuText, MF_BYCOMMAND);
			cMenu.AppendMenu (nFlags, ID_EDIT_DELETE, strMenuText);

			// Add separator:
			cMenu.AppendMenu (MF_SEPARATOR);

			// Add Properties menu item:
			pMainMenu->GetMenuString (ID_EDIT_PROPERTIES, strMenuText, MF_BYCOMMAND);
			cMenu.AppendMenu (nFlags, ID_EDIT_PROPERTIES, strMenuText);
			}
		
		// Else click was on the background:
		else
			{
			// Add Add Server Connection menu item:
			pMainMenu->GetMenuString (ID_EDIT_NEWSERVER, strMenuText, MF_BYCOMMAND);
			cMenu.AppendMenu (nFlags, ID_EDIT_NEWSERVER, strMenuText);

			// Check format of clipboard contents to see if it contains
			// a server connection.  If so, add a paste menu item.
			if (IsClipboardFormatAvailable (CF_SERVER))
				{
				// Add separator:
				cMenu.AppendMenu (MF_SEPARATOR);

				// Add Paste menu item:
				pMainMenu->GetMenuString (ID_EDIT_PASTE, strMenuText, MF_BYCOMMAND);
				cMenu.AppendMenu (nFlags, ID_EDIT_PASTE, strMenuText);
				}
			}

		// Place the popup menu at the point of right mouse click, and route
		// all WM_COMMAND messages though the frame:
		if (cMenu.GetMenuItemCount () > 0)
			cMenu.TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd ());
		}
	}

// **************************************************************************
// OnLButtonDblClk ()
//
// Description:
//	Process left button double click messages for this view.  Display the 
//	properties for the item selected.
//
// Parameters:
//  UINT		uFlags		Indicates whether various virtual keys are down.
//	CPoint		point		Specifies the x and y coordinates of the cursor. 
//							  These coordinates are always relative to the
//							  upper-left corner of the window.
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnLButtonDblClk (UINT nFlags, CPoint point) 
	{
	// Get pointer to our document.  We will need it to display a property
	// sheet for selected item.
	CKDocument *pDoc = (CKDocument *) GetDocument ();
	ASSERT (pDoc != NULL);

	// Get reference to tree control:
	CTreeCtrl &cTree = GetTreeCtrl ();
	HTREEITEM hItem;
	UINT uHitFlags;

	// Get a handle to the item that was clicked on.  (Handle will
	// be NULL if click was not on an item.)
	hItem = cTree.HitTest (point, &uHitFlags);

	// If there was an item we clicked select it:
	if (hItem && (uHitFlags & TVHT_ONITEMLABEL)) 
		cTree.Select (hItem, TVGN_CARET);
		
	// Convert point to screen coordinates:
	ClientToScreen (&point);	

	// Show property sheet for selected item:
	// Note, tree items are created with pointers to associated CKServer
	// or CKGroup objects saved in item data parameter.  We can retrieve
	// these parameters, case appropriately, and give to edit functions.
	if (hItem)
		{
		// If handle to parent item is NULL, then we know we selected a server:
		if (cTree.GetParentItem (hItem) == NULL)
			pDoc->EditServer ((CKServer *) cTree.GetItemData (hItem));

		// Otherwise we must have selected a group:
		else
			pDoc->EditGroup ((CKGroup *) cTree.GetItemData (hItem));
		}
	}


/////////////////////////////////////////////////////////////////////////////
// CKGroupView tree handlers
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
// OnSelectionChanged ()
//
// Description:
//	Called when a item change in selection occurs.  We take this opportunity 
//	to set the newly selected item to a bold font to indicate selection.
//
// Parameters:
//  NMHDR		*pNMHDR			Contains information about a notification message.
//	LRESULT		*pResult		A 32-bit value returned from a window procedure 
//								  or callback function.
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnSelectionChanged (NMHDR *pNMHDR, LRESULT *pResult) 
	{
	*pResult = 0;

	// Cast notification message to proper tree view notification message:
	NM_TREEVIEW *ptv = (NM_TREEVIEW *)pNMHDR;

	// Get reference to tree control:
	CTreeCtrl &cTree = GetTreeCtrl ();

	ASSERT (ptv->itemNew.mask & TVIF_HANDLE);
	ASSERT (ptv->itemOld.mask & TVIF_HANDLE);

	// Unbold the previous selection:
	if (ptv->itemOld.hItem)
		cTree.SetItemState (ptv->itemOld.hItem, 0, TVIS_BOLD);

	// Make new selection bold:
	if (ptv->itemNew.hItem)
		cTree.SetItemState (ptv->itemNew.hItem, TVIS_BOLD, TVIS_BOLD);

	// Get pointer to our document so we can inform it of our
	// new selection:
	CKDocument *pDoc = (CKDocument *) GetDocument ();

	if (pDoc)
		{
		// Get handle of selected item's parent.  We will use it to
		// see if we selected a server of group.
		HTREEITEM hParent = cTree.GetParentItem (ptv->itemNew.hItem);

		// Note, tree items are created with pointers to associated CKServer
		// or CKGroup objects saved in item data parameter.  We can retrieve
		// these parameters, case appropriately, and give to edit functions.

		// If handle of parent is NULL, then we know we selected a server:
		if (hParent == NULL)
			{
			// Give document selected server (no selected group):
			pDoc->SetSelectedServerGroup (
				(CKServer *)cTree.GetItemData (ptv->itemNew.hItem), NULL);
			}

		// Otherwise we must have selected a group:
		else
			{
			// Give document selected group (and parent server):
			pDoc->SetSelectedServerGroup (
				NULL, (CKGroup *)cTree.GetItemData (ptv->itemNew.hItem));
			}
		}
	}

// **************************************************************************
// Insert ()
//
// Description:
//	Insert a new object into the view.
//
// Parameters:
//  HTREEITEM	hParent		Handle to parent item in tree control.
//	CObject		*pObject	Pointer to object to insert.
//	bool		bSelect		Set to true to select item after inserted (default).
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::Insert (HTREEITEM hParent, CObject *pObject, bool bSelect /*= true */)
	{
	ASSERT (pObject != NULL);
	
	TV_INSERTSTRUCT tvis;
	HTREEITEM hItem;

	// Get reference to our tree control:
	CTreeCtrl &cTree = GetTreeCtrl ();
		
	// Fill up the tree view insert structure:
	tvis.hParent = hParent;						// Handle to parent item
	tvis.hInsertAfter = TVI_SORT;				// Sort items in tree
												// item props we will fill	
	tvis.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM | TVIF_TEXT;
	
	tvis.item.lParam = (LPARAM)pObject;			// item data: pointer to object
	tvis.item.pszText = LPSTR_TEXTCALLBACK;		// item text: defer to OnGetDispInfo ()
	tvis.item.iImage = I_IMAGECALLBACK;			// item image: defer to OnGetDispInfo ()
	tvis.item.iSelectedImage = I_IMAGECALLBACK;	// item selimage: defer to OnGetDispInfo ()

	// Insert the item:
	hItem = cTree.InsertItem (&tvis);
	
	// Select the item if asked:
	if (bSelect)
		cTree.SelectItem (hItem);

	// Give item object the handle of the tree control item it is associated with:

	// If parent is tree control root, then we know item is a server:
	if (hParent == TVI_ROOT)
		((CKServer *)pObject)->SetGUIHandle (hItem);

	// Otherwise item must be a group:
	else
		((CKGroup *)pObject)->SetGUIHandle (hItem);
	}

// **************************************************************************
// Delete ()
//
// Description:
//	Delete an object from the view.
//
// Parameters:
//  HTREEITEM	hItem		Handle of item to delete.
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::Delete (HTREEITEM hItem)
	{
	// Delete tree item:
	GetTreeCtrl ().DeleteItem (hItem);
	}

// **************************************************************************
// OnGetDispInfo ()
//
// Description:
//	Handle notification to fill item information for display.
//
// Parameters:
//  NMHDR		*pNMHDR			Contains information about a notification message.
//	LRESULT		*pResult		A 32-bit value returned from a window procedure 
//								  or callback function.
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnGetDispInfo (NMHDR *pNMHDR, LRESULT *pResult) 
	{
	// Cast notification message structure appropriately:
	TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;

	// Extract tree view item:
	TV_ITEM *pItem = &pTVDispInfo->item;

	// If handle to parent item is NULL, we know we are working with a server:
	bool bServerItem = (GetTreeCtrl ().GetParentItem (pItem->hItem) == NULL);
	
	// Pointer to object from tree item parameter.  We can cast this to a 
	// CKServer or CKGroup as the case may be.
	CObject *pObject = (CObject *) pItem->lParam;

	// The item's mask text bit will be set if we are being asked to supply
	// the item's text.
	if (pItem->mask & TVIF_TEXT)
		{
		if (bServerItem)
			lstrcpyn (pItem->pszText, ((CKServer *)pObject)->GetProgID (), pItem->cchTextMax);
		else
			lstrcpyn (pItem->pszText, ((CKGroup *)pObject)->GetName (), pItem->cchTextMax);
		}

	// The item's mask (not selected) image bit will be set if we are being
	// asked to supply the items image (index into image control).
	if (pItem->mask & TVIF_IMAGE)
		{
		// Give server image if server is specified:
		if (bServerItem)
			{
			// Give server connected image if connected:
			if (((CKServer *)pObject)->IsConnected ())
				pItem->iImage = 0;

			// Else give server not connected image:
			else
				pItem->iImage = 6;
			}

		// Else give group image:
		else
			{
			// Cast pObject to CKGroup:
			CKGroup *pGroup = (CKGroup *)pObject;
			
			// If group is valid, give active or inactive image as case may be:
			if (pGroup->IsValid ())
				pItem->iImage = pGroup->IsActive () ? 2 : 3;
			
			// Else give invalid group image:
			else
				pItem->iImage = 7;
			}
		}

	// The item's mask selected image bit will be set if we are being
	// asked to supply the items image (index into image control).
	if (pItem->mask & TVIF_SELECTEDIMAGE)
		{
		// Give server image is server is specified:
		if (bServerItem)
			{
			// Give server connected image if connected:
			if (((CKServer *)pObject)->IsConnected ())
				pItem->iSelectedImage = 0;

			// Else give server not connected image:
			else
				pItem->iSelectedImage = 6;
			}

		// Else give group image:
		else
			{
			// Cast pObject to CKGroup:
			CKGroup *pGroup = (CKGroup *)pObject;
			
			// If group is valid, give active of inactive image as case may be:
			if (pGroup->IsValid ())
				pItem->iSelectedImage = pGroup->IsActive () ? 4 : 5;

			// Else give invalid group image:
			else
				pItem->iSelectedImage = 7;
			}
		}

	*pResult = 0;
	}


/////////////////////////////////////////////////////////////////////////////
// CKGroupView diagnostics
/////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
void CKGroupView::AssertValid () const
	{
	CTreeView::AssertValid ();
	}

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


/////////////////////////////////////////////////////////////////////////////
// CKGroupView message handlers
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
// OnNewServer ()
//
// Description:
//	New server connection menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKGroupView::OnNewServer () 
	{
	// Get pointer to our document object.  (Pointer will be NULL if we

⌨️ 快捷键说明

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