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

📄 itemadddlg.cpp

📁 PC客户和opc通信的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// **************************************************************************
// OnBranchSelected ()
//
// Description:
//	Handles notification that the tree control's selected item has changed.
//
// 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 CKItemAddDlg::OnBranchSelected (NMHDR *pNMHDR, LRESULT *pResult) 
	{
	// Cast notificatin header to tree view notification header:
	NM_TREEVIEW* pnmtv = (NM_TREEVIEW*)pNMHDR;

	// Select the branch:
	SelectBranch (pnmtv->itemNew.hItem);
	*pResult = 0;
	}

// **************************************************************************
// OnBranchFilterChange ()
//
// Description:
//	Handles notification that branch filter edit control has changed.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::OnBranchFilterChange () 
	{
	// Make sure member variables have values currently displayed in
	// controls:
	UpdateData (true);

	// Delete all items and start over if the branch filter has been modified:
	if (m_pBranchList->GetCount ())
		{
		m_pBranchList->DeleteAllItems ();
		BrowseRootLevel ();
		}
	}

// **************************************************************************
// OnLeafFilterChange ()
//
// Description:
//	Handles notification that leaf filter edit control has changed.
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::OnLeafFilterChange () 
	{
	// Make sure member variables have values currently displayed in
	// controls: 
	UpdateData (true);

	// Get currently selected item:
	HTREEITEM hItem = m_pBranchList->GetSelectedItem ();
	
	// Select current item.  This will cause us to re-browse to position.
	// Data will come back from browser using new filter settings:
	if (hItem != NULL)
		SelectBranch (hItem);
	}

// **************************************************************************
// OnAccessFilterChange ()
//
// Description:
//	Handles notification that access filter combo box has changed.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::OnAccessFilterChange () 
	{
	// Get the access rights:
	m_dwFilterAccessRights = 
		((CComboBox *) GetDlgItem (IDC_FILTERACCESS))->GetCurSel ();

	// Get currently selected item:
	HTREEITEM hItem = m_pBranchList->GetSelectedItem ();
	
	// Select current item.  This will cause us to re-browse to position.
	// Data will come back from browser using new filter settings:
	if (hItem != NULL)
		SelectBranch (hItem);
	}

// **************************************************************************
// OnVartypeFilterChange ()
//
// Description:
//	Handles notificatin that datatype filter combo box has changed.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::OnVartypeFilterChange () 
	{
	// Get pointer to data type filter combo box:
	CComboBox *pCombo = (CComboBox *)GetDlgItem (IDC_FILTERTYPE);

	// Get the selected data type string:
	CString strType;
	pCombo->GetLBText (pCombo->GetCurSel (), strType);

	// Convert that string to a variant type:
	m_vtFilterType = VartypeFromString (strType);

	// Get currently selected item:
	HTREEITEM hItem = m_pBranchList->GetSelectedItem ();
	
	// Select current item.  This will cause us to re-browse to position.
	// Data will come back from browser using new filter settings:
	if (hItem != NULL)
		SelectBranch (hItem);
	}

// **************************************************************************
// OnClickLeafList ()
//
// Description:
//	Handles notification that an leaf/item in list control has been double
//	clicked on.
//
// 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 CKItemAddDlg::OnClickLeafList (NMHDR *pNMHDR, LRESULT *pResult) 
	{
	int nSelItem;

	// Get selected item index:
	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)
		{
		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:
#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)
				{
				// Update selector to end of the list:
				m_nSelIndex = m_nListIndex;

				// Re-initialize for new item:
				m_strItemID = lpszQualifiedID;
				m_strAccessPath.Empty ();
				UpdateData (false);

				// Apply new dataset:
				m_bModified = true;
				OnApplyChange ();

				// 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("OTC: Unable to get the qualified item id for %s\r\n"), 
					m_pLeafList->GetItemText (nSelItem, 0));
				}
			}
		
		// Catch exceptions:
		catch (...)
			{
			m_pIBrowse = NULL;
			UpdateStatus ();
			}
		}

	*pResult = 0;
	}

// **************************************************************************
// OnChange ()
//
// Description:
//	Default event handler for control change notifications.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::OnChange () 
	{
	// Make sure member variables have values currently displayed in
	// controls:
	UpdateData (true);

	// We will consider ourselves modified if there is an item ID defined:
	m_bModified = !m_strItemID.IsEmpty ();

	// Update control status:
	UpdateStatus ();
	}

