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

📄 grouppropertysheet.cpp

📁 KepWare的OPC Client 示例.面向C
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					m_pGroup->IsIAsyncIOSupported () ? strYes : strNo);				
				break;

			// IDataObject
			case 5:
				// Insert interface item in list control:
				pList->InsertItem (nIndex, _T ("IDataObject"), ILI_INTERFACE);
				
				// Set required field:
				pList->SetItemText (nIndex, 1, strYes);					

				// Set supported field:
				pList->SetItemText (nIndex, 2, 
					m_pGroup->IsIDataObjectSupported () ? strYes : strNo);				
				break;

			// IOPCAsyncIO2
			case 6:
				// Insert interface item in list control:
				pList->InsertItem (nIndex, _T ("IOPCAsyncIO2 (2.0 only)"), ILI_INTERFACE);
				
				// Set required field:
				pList->SetItemText (nIndex, 1, strYes);						

				// Set supported field:
				pList->SetItemText (nIndex, 2, 
					m_pGroup->IsIAsyncIO2Supported () ? strYes : strNo);				
				break;

			// IConnectionPointContainer
			case 7:
				// Insert interface item in list control:
				pList->InsertItem (nIndex, _T ("IConnectionPointContainer (2.0 only)"), ILI_INTERFACE);	
				
				// Set required field:
				pList->SetItemText (nIndex, 1, strYes);										

				// Set supported field:
				pList->SetItemText (nIndex, 2, 
					m_pGroup->IsIConnectionPointContainerSupported () ? strYes : strNo);				
				break;

			default:
				// Unexpected interface index.  Programmer error.
				ASSERT (FALSE);
				break;
			}

		// Increment interface index for next time around:
		nIndex++;
		}

	// return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
	return (TRUE);  
	}


/////////////////////////////////////////////////////////////////////////////
// CKGroupPropertySheet
/////////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC (CKGroupPropertySheet, CPropertySheet)

// **************************************************************************
BEGIN_MESSAGE_MAP (CKGroupPropertySheet, CPropertySheet)
	//{{AFX_MSG_MAP(CKGroupPropertySheet)
	ON_BN_CLICKED (ID_APPLY_NOW, OnApplyNow)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP ()


// **************************************************************************
// CKGroupPropertySheet ()
//
// Description:
//	Constructor for creating a new group object.
//
// Parameters:
//  CKServer	*pServer		Pointer to server object.
//
// Returns:
//  none
// **************************************************************************
CKGroupPropertySheet::CKGroupPropertySheet (CKServer *pServer)
	: CPropertySheet (IDS_GROUP_PROPERTIES)
	{
	// Server should have been specified:
	ASSERT (pServer != NULL);

	// Initialize member variables:
	m_pServer = pServer;
	m_pGroup = NULL;

	// Add general page:
	AddPage (&m_cGeneralPage);

	// Remove apply button for group creation (This contructor version will
	// only be used when a new group is being created).
	m_psh.dwFlags |= PSH_NOAPPLYNOW;
	}

