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

📄 itemview.cpp

📁 KepWare的OPC Client 示例.面向C
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//
// Description:
//	Determins if a cell tool tip is required.  Tool tip is required if cell
//	is too narrow to display its full text.
//
// Parameters:
//  int			nRow		Row number of cell.
//	int			nCol		Column number of cell.
//	CRect		rc			Rectangle that bounds the cell.
//
// Returns:
//  bool - true if tool tip is requred.
// **************************************************************************
bool CKItemView::RequireCellToolTip (int nRow, int nCol, CRect rc) const
	{
	// Get reference to our list control:
	CListCtrl &cList = GetListCtrl ();

	// Get cell text:
	CString strText;
	strText = cList.GetItemText (nRow, nCol);	

	// Create a device context so we can get the cell text extent:
	CWindowDC dc (&cList);

	// Set the current font:
	dc.SelectObject (cList.GetFont ());

	// Get the size of the text (given device context and font):
	CSize size = dc.GetTextExtent (strText);

	// If the string is longer then the column width, then we will return
	// true (need tool tip).  Otherwise we will return false.  Subtract
	// twenty from cell's width for column buffering.
	return (size.cx >= (rc.right - rc.left - 20));
	}


/////////////////////////////////////////////////////////////////////////////
// CKItemView diagnostics
/////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
void CKItemView::AssertValid () const
	{
	CListView::AssertValid ();
	}

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


/////////////////////////////////////////////////////////////////////////////
// CKItemView message handlers
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
// OnTimer ()
//
// Description:
//	Timer event handler.  Do periodic maintenance of view.
//
// Parameters:
//	UINT		nIDEvent		Timer event type.
//
// Returns:
//  void
// **************************************************************************
void CKItemView::OnTimer (UINT nIDEvent) 
	{
	// Process according to timer event type:
	switch (nIDEvent)
		{
		// Update the item pane.  This will cause the items' value,
		// quality, timestamp, etc. to get refreshed.
		case UPDATE_ITEMPANE_EVENT:
			{
			// Get reference to our list control:
			CListCtrl &cList = GetListCtrl ();

			// If there are items in the list control, update the view:
			if (cList.GetItemCount () > 0)
				{
				int nTopIndex;
				CRect rc;
				CRect rcitem;
				
				// Get index of the first visible item:
				nTopIndex = cList.GetTopIndex ();

				// Get client area rectangle.  We will modify this rectangle to 
				// only include the area of the view we wish to refresh.
				cList.GetClientRect (&rc);

				// Get rectangel that bounds top visible item:
				cList.GetItemRect (nTopIndex, &rcitem, LVIR_BOUNDS);

				// Reset left bound of rc to exclude first column.  There 
				// is no need to refresh this (item ID and image).  We are 
				// interested in refreshing all other properties/columns.
				rc.left = cList.GetColumnWidth (0);

				// No need to refresh anything above top of uppermost item:
				rc.top = rcitem.top;
							
				// Force a repaint of the area:
				cList.InvalidateRect (&rc, FALSE);
				}
			}
			break;

		// Use default processing of all other event types:
		default:
			CListView::OnTimer (nIDEvent);
			break;
		}
	}

// **************************************************************************
// OnUpdateTimer ()
//
// Description:
//	Item Update Interval menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemView::OnUpdateTimer () 
	{
	// Create an update interval dialog.  Pass it the current update
	// interval.
	CKUpdateIntervalDlg dlg (m_wUpdateInterval);
	
	// Show as a modal dialog.  If user hits "OK", we will apply changes:
	if (dlg.DoModal () == IDOK)
		{
		// If the user specified an update interval that is different than
		// the current one, reset the timer and store new value.
		if (m_wUpdateInterval != (WORD) dlg.m_nInterval)
			{
			// Stop the update view timer:
			KillTimer (UPDATE_ITEMPANE_EVENT);

			// Store the new update interval:
			m_wUpdateInterval = (WORD) dlg.m_nInterval;

			// Restart update view timer with new interval:
			SetTimer (UPDATE_ITEMPANE_EVENT, m_wUpdateInterval, NULL);
			}
		}
	}

// **************************************************************************
// OnNewItem ()
//
// Description:
//	New item menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemView::OnNewItem () 
	{
	// Get pointer to our document object.  (This could be NULL if the 
	// document was not properly attached.)
	CKDocument *pDoc = (CKDocument *) GetDocument ();

	// Tell document to add a new item:
	if (pDoc)
		pDoc->AddItem ();
	}

