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

📄 itemadddlg.cpp

📁 PC客户和opc通信的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:

// **************************************************************************
// AddBranches ()
//
// Description:
//	Add branch to tree control.
//
// Parameters:
//  LPENUMSTRING	pIEnumString	Stores the interface pointer
//	HTREEITEM		hParent			Handle of parent tree item
//	DWORD			dwData			Item data value to give new branches
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::AddBranches (LPENUMSTRING pIEnumString, HTREEITEM hParent, DWORD dwData)
	{
	ASSERT (hParent != NULL);

	ULONG celt = 1;			
	LPOLESTR rgelt;			
	ULONG celtFetched = 0;	
	TCHAR szBuffer [DEFBUFFSIZE];

	// Remove the dummy branch if one exists:
	RemoveDummyBranch (hParent);

	// Start at the beginning of the list:
	pIEnumString->Reset ();	
	pIEnumString->Next (celt, &rgelt, &celtFetched);

	// Add each branch to the browse control:
	while (celtFetched > 0) 
		{
		HTREEITEM hNewItem = NULL;

		// COM requis that all strings be sent in UNICODE format.
		// Convert if necessary and copy to szBuffer:
#ifdef _UNICODE
		lstrcpyn (szBuffer, rgelt, sizeof (szBuffer) / sizeof (TCHAR));
#else
		_wcstombsz (szBuffer, rgelt, sizeof (szBuffer) / sizeof (TCHAR));
#endif

		// Insert the branch:
		hNewItem = m_pBranchList->InsertItem (szBuffer, ILI_BRANCH, ILI_SELBRANCH, hParent);
		m_pBranchList->SetItemData (hNewItem, dwData);	

		// Always fake each branch into having a sub item in the tree:
		AddDummyBranch (hNewItem);

		// Free the branch name:
		CoTaskMemFree (rgelt);	

		// Re-initialize and get the next item:
		celt = 1;
		celtFetched = 0;
		pIEnumString->Next (celt, &rgelt, &celtFetched);
		}
	}

// **************************************************************************
// ExpandBranch ()
//
// Description:
//	Called to expand branch.
//
// Parameters:
//  HTREEITEM	hItem		Handle of tree control branch to expand.
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::ExpandBranch (HTREEITEM hItem)
	{
	ASSERT (hItem != NULL);

	int nPos;
	HRESULT hr;
	LPENUMSTRING pIEnumString;

#ifndef _UNICODE
	WCHAR szFilter [DEFBUFFSIZE];
#endif

	// Get the new browse position from the item that was previously
	// selected:
	nPos = (int) m_pBranchList->GetItemData (hItem);
	nPos++;
	ASSERT (nPos >= 0);

	try
		{
		// Re-intialize the server's position to the root level. 
		do
			{
			// Use the OPC_BROWSE_UP rather than the OPC_BROWSE_TO which
			// is only supported in OOPC version 2.0.  We will have to 
			// browse up to root one level at a time.  Function will
			// fail when we are at root.
			hr = m_pIBrowse->ChangeBrowsePosition (OPC_BROWSE_UP, L"\0");
			} while (SUCCEEDED (hr));

		// Now browse down to the new position:
		CStringArray strBranches;
		HTREEITEM hParentItem;

		strBranches.SetSize (nPos + 1);
		hParentItem = hItem;

		for (int i = 0; i <= nPos; i++)
			{
			ASSERT (hItem);
			strBranches [i] = m_pBranchList->GetItemText (hParentItem);
			hParentItem = m_pBranchList->GetParentItem (hParentItem);
			}

		hr = S_OK;

		// > 0 we do not want to include the "Root" item since the
		// client only uses this branch:
		while (SUCCEEDED (hr) && nPos-- > 0)	
			{
#ifdef _UNICODE
			hr = m_pIBrowse->ChangeBrowsePosition (OPC_BROWSE_DOWN, strBranches [nPos]);
#else
			WCHAR szBranch [DEFBUFFSIZE];
			_mbstowcsz (szBranch, strBranches [nPos], sizeof (szBranch) / sizeof (WCHAR));
			hr = m_pIBrowse->ChangeBrowsePosition (OPC_BROWSE_DOWN, szBranch);
#endif
			}

		// Browse for root level:
#ifdef _UNICODE
		hr = m_pIBrowse->BrowseOPCItemIDs (OPC_BRANCH,				// provide items with children
										  m_strFilterBranch,		// id filtering
										  VT_EMPTY,					// no datatype filtering on a branch
										  0,						// no access filtering on a branch
										  &pIEnumString);			// store the interface pointer here
#else
		_mbstowcsz (szFilter, m_strFilterBranch, sizeof (szFilter) / sizeof (WCHAR));
		hr = m_pIBrowse->BrowseOPCItemIDs (OPC_BRANCH,				// provide items with children
										  szFilter,					// id filtering
										  VT_EMPTY,					// no datatype filtering on a branch
										  0,						// no access filtering on a branch
										  &pIEnumString);			// store the interface pointer here
#endif

		// On success add the branches to the root:
		if (SUCCEEDED (hr) && pIEnumString)
			{
			AddBranches (pIEnumString, hItem, m_pBranchList->GetItemData (hItem) + 1);
			pIEnumString->Release ();
			}
		else
			{
			RemoveDummyBranch (hItem);
			throw (-1);
			}
		}
	
	catch (...)
		{
		m_pIBrowse = NULL;
		UpdateStatus ();
		}
	}