// **************************************************************************
// OnAddLeaves ()
//
// Description:
//	Add Leaves button event handler.  Add leaves to list.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::OnAddLeaves ()
	{
	// If one or more items/leaves are selected, then add them:
	if (m_pLeafList->GetSelectedCount ())
		{
		// 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;
		int nItem = 0;

		// Postpone repaint untill we are done.  This will make things
		// go faster and look smoother.
		SetRedraw (false);

		// Get first selected item:
		nItem = m_pLeafList->GetNextItem (-1, LVNI_ALL | LVNI_SELECTED);

		// Retrieve the qualified item ID and add it to the list:
		while (nItem >= 0)
			{
			HRESULT hr;
			WCHAR szItemID [DEFBUFFSIZE];
			LPWSTR lpszQualifiedID;

			// COM requires all strings to be in UNICODE format.  Convert item ID
			// if needed and copy to allocated memory:
#ifdef _UNICODE
			lstrcpyn (szItemID, m_pLeafList->GetItemText (nItem, 0), sizeof (szItemID) / sizeof (WCHAR));
#else
			_mbstowcsz (szItemID, m_pLeafList->GetItemText (nItem, 0), sizeof (szItemID) / sizeof (WCHAR));
#endif

			// Use browser to get item's fully qualified ID:
			try
				{
				// If we are using a flag browse space, need to browse to 
				// root first:
				if (m_bBrowseFlat)
					BrowseToRoot ();

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

				// If we succeeded, then update controls accordingly:
				if (SUCCEEDED (hr) && lpszQualifiedID)
					{
					// Update selector to end of the list:
					m_nSelIndex = m_nListIndex;

					// Re-initialize for new item:
					m_strItemID = lpszQualifiedID;
					m_strAccessPath.Empty ();
					UpdateData (false);

					// Apply new dataset:
					m_bModified = true;
					OnApplyChange ();

					// Update control state:
					UpdateStatus ();

					// Free server allocation for qualified item ID:
					CoTaskMemFree (lpszQualifiedID);
					}

				// If we failed to get fully qualified ID, issue a trace
				// statement for debugging:
				else
					{
					TRACE (_T("OTC: Unable to get the qualified item id for %s\r\n"), 
						m_pLeafList->GetItemText (nItem, 0));
					}
				}
			
			// Catch exceptions:
			catch (...)
				{
				m_pIBrowse = NULL;
				UpdateStatus ();

				break;
				}

			// Get the next item.  Continue to loop until GetNextItem() returns
			// -1, indicating that we have processed all selected items:
			nItem = m_pLeafList->GetNextItem (nItem, LVNI_ALL | LVNI_SELECTED);
			}

		// Now we are done adding items, we can allow repaints:
		SetRedraw (true);

		// Force a repaint:
		Invalidate ();
		}
	}


/////////////////////////////////////////////////////////////////////////////
// CKItemAddDlg helpers
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
// SelectItem ()
//
// Description:
//	Called to select a item.
//
// Parameters:
//	int			nIndex			List control index of item to select
//	bool		bDuplicate		Set to true if duplication item currently 
//								  selected
//	bool		bDotBitAddress	Set to true if item is being duplicated and
//								  has "dot bit number" address format.
//
// Returns:
//  void
// **************************************************************************
void CKItemAddDlg::SelectItem (int nIndex, bool bDuplicate /* = false */, bool bDotBitAddress /* = false */)
	{
	// Apply current changes:
	if (OnApplyChange ())
		{
		CKItem *pItem = NULL;

		// Select the new item:
		m_nSelIndex = nIndex;

		// If we are creating a new item, update selector:
		if (m_nSelIndex == -1)
			m_nSelIndex = m_nListIndex;

		// else if we are viewing an existing item, get pointer to associated
		// item object stored in item list:
		else if (m_nSelIndex < m_nListIndex)
			pItem = (CKItem *) m_cItemList.GetAt (m_nSelIndex);
		
		// else something is wrong:
		else
			{
			ASSERT (FALSE);
			}

		// If we are viewing an existing item update the current
		// dialog data set:
		if (pItem)
			{
			m_strAccessPath = pItem->GetAccessPath ();
			m_bActive = pItem->IsActive ();
			m_vtDataType = pItem->GetDataType ();

			if (m_bBrowseFlat)
				BrowseToRoot ();

			m_strItemID = pItem->GetItemID ();
			}

		// Else create a new item:
		else
			{
			// If we are asked to duplicate, new item will be duplicate
			// of curretnly selected item:
			if (bDuplicate)
				AutoIncrementID (bDotBitAddress);

			// else it will have default properties:
			else
				{
				m_strAccessPath = ITEM_DEFAULT_ACCESSPATH;
				m_bActive = ITEM_DEFAULT_ACTIVESTATE;
				m_strItemID = ITEM_DEFAULT_ITEMID;
				m_vtDataType = ITEM_DEFAULT_DATATYPE;
				}
			}

		// Update control values from associated member variables:
		UpdateData (false);

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

// **************************************************************************
// OnApplyChange ()
//
// Description:
//	Called to apply changes.
//
// Parameters:

⌨️ 快捷键说明

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