// **************************************************************************
// OnProperties ()
//
// Description:
//	Item Properties menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemView::OnProperties () 
	{
	// 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 a list of selected items:
	CObArray cItemList;
	DWORD cdwItems;
	cdwItems = GetSelectedItems (cItemList);

	// If there are selected items, display the properties dialog:
	if (cdwItems > 0)
		{
		// Get pointer to selected server:
		CKServer *pServer = GetSelectedServer ();
		ASSERT (pServer != NULL);

		// Get pointer to selected group:
		CKGroup *pGroup = GetSelectedGroup ();
		ASSERT (pGroup != NULL);

		// Construct an item properties dialog.  Pass it the array of 
		// selected items, the selected group's item management COM
		// interface pointer, and a pointer to the selected server.
		CKItemPropertiesDlg dlg (cItemList, 
			cdwItems, 
			pGroup->GetIItemMgt (),
			pServer);

		// So as modal dialog.  Any changes to properties will be made
		// in the dialog object.
		dlg.DoModal ();

		// If changes were made, set document's modified flag:
		if (dlg.IsModified ())
			((CKDocument *) GetDocument ())->SetModified (true);
		}
	}

// **************************************************************************
// OnCopy ()
//
// Description:
//	Item copy menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemView::OnCopy () 
	{
	// 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;

	// Create a memory file object to copy selected item settings to:
	CFixedSharedFile sf;

	// Create an object array to contain selected items:
	CObArray cItemList;
	DWORD cdwItems;

	// Fill array with selected items:
	cdwItems = GetSelectedItems (cItemList);

	// If there are selected items, copy them to the shared memory file:
	if (cdwItems > 0)
		{
		try
			{
			// First write the number of items copied:
			sf.Write (&cdwItems, sizeof (cdwItems));
			
			// Copy the items:
			for (DWORD dwIndex = 0; dwIndex < cdwItems; dwIndex++)
				{
				// Get pointer to CKItem object from list:
				CKItem *pItem = (CKItem *) cItemList [dwIndex];
				ASSERT (pItem != NULL);

				// Call item's copy function to write its settings to 
				// the shared memory file:
				pItem->Copy (sf);
				}
			}
		
		catch (...)
			{
			ASSERT (FALSE);
			return;
			}

		// Place the object data in the clipboard:
		sf.CopyToClipboard (CF_ITEM);
		}
	}

// **************************************************************************
// OnCut ()
//
// Description:
//	Item cut menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemView::OnCut () 
	{
	// Perform a copy first:
	OnCopy ();

	// Then delete:
	OnDelete ();
	}

// **************************************************************************
// OnPaste ()
//
// Description:
//	Item Paste menu event handler.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKItemView::OnPaste () 
	{
	// 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;

	// Open the clipboard.  Return if we can't open it for some reason.
	if (OpenClipboard ())
		{
		// Get pointer to our document object.
		CKDocument *pDoc = (CKDocument *) GetDocument ();

		// Create a shared memory file to use to extract clipboard
		// contents from:
		CFixedSharedFile sf;

		// Create an object array to contain pointers to the 
		// items we will be adding shortly:
		CObArray cItemList;
		DWORD cdwItems;
		CKItem *pItem;

		// Get pointer to selected group.  This is where the items
		// will get added:
		CKGroup *pGroup = GetSelectedGroup ();
		ASSERT (pGroup != NULL);

		// Be prepared to handle execptions:
		try
			{
			// Is there item data in the clipboard?
			if (IsClipboardFormatAvailable (CF_ITEM))
				{
				// Set shared memory file handle to clipboard data:
				sf.SetHandle (GetClipboardData (CF_ITEM), FALSE);

				// Retrieve number of items and create and allocate enough
				// array memory to hold all of the item pointers:
				sf.Read (&cdwItems, sizeof (cdwItems));
				ASSERT (cdwItems > 0);

				// Reset the list control's size to accomodate new items:
				cItemList.SetSize (cdwItems + 1);

				// Now retrieve the items from the clipboard:
				for (DWORD dwIndex = 0; dwIndex < cdwItems; dwIndex++)
					{
					// Instantiate a new item object.  Pass it a pointer
					// to its parent group:
					pItem = new CKItem (pGroup);

					// Call the item's paste functin to read its properties
					// from the clipboard via the shared memory file:
					pItem->Paste (sf);

					// Add the new item to the object array:
					cItemList [dwIndex] = pItem;
					}

				// Terminate the list.  Will allow us to process list using
				// "while (element)" loop if we want:
				cItemList [dwIndex] = NULL;

				// Tell the document to add the items:
				pDoc->AddItems (cItemList, cdwItems);
				}

			// If item data is not in clipboard, then there is a problem.
			// throw exception to be caught below:
			else
				throw (-1);
			}
		
		catch (...)
			{
			ASSERT (FALSE);
			EmptyClipboard (); 
			}

		// Free the shared memory handle and close the clipboard:
		sf.Detach ();
		CloseClipboard ();
		}
	}

// *********************************************

⌨️ 快捷键说明

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