// **************************************************************************
// SelectBranch ()
//	
// Description:
//	Select a branch in tree control.
//
// Parameters:
//  HTREEITEM	hItem		Handle of tree control's item to select
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::SelectBranch (HTREEITEM hItem)
	{
	ASSERT (hItem != NULL);

	int nPos;
	HRESULT hr;
	LPENUMSTRING pIEnumString;
	CStringArray strBranches;
	HTREEITEM hParentItem;

#ifndef _UNICODE
	WCHAR szFilter [DEFBUFFSIZE];
#endif

	// Get the new browse position from the item that was previously
	// selected:
	nPos = (int) m_pBranchList->GetItemData (hItem);
	nPos++;
	ASSERT (nPos >= 0);

	try
		{
		// Re-intialize the server's position to the root level:
		do
			{
			// Use the OPC_BROWSE_UP rather than the OPC_BROWSE_TO which
			// is only supported in OOPC version 2.0.  We will have to 
			// browse up to root one level at a time.  Function will
			// fail when we are at root.
			hr = m_pIBrowse->ChangeBrowsePosition (OPC_BROWSE_UP, L"\0");
			} while (SUCCEEDED (hr));

		// Now browse down to the new position:
		strBranches.SetSize (nPos + 1);
		hParentItem = hItem;

		for (int i = 0; i <= nPos; i++)
			{
			ASSERT (hItem);
			strBranches [i] = m_pBranchList->GetItemText (hParentItem);
			hParentItem = m_pBranchList->GetParentItem (hParentItem);
			}

		hr = S_OK;

		// > 0 we do not want to include the "Root" item since the client
		// only uses this branch:
		while (SUCCEEDED (hr) && nPos-- > 0)	
			{
#ifdef _UNICODE
			hr = m_pIBrowse->ChangeBrowsePosition (OPC_BROWSE_DOWN, strBranches [nPos]);
#else
			WCHAR szBranch [DEFBUFFSIZE];

			_mbstowcsz (szBranch, strBranches [nPos], sizeof (szBranch) / sizeof (WCHAR));
			hr = m_pIBrowse->ChangeBrowsePosition (OPC_BROWSE_DOWN, szBranch);
#endif
			}

		// Browse for root level:
#ifdef _UNICODE
		hr = m_pIBrowse->BrowseOPCItemIDs (
			m_bBrowseFlat ? OPC_FLAT : OPC_LEAF,	// provide items with children
			m_strFilterLeaf,						// item id filtering
			m_vtFilterType,							// datatype filter
			m_dwFilterAccessRights,					// access rights filtering
			&pIEnumString);							// store the interface pointer here
#else
		_mbstowcsz (szFilter, m_strFilterLeaf, sizeof (szFilter) / sizeof (WCHAR));

		hr = m_pIBrowse->BrowseOPCItemIDs (
			m_bBrowseFlat ? OPC_FLAT : OPC_LEAF,	// provide items with children
			szFilter,								// item id filtering
			m_vtFilterType,							// datatype filter
			m_dwFilterAccessRights,					// access rights filtering
			&pIEnumString);							// store the interface pointer here
#endif

		// On success add the branches to the root:
		if (SUCCEEDED (hr) && pIEnumString)
			{
			AddLeaves (pIEnumString);
			pIEnumString->Release ();
			}
		else
			throw (-1);
		}
	
	catch (...)
		{
		m_pIBrowse = NULL;
		UpdateStatus ();
		}
	}