// **************************************************************************
// CKGroupPropertySheet ()
//
// Description:
//	Constructor for editing an existing group object.
//
// Parameters:
//  CKGroup		*pGroup		Pointer to group object.
//
// Returns:
//  none
// **************************************************************************
CKGroupPropertySheet::CKGroupPropertySheet (CKGroup *pGroup)
	: CPropertySheet (IDS_GROUP_PROPERTIES)
	{
	// Group should have been specified:
	ASSERT (pGroup != NULL);

	// Initialize member variables:
	m_pGroup = pGroup;
	m_pServer = NULL;
	
	HRESULT hr = E_FAIL;
	OPCHANDLE hClient;
	OPCHANDLE hServer;
	LPWSTR pszName;

	IOPCGroupStateMgt* pIGSM = m_pGroup->GetIGroupStateMgt ();

	// Get the OPC Group state as known by the server:
	if (pIGSM != NULL)
		{
		hr = pIGSM->GetState (
			&m_cGeneralPage.m_dwUpdateRate,
			&m_cGeneralPage.m_bActive,
			&pszName,
			&m_cGeneralPage.m_lBias,
			&m_cGeneralPage.m_fDeadband,
			&m_cGeneralPage.m_dwLanguageID,
			&hClient,
			&hServer);

		if (SUCCEEDED (hr))
			{
			ASSERT (hClient == (OPCHANDLE) pGroup);

			// Save and free group name:
			if (pszName)
				{
				m_cGeneralPage.m_strName = pszName;
				CoTaskMemFree (pszName);
				}
			}
		}

	// Else use the state currently set for the CKGroup object:
	else
		{
		m_cGeneralPage.m_strName = m_pGroup->GetName ();
		m_cGeneralPage.m_dwUpdateRate = m_pGroup->GetUpdateRate ();
		m_cGeneralPage.m_bActive = m_pGroup->IsActive ();
		m_cGeneralPage.m_lBias = m_pGroup->GetBias ();
		m_cGeneralPage.m_fDeadband = m_pGroup->GetDeadband ();
		m_cGeneralPage.m_dwLanguageID = m_pGroup->GetLanguageID ();
		}

	m_cGeneralPage.m_nUpdateMethod	= m_pGroup->GetUpdateMethod ();

	// Add general page:
	AddPage (&m_cGeneralPage);

	// Pass a pointer to the supported interface page:
	m_cInterfacePage.m_pGroup = m_pGroup;

	// Add the interface page:
	AddPage (&m_cInterfacePage);
	}

// **************************************************************************
/// ~CKGroupPropertySheet ()
//
// Description:
//	Destructor
//
// Parameters:
//  none
//
// Returns:
//  none
// **************************************************************************
CKGroupPropertySheet::~CKGroupPropertySheet ()
	{
	}


/////////////////////////////////////////////////////////////////////////////
// CKGroupPropertySheet message handlers
/////////////////////////////////////////////////////////////////////////////

// **************************************************************************
// DoModal ()
//
// Description:
//	Augment DoModal behaviour of dialog.  Create new group object if
//	necessary.  Apply changes to object properties.
//
// Parameters:
//  none
//
// Returns:
//  int - IDOK, IDCANCEL.
// **************************************************************************
int CKGroupPropertySheet::DoModal () 
	{
	// Perform default processing:
	int nRC = CPropertySheet::DoModal ();

	// If the user selects "OK" add/modify group:
	if (nRC == IDOK)
		{
		// If m_pGroup is NULL, we need to create a new group:
		if (m_pGroup == NULL)
			{
			ASSERT (m_pServer != NULL);

			try
				{
				// Instantiate a new CKGroup:
				m_pGroup = new CKGroup (m_pServer);
				}
			
			catch (...)
				{
				// If problem, delete the group and return with IDCANCEL code:
				ASSERT (FALSE);

				m_pGroup = NULL;
				return (IDCANCEL);
				}
			}
		
		// Otherwise apply changes to existing group:
		else
			OnApplyNow ();

		// Set attributes from general page:

		// Create name if currently undefined:
		if (m_cGeneralPage.m_strName.IsEmpty ())
			{
			if (!m_pServer || !m_pServer->IsAlive ())
				{
				m_cGeneralPage.m_strName = DEFAULTGROUPNAME;
				m_pServer->GenerateGroupName (m_cGeneralPage.m_strName);
				}
			}

		m_pGroup->SetName (m_cGeneralPage.m_strName);
		m_pGroup->SetActive (m_cGeneralPage.m_bActive);
		m_pGroup->SetLanguageID (m_cGeneralPage.m_dwLanguageID);
		m_pGroup->SetDeadband (m_cGeneralPage.m_fDeadband);
		m_pGroup->SetBias (m_cGeneralPage.m_lBias);
		m_pGroup->SetUpdateRate (m_cGeneralPage.m_dwUpdateRate);
		m_pGroup->SetUpdateMethod (m_cGeneralPage.m_nUpdateMethod);

		// If this is a new group (indicated by non-NULL m_pServer),
		// add it to the server:
		if (m_pServer)
			m_pServer->AddGroup (m_pGroup);
		}

	// Pass along return code from default processing:
	return (nRC);
	}

