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

📄 itemwritedlg.cpp

📁 PC客户和opc通信的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//  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 CKItemWriteDlg::OnGetDispInfo (NMHDR *pNMHDR, LRESULT *pResult) 
	{
	// Get list view item description structure from argument:
	LV_DISPINFO *plvdi = (LV_DISPINFO *)pNMHDR;
	LV_ITEM *plvItem = &plvdi->item;

	// Use structure to get pointer to CKItem object associated with the 
	// list view item we are being asked about:
	CKItem *pItem = (CKItem *)plvItem->lParam;
	ASSERT (pItem != NULL);

	// We are being ased to supply text for list view item:
	if (plvItem->mask & LVIF_TEXT)
		{
		// Determine what column data is needed for, and fill request
		// accordingly:
		switch (plvItem->iSubItem)
			{
			case 0:	// Item ID
				lstrcpyn (plvItem->pszText, pItem->GetItemID (), plvItem->cchTextMax);
				break;

			case 1:	// Current Value
				{
				CString strValue;
				pItem->GetValue (strValue);

				lstrcpyn (plvItem->pszText, strValue, plvItem->cchTextMax);
				}
				break;

			case 2:	// Write Value (notification handled by subclass):
				if (plvdi->hdr.code == LVN_ENDLABELEDIT)
					{
					TRACE (_T("WriteValue\r\n"));
					}
				break;
			}
		}

	// We are being asked to supply item image.  Specify index into image
	// list according to item's valid and active state:
	if (plvItem->mask & LVIF_IMAGE)
		{
		if (pItem->IsValid ())
			plvItem->iImage = pItem->IsActive () ? 0 : 1;
		else
			plvItem->iImage = 2;
		}
	
	*pResult = 0;
	}

// **************************************************************************
// OnOK ()
//
// Description:
//	OK button event handler.  Use opportunity to apply changes.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemWriteDlg::OnOK () 
	{
	// Apply changes:
	OnApply ();

	// Perform default processing:
	CDialog::OnOK ();
	}

// **************************************************************************
// OnApply ()
//
// Description:
//	Apply changes.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemWriteDlg::OnApply () 
	{
	// 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;

	// Get pointer to our list control:
	CListCtrl *pListCtrl = (CListCtrl *) GetDlgItem (IDC_LIST);
	ASSERT (pListCtrl != NULL);

	// Build and issue write request:
	try
		{
		// Decalare object arrays to contain list of items to write to and
		// the values to be written, and related variables:
		CKItem *pItem;
		CObArray cItems;
		CStringArray cValues;
		DWORD cdwItems = 0;

		ASSERT (m_cdwItems > 0);

		// Construct an item and value list for this write operation:
		cItems.SetSize (m_cdwItems);
		cValues.SetSize (m_cdwItems);

		// Cycle through the list to see which item values have been set:
		for (DWORD dwItem = 0; dwItem < m_cdwItems; dwItem++)
			{
			// Get pointer to next item in list:
			pItem = (CKItem *) m_pItemList->GetAt (dwItem);
			ASSERT (pItem != NULL);

			// We use the WParam to flag write value applied:
			if (pItem->GetWParam ())
				{
				// Write value entered, so add item and value to list of
				// items to request writes for:
				cItems [cdwItems] = pItem;

				// Add corresponding write value to value array:
				cValues [cdwItems] = pListCtrl->GetItemText (dwItem, 2);

				// Clear write applied flag:
				pItem->SetWParam (0);

				// Increment the number of items to write to:
				++cdwItems;
				}
			}

		// Send the write request (as long as there is at least one write value):
		if (cdwItems > 0)
			{
			switch (m_wType)
				{
				case WRITE_SYNC:
					m_pGroup->WriteSync (cItems, cValues, cdwItems);
					break;

				case WRITE_ASYNC10:
					m_pGroup->WriteAsync10 (cItems, cValues, cdwItems);
					break;

				case WRITE_ASYNC20:
					m_pGroup->WriteAsync20 (cItems, cValues, cdwItems);
					break;
				}
			}
		}
	
	catch (...)
		{
		ASSERT (FALSE);
		TRACE (_T("OTC: CKItemWriteDlg::OnApply () - memory exception thrown\r\n"));
		}

	// Disable the apply button until next write value change:
	((CButton *) GetDlgItem (IDC_APPLY))->EnableWindow (FALSE);
	}

// **************************************************************************
// OnWriteValueChanged ()
//
// Description:
//	Write value changed event handler.
//
// Parameters:
//  WPARAM		wSubItem		Not used
//	LPARAM		lItem			Index of item.
//
// Returns:
//  long - 0.
// **************************************************************************
long CKItemWriteDlg::OnWriteValueChanged (WPARAM wSubItem, LPARAM lItem)
	{
	// Use the CKItem param to show that a write value has been applied.
	// We will use this in OnApply to determine whether to send write 
	// data for that value.

	// Get pointer to our list control:
	CListCtrl *pList = (CListCtrl *) GetDlgItem (IDC_LIST);
	ASSERT (pList != NULL);

	// Get pointer to CKItem object associated with specified list
	// control item (stored as item data value):
	CKItem *pItem = (CKItem *)pList->GetItemData ((DWORD)lItem);
	ASSERT (pItem != NULL);

	// Set item's write value applied flag:
	pItem->SetWParam (1);

	// Since we now have pending writes, enable the apply button:
	((CButton *) GetDlgItem (IDC_APPLY))->EnableWindow (TRUE);

	return (0);
	}

// **************************************************************************
// OnTimer ()
//
// Description:
//	Timer event handler.  Do periodic maintenance of view.
//
// Parameters:
//	UINT		nIDEvent		Timer event type.
//
// Returns:
//  void
// **************************************************************************
void CKItemWriteDlg::OnTimer (UINT nIDEvent) 
	{
	// Perform task according to itmer event type:
	switch (nIDEvent)
		{
		// This is our view update event type:
		case UPDATE_CURRENT_VALUE:
			{
			// Invalidate current values column to force a repaint of the current
			// values.

			// Get pointer to our list control:
			CListCtrl *pList = (CListCtrl *) GetDlgItem (IDC_LIST);
			ASSERT (pList != NULL);
			ASSERT (pList->GetItemCount () > 0);

			int nTopIndex;
			CRect rc;
			CRect rcitem;
				
			// Get an index to the first visible item:
			nTopIndex = pList->GetTopIndex ();

			// Get top-most boundary:
			pList->GetItemRect (nTopIndex, &rcitem, LVIR_BOUNDS);

			// Get bottom-most boundary:
			pList->GetClientRect (&rc);

			rc.left = pList->GetColumnWidth (0);				// left-most
			rc.right = rc.left + pList->GetColumnWidth (1);		// right-most
			rc.top = rcitem.top;
						
			// Invalidate the region:
			pList->InvalidateRect (&rc, FALSE);					// invalidate this rectange
			}
			break;

		default:
			// Perform default processing for all other event types:
			CDialog::OnTimer (nIDEvent);
			break;
		}
	}


⌨️ 快捷键说明

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