// **************************************************************************
// AddDummyBranch ()
//
// Description:
//	Add a dummy branch to tree control.
//
// Parameters:
//  HTREEITEM	hParent		Handle to parent item.
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::AddDummyBranch (HTREEITEM hParent)
	{
	ASSERT (hParent != NULL);
	
	HTREEITEM hDummyItem;

	// Insert a dummy item:
	hDummyItem = m_pBranchList->InsertItem (NULL_ITEM_NAME, hParent);
	ASSERT (hDummyItem != NULL);
			
	m_pBranchList->SetItemData (hDummyItem, NULL_ITEM_DATA);
	}

// **************************************************************************
// RemoveDummyBranch ()
//
// Description:
//	Remove a dummy branch from tree control.
//
// Parameters:
//  HTREEITEM	hParent		Handle of parent item.
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::RemoveDummyBranch (HTREEITEM hParent)
	{
	ASSERT (hParent != NULL);

	HTREEITEM hDummyItem;

	// Get child item:
	hDummyItem = m_pBranchList->GetChildItem (hParent);
	while (hDummyItem)
		{
		CString strItem = m_pBranchList->GetItemText (hDummyItem);
		
		if (strItem.CompareNoCase (NULL_ITEM_NAME) == 0)
			{
			if (m_pBranchList->GetItemData (hDummyItem) == NULL_ITEM_DATA)
				{
				m_pBranchList->DeleteItem (hDummyItem);
				break;
				}
			}

		hDummyItem = m_pBranchList->GetNextSiblingItem (hDummyItem);
		}
	}

// **************************************************************************
// DeleteChildBranches ()
//
// Description:
//	Deletes all child branches of specified item in tree control
//
// Parameters:
//  HTREEITEM	hParent		Handle to parent item.
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::DeleteChildBranches (HTREEITEM hParent)
	{
	ASSERT (hParent != NULL);

	HTREEITEM hItem;
	hItem = m_pBranchList->GetChildItem (hParent);

	while (hItem)
		{
		m_pBranchList->DeleteItem (hItem);
		hItem = m_pBranchList->GetChildItem (hParent);
		}
	
	AddDummyBranch (hParent);
	}

// **************************************************************************
// AddLeaves ()
//
// Description:
//	Add leaves to tree control.
//
// Parameters:
//  LPENUMSTRING	pIEnumString	Stores the interface pointer
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::AddLeaves (LPENUMSTRING pIEnumString)
	{
	ULONG celt = 1;			
	LPOLESTR rgelt;			
	ULONG celtFetched = 0;	
	int nIndex = 0;

#ifndef _UNICODE
	TCHAR szBuffer [DEFBUFFSIZE];
#endif

	// Delete any leaves that are presently being displayed:
	m_pLeafList->DeleteAllItems ();

	// Start at the beginning of the list:
	pIEnumString->Reset ();	
	pIEnumString->Next (celt, &rgelt, &celtFetched);

	// Add each leaf to the leaf control:
	while (celtFetched > 0) 
		{
		// Insert the leaf:
#ifdef _UNICODE
		m_pLeafList->InsertItem (nIndex++, rgelt, ILI_LEAF);
#else
		_wcstombsz (szBuffer, rgelt, sizeof (szBuffer) / sizeof (TCHAR));
		m_pLeafList->InsertItem (nIndex++, szBuffer, ILI_LEAF);
#endif

		// Free the branch name:
		CoTaskMemFree (rgelt);	

		// Re-initialize and get the next item:
		celt = 1;
		celtFetched = 0;
		pIEnumString->Next (celt, &rgelt, &celtFetched);
		}

	// Select first leaf by default:
	if (m_pLeafList->GetItemCount ())
		m_pLeafList->SetItemState (0, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
	}


/////////////////////////////////////////////////////////////////////////////
// CKDuplicateItemCountDlg dialog
/////////////////////////////////////////////////////////////////////////////

#define MIN_DUPLICATE_ITEMS		1
#define MAX_DUPLICATE_ITEMS		32767

// **************************************************************************
// CKDuplicateItemCountDlg ()
//
// Description:
//	Constructor
//
// Parameters:
//  none
//
// Returns:
//  none
// **************************************************************************
CKDuplicateItemCountDlg::CKDuplicateItemCountDlg () : CDialog (IDD_DUPLICATE_COUNT)
	{
	m_cnDuplicateItems = MIN_DUPLICATE_ITEMS;
	}

// **************************************************************************
// DoDataExchange ()
//
// Description:
//	This method is called by the framework to exchange and validate dialog data.
//
// Parameters:
//  CDa

⌨️ 快捷键说明

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