// **************************************************************************
// OnApplyNow ()
//
// Description:
//	Apply changes to group object properties.
//
// Parameters:
//  none
//
// Returns:
//  void
// **************************************************************************
void CKGroupPropertySheet::OnApplyNow ()
	{
	// See if there are any changes to apply:
	if (m_cGeneralPage.IsChanged ())
		{
		// Force an update data:
		if (m_cGeneralPage.m_hWnd)
			m_cGeneralPage.UpdateData (TRUE);
		
		// Get the group state management interface:
		ASSERT (m_pGroup != NULL);
		IOPCGroupStateMgt* pIGSM = m_pGroup->GetIGroupStateMgt ();

		// Set OPC Group state:
		if (pIGSM)
			{
			HRESULT hr = E_FAIL;
			DWORD dwRevUpdateRate = 0;					// for [out] args

			OPCHANDLE hClient = (OPCHANDLE) m_pGroup;	// keep same client handle
			OPCHANDLE hServer;
			LPWSTR pszName;

			// Set the state based on current settings:
			hr = pIGSM->SetState ( 
				&m_cGeneralPage.m_dwUpdateRate,		// update rate
				&dwRevUpdateRate,					// revised update rate
				&m_cGeneralPage.m_bActive,			// active state
				&m_cGeneralPage.m_lBias,			// time bias
				&m_cGeneralPage.m_fDeadband,		// deadband
				&m_cGeneralPage.m_dwLanguageID,		// language id
				&hClient);

			ASSERT (SUCCEEDED (hr));

			// Allocate memorty to contain the name of the group:
			pszName = (LPWSTR) CoTaskMemAlloc ((m_cGeneralPage.m_strName.GetLength () + 1) *
				sizeof (WCHAR));

			// Set the group name:
			if (pszName)
				{
				// COM requires that all string be in UNICODE format, so 
				// conversion may be necessary.
#ifdef _UNICODE
				// This is a UNICODE build, so no need to convert.  Just copy
				// our name to output buffer:
				lstrcpyn (pszName, m_cGeneralPage.m_strName, m_cGeneralPage.m_strName.GetLength () + 1);
#else
				// This is an ANSI build, so convert to UNICODE and place
				// result in output buffer:
				_mbstowcsz (pszName, m_cGeneralPage.m_strName, m_cGeneralPage.m_strName.GetLength () + 1);
#endif
				// Set the name:
				pIGSM->SetName (pszName);

				// Free the memory allocated for the name:
				CoTaskMemFree (pszName);
				}

			// Now get the state to verify that the server changed our settings:
			hr = pIGSM->GetState (
				&m_cGeneralPage.m_dwUpdateRate,
				&m_cGeneralPage.m_bActive,
				&pszName,
				&m_cGeneralPage.m_lBias,
				&m_cGeneralPage.m_fDeadband,
				&m_cGeneralPage.m_dwLanguageID,
				&hClient,
				&hServer);

			ASSERT (SUCCEEDED (hr));
			ASSERT (hClient == (OPCHANDLE) m_pGroup);
			ASSERT (hServer == m_pGroup->GetServerHandle ());

			// Get and free group name:
			if (pszName)
				{
				m_cGeneralPage.m_strName = pszName;
				CoTaskMemFree (pszName);
				}

			// Gransfer get state data to dialog if it is not closing from an OK:
			if (m_cGeneralPage.m_hWnd)
				m_cGeneralPage.UpdateData (FALSE);
			}

		// Set changed flag:
		m_cGeneralPage.SetChanged (false);
		}
	}

// **************************************************************************
// OnInitDialog ()
//
// Description:
//	Called by framework immediately before the dialog box is displayed,
//
// Parameters:
//  none
//
// Returns:
//  BOOL - Result of base class processing.
// **************************************************************************
BOOL CKGroupPropertySheet::OnInitDialog () 
	{
	// Perform default processing:
	BOOL bResult = CPropertySheet::OnInitDialog ();
	
	// Append item count to title:
	if (m_pGroup)
		{
		// Get current title text:
		CString strTitle;
		GetWindowText (strTitle);

		// Format a string to contain item count:
		CString strItemCount;
		strItemCount.Format (IDS_APPENDITEMCOUNT, m_pGroup->GetItemCount ());

		// Append count to tile:
		strTitle += strItemCount;

		// Set window title to modified:
		SetWindowText (strTitle);
		}

	// Return result from default processing:
	return (bResult);
	}

⌨️ 快捷键说明

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