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

📄 dlgopcitemadd.cpp

📁 OPC Client 源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#else
			_mbstowcsz (pszAccessPath, m_strAccessPath, m_strAccessPath.GetLength () + 1);
#endif
			}

		//填充字符结构:
		pItems->bActive = m_bActive;
		pItems->dwBlobSize = 0;
		pItems->hClient = NULL;
		pItems->pBlob = NULL;
		pItems->szAccessPath = pszAccessPath;
		pItems->szItemID = pszItemID;
		pItems->vtRequestedDataType = m_vtDataType;
		
		// Validate the single item, (no blob requested):
		try
			{
			// Request server to validate item described initem def structure
			// through the group's item management interface:
			//检查数据是否有效
			hr = m_pIItemMgt->ValidateItems (1, pItems, FALSE, &pResults, &pErrors);

			// If server reports that item is valid, we may still have to modify
			// the data type.  We will also validate "dot bit address" format.
			if (hr == S_OK)
				{
				// Modify the data type to what the server will use:
				m_vtDataType = pResults->vtCanonicalDataType;

				// Update the dialog controls to reflect data type change:
				UpdateData (false);

				// Verify dot bit address setting:
				if (pbDotBitAddress)
					{
					// Assume false until proven otherwise:
					*pbDotBitAddress = false;

					// Data type must be VT_BOOL for dot bit address,  It it's not,
					// fall through with flag set to false:
					if (m_vtDataType == VT_BOOL)
						{
						// To determine if truly a dot bit address, strip off
						// bit number and see if its still valid.

						// First free previous results,
						if (pResults)
							{
							CoTaskMemFree (pResults);
							pResults = NULL;
							}

						// and previous errors:
						if (pErrors)
							{
							CoTaskMemFree (pErrors);
							pErrors = NULL;
							}

						// Get position of "dot" character:
						WCHAR *pwch = wcsrchr (pItems->szItemID, L'.');

						if (pwch)
							{
							// Replace "dot" with NULL terminator, effectively stripping
							// off the whole "dot bit number" portion of the address:
							*pwch = L'\0';

							// Reset the requested data type to VT_EMPTY.  We are only
							// interested in seeing if the address is valid for some
							// unspecified data type.
							pItems->vtRequestedDataType = VT_EMPTY;

							// Request server to validate the "stripped" address:
							if (SUCCEEDED (m_pIItemMgt->ValidateItems (1, pItems, FALSE, &pResults, &pErrors)))
								{
								// If the server reports that the "stipped" address is valid, and
								// that it corresponds to some data type other than VT_BOOL, 
								// then the original address is truely a "dot bit" address:
								if (pResults->vtCanonicalDataType != VT_EMPTY && pResults->vtCanonicalDataType != VT_BOOL)
									*pbDotBitAddress = true;
								}
							}
						}
					}
				}

			// else if server reports that item is not valid.  Assign item
			// error for return:
			else
				{
				hr = pErrors [0];
				}
			}
		
		// Catch exceptions:
		catch (...)
			{
			// Probably something wrong with interface pointer, so invalidate it:
			m_pIItemMgt = NULL;

			// Update dialog control status:
//			UpdateStatus ();
			}

		// Free allocations (COM requires us to do this):
		CoTaskMemFree (pItems);

		if (pszItemID)
			CoTaskMemFree (pszItemID);

		if (pszAccessPath)
			CoTaskMemFree (pszAccessPath);

		if (pResults)
			CoTaskMemFree (pResults);

		if (pErrors)
			CoTaskMemFree (pErrors);
		}

	// Return validation return code:
	return (hr);
}

void CDlgOPCItemAdd::OnAddItem() 
{
	m_pBtnAdd->EnableWindow (FALSE);

	//得到选择项:
	POSITION pos = m_pLeafList->GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		TRACE("No items were selected!\n");
		return;
	}
   while (pos)
   {
	  int nSelItem = m_pLeafList->GetNextSelectedItem(pos);

	  // If index looks good, get properties of selected item and
	// update all other controls accordingly:
	if (nSelItem >= 0)
		{
		HRESULT hr;
		WCHAR szItemID [DEFBUFFSIZE];
		LPWSTR lpszQualifiedID;

		// COM requires all strings to be in UNICODE format. Convert
		// item ID if needed, then copy to allocated buffer:
	   TRACE("Item %d =%s was selected!\n", nSelItem,m_pLeafList->GetItemText (nSelItem, 0));
#ifdef _UNICODE
		lstrcpyn (szItemID, m_pLeafList->GetItemText (nSelItem, 0), sizeof (szItemID) / sizeof (WCHAR));
#else
		_mbstowcsz (szItemID, m_pLeafList->GetItemText (nSelItem, 0), sizeof (szItemID) / sizeof (WCHAR));
#endif

		try
			{
			// If we are using a flat browse space, then we need to browse to
			// root first:
//			if (m_bBrowseFlat)
//				BrowseToRoot ();

			// User browser to get item's fully qualified ID:
			hr = m_pIBrowse->GetItemID (szItemID, &lpszQualifiedID);

			// If we succeeded, update controls:
			if (SUCCEEDED (hr) && lpszQualifiedID)
				{
				//得到ID:
				m_strItemID = lpszQualifiedID;
//				m_strAccessPath.Empty ();
				UpdateData (false);

				//增加新项:
				AddItem();

				// Update control state:
//				UpdateStatus ();

				// Free server allocation for qualified item id:
				CoTaskMemFree (lpszQualifiedID);
				}
		
			// If we didn't get qualified ID, issue a trace statement for debugging:
			else
				{
				TRACE (_T("OPC: Unable to get the qualified item id for %s\r\n"), 
					m_pLeafList->GetItemText (nSelItem, 0));
				}
			}
		
		// Catch exceptions:
		catch (...)
			{
			m_pIBrowse = NULL;
//			UpdateStatus ();
			}
		}

	}

}

void CDlgOPCItemAdd::OnDelItem() 
{
	m_pBtnDel->EnableWindow (FALSE);

	int nSelItem;
	//得到选择项:
	nSelItem = m_pAddItemList->GetNextItem (-1, LVNI_ALL | LVNI_SELECTED);
	if (nSelItem < 0)
		return;
	CString strItemID=m_pAddItemList->GetItemText (nSelItem,0);

	for(int nIndex=0;nIndex<=m_cAddItems.GetUpperBound ();nIndex++)
	{
		COPCItem* pItem=(COPCItem*)m_cAddItems.GetAt (nIndex);
		ASSERT(pItem != NULL);

		if(pItem->GetItemID ()==strItemID)
		{
			m_cAddItems.RemoveAt (nIndex);
			m_pAddItemList->DeleteItem (nSelItem);

			delete pItem;	//删除对象
			pItem=NULL;

			return;
		}
	}
}

//是否已经存在
int CDlgOPCItemAdd::IsBeing()
{
	//1.当前数组中是否存在
	int nBeing=0;
	for(int nIndex=0;nIndex<=m_cAddItems.GetUpperBound ();nIndex++)
	{
		COPCItem* pItem=(COPCItem*)m_cAddItems.GetAt (nIndex);
		
		if(m_strItemID==pItem->GetItemID ())
		{
			nBeing=nIndex+1;
			break;
		}
	}
	//2.当前选择组中是否存在
	if(nBeing==0)
		if(m_pServer->GetSelGroup ()->IsBeingItem(m_strItemID))
			nBeing=BEINGITEM_SELGROUP;
			

	return nBeing;
}

void CDlgOPCItemAdd::OnClickLeafList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here

	int nSelItem;

	//得到选择项:
	nSelItem = m_pLeafList->GetNextItem (-1, LVNI_ALL | LVNI_SELECTED);

	// If index looks good, get properties of selected item and
	// update all other controls accordingly:
	if (nSelItem >= 0)
		m_pBtnAdd->EnableWindow (TRUE);

	*pResult = 0;
}

void CDlgOPCItemAdd::OnClickAddItemlist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int nSelItem;

	//得到选择项:
	nSelItem = m_pAddItemList->GetNextItem (-1, LVNI_ALL | LVNI_SELECTED);
	if (nSelItem < 0)
		m_pBtnDel->EnableWindow (FALSE);
	else
		m_pBtnDel->EnableWindow (TRUE);


	*pResult = 0;
}

void CDlgOPCItemAdd::OnDblclkAddItemList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int nSelItem;
	//得到选择项:
	nSelItem = m_pAddItemList->GetNextItem (-1, LVNI_ALL | LVNI_SELECTED);
	if(nSelItem>=0 && nSelItem<=m_cAddItems.GetUpperBound ())
	{
		CString strItemText=m_pAddItemList->GetItemText (nSelItem,0);
		CString strItemID;
		COPCItem* pItem=NULL;
		for(int nItem=0;nItem<=m_cAddItems.GetUpperBound ();nItem++)
		{
 			pItem=(COPCItem*)m_cAddItems.GetAt (nItem);
			strItemID=pItem->GetItemID ();
			if(strItemText==strItemID)
				break;
			else
				pItem=NULL;
		}
		if(pItem)
		{
			CString strTip=_T("ID: ")+pItem->GetItemID ();
 			CDlgModifyName dlg(&pItem->GetAlias (),&strTip);
			
			if(IDOK==dlg.DoModal())
			{
				CString strText=dlg.GetName();
				if(!m_pServer->GetSelGroup ()->IsBeingAlias (strText))
				{
  					pItem->SetAlias (strText);
					m_pAddItemList->SetItemText (nSelItem,1,strText);
				}
				else
				{
					CString strTip;
					strTip.Format ("%s 项已经存在[%s]组中",m_pServer->GetSelGroup ()->GetName());
					AfxMessageBox (strTip, MB_OK| MB_ICONQUESTION);
				}
			}
		}
	}
	*pResult = 0;
}

void CDlgOPCItemAdd::AddItem()
{
	int nBeing=IsBeing();		//查看是否重复使用
	if(nBeing>0)
	{
		CString strTip;
		if(BEINGITEM_SELGROUP==nBeing)
			strTip.Format ("%s 已经存在[%s]组中",m_strItemID,m_pServer->GetSelGroup ()->GetName());
		else
			strTip.Format ("%s 已经被选择",m_strItemID);

		AfxMessageBox (strTip, MB_OK| MB_ICONQUESTION);

		m_pBtnAdd->EnableWindow (FALSE);
		return;
	}

	COPCItem *pItem = NULL;

	// Make sure member variables have values currently displayed in
	// associated controls:
	UpdateData (true);

	// If auto validating, validate item before applying changes.
	//验证当前选择项:
	HRESULT hr = Validate ();

	if (FAILED (hr))
		{
		// Define a string format for the validation return code:
		CString strHR;
		strHR.Format (_T("0x%08X"), hr);

		// Define a string format for the validation return code:
		CString strFailure;
//				strFailure.FormatMessage (IDS_VALIDATE_ITEM_FAILED_CONTINUE, m_strItemID, strHR);
		strFailure.Format ("错误: OPCServer验证[%s]失败",m_strItemID);

		// Show message bow with error string.  Give user of option of
		// continuing.  If user clicks "NO", return now without applying
		// changes:
		AfxMessageBox (strFailure, MB_OK | MB_ICONQUESTION);
		return ;
		}

	// If we make it here, it is OK to apply changes.
	try 
		{
		// 创建一个新项,并加数组入中:
		pItem = new COPCItem ;
		m_cAddItems.SetAtGrow (m_cAddItems.GetUpperBound ()+1, pItem);
		}
	
	catch (...)
		{
		ASSERT (FALSE);
		}
		
	m_strItemName=m_strItemID;
	//设定项的属性:
	pItem->SetAccessPath (m_strAccessPath);
	pItem->SetActive(m_bActive);
	pItem->SetDataType(m_vtDataType);
	pItem->SetItemID(m_strItemID);
	pItem->SetAlias (m_strItemName);

	//显示数据×√
	int nIndex=	m_pAddItemList->InsertItem (0, m_strItemName, 0);
	m_pAddItemList->SetItemText(nIndex,1,m_strItemID);
	m_pAddItemList->SetItemText(nIndex,2,pItem->StringFromVartype (m_vtDataType));
	CString strActive=_T("√");
	if(!m_bActive)
		strActive=_T("×");
	m_pAddItemList->SetItemText(nIndex,3,strActive);
	m_pAddItemList->SetItemText(nIndex,4,m_strAccessPath);

}

CObArray* CDlgOPCItemAdd::GetAddItems()
{
	return &m_cAddItems;
}

//DEL void CDlgOPCItemAdd::OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult) 
//DEL {
//DEL 	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
//DEL 	// TODO: Add your control notification handler code here
//DEL 	pDispInfo->item.mask|=LVIF_TEXT;
//DEL 	int nSelIndex = pDispInfo->item.iItem;
//DEL 	CString strText=pDispInfo->item.pszText;
//DEL 	if (nSelIndex >= 0)
//DEL 	{
//DEL 		strText=m_pAddItemList->GetItemText(nSelIndex,0);
//DEL 		COPCItem* pItem=(COPCItem*)m_cAddItems.GetAt (nSelIndex);
//DEL 		pItem->SetAlias (strText);
//DEL 
//DEL 	}
//DEL 
//DEL 	*pResult = 0;
//DEL }

⌨️ 快捷键说明

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