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

📄 kservergeneralpage.cpp

📁 这个是我现在在做组态软件开发用到的OPC源码
💻 CPP
字号:
// KServerGeneralPage.cpp : implementation file
//

#include "stdafx.h"
#include "test2.h"
#include "KServerGeneralPage.h"
#include "server.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static LPCTSTR lpszDataAccessServers10 = _T("OPC Data Access Servers Version 1.0");
static LPCTSTR lpszDataAccessServers20 = _T("OPC Data Access Servers Version 2.0");
static LPCTSTR lpszOPCServers = _T("OPC General");

static const CATID CATID_OPCDAServer10 = 
{ 0x63d5f430, 0xcfe4, 0x11d1, { 0xb2, 0xc8, 0x0, 0x60, 0x8, 0x3b, 0xa1, 0xfb } };
// {63D5F430-CFE4-11d1-B2C8-0060083BA1FB}

static const CATID CATID_OPCDAServer20 = 
{ 0x63d5f432, 0xcfe4, 0x11d1, { 0xb2, 0xc8, 0x0, 0x60, 0x8, 0x3b, 0xa1, 0xfb } };
// {63D5F432-CFE4-11d1-B2C8-0060083BA1FB}

// image list indices
#define ILI_CATAGORY	0
#define ILI_COMPONENT	1
#define ILI_INTERFACE	2
/////////////////////////////////////////////////////////////////////////////
// CKServerGeneralPage dialog


/*CKServerGeneralPage::CKServerGeneralPage(CWnd* pParent /*=NULL*/
	/*: CDialog(CKServerGeneralPage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKServerGeneralPage)
	m_strRemoteMachine = _T("");
	m_strProgID = _T("");
	//}}AFX_DATA_INIT
}*/
CKServerGeneralPage::CKServerGeneralPage (CKServer *pServer)
	: CDialog (CKServerGeneralPage::IDD)
	{
	// Initialize member variables:
	m_pServer = pServer;
	m_strProgID = cApp.GetDefConnectProgID ();
	m_strRemoteMachine.Empty ();

	m_pServerList = NULL;
	}

void CKServerGeneralPage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKServerGeneralPage)
	DDX_Text(pDX, IDC_MACHINENAME, m_strRemoteMachine);
	DDV_MaxChars(pDX, m_strRemoteMachine, 256);
	DDX_Text(pDX, IDC_PROGID, m_strProgID);
	DDV_MaxChars(pDX, m_strProgID, 256);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CKServerGeneralPage, CDialog)
	//{{AFX_MSG_MAP(CKServerGeneralPage)
	ON_NOTIFY(TVN_SELCHANGED, IDC_SERVERLIST, OnSelChanged)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKServerGeneralPage message handlers



void CKServerGeneralPage::OnSelChanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

	// see if it was a server selection (i.e, has a parent item)

	// Update page is server was selected (non-NULL parent item):
	if (m_pServerList->GetParentItem (pNMTreeView->itemNew.hItem) != NULL)
		{
		// Transfer selection to ProgID edit control:
		m_strProgID = m_pServerList->GetItemText (pNMTreeView->itemNew.hItem);

		// Update controls:
		UpdateData (FALSE);//变量值传给控件

		// Make sure selected server is visible:
		m_pServerList->EnsureVisible (pNMTreeView->itemNew.hItem); 
		}

	*pResult = 0;
}

BOOL CKServerGeneralPage::OnInitDialog() 
{CWaitCursor wc;
	CDialog::OnInitDialog ();

	// Initialize the tree control:
	m_pServerList = (CTreeCtrl *) GetDlgItem (IDC_SERVERLIST);
	ASSERT (m_pServerList != NULL);

	// Initialize the image list for the interfaces. The bitmap must use a 
	// purple background color, RGB (255, 0, 255), so that the CImageList
	// object can construct a mask.  The images are 16x16 pixels.  Set the
	// image list background color to CLR_NONE so masked pixels will be
	// transparent. 
	// Image number		Use
	//	0				not used
	//	1				not used
	//	2				Interface
	m_cImageList.Create (IDB_COMPONENTS, 16, 4, RGB (255, 0, 255));
	m_cImageList.SetBkColor (CLR_NONE);
	m_pServerList->SetImageList (&m_cImageList, TVSIL_NORMAL);

	HTREEITEM hParent;

	// Insert data access 1.0 servers:
	hParent = m_pServerList->InsertItem (lpszDataAccessServers10, ILI_CATAGORY, ILI_CATAGORY);
	DisplayComponentCatList (hParent, CATID_OPCDAServer10);

	// Insert data access 2.0 servers:
	hParent = m_pServerList->InsertItem (lpszDataAccessServers20, ILI_CATAGORY, ILI_CATAGORY);
	DisplayComponentCatList (hParent, CATID_OPCDAServer20);

	// Insert general OPC registered servers:
	hParent = m_pServerList->InsertItem (lpszOPCServers, ILI_CATAGORY, ILI_CATAGORY);
	DisplayGeneralOPCServers (hParent);

	// return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE	
	return (TRUE);
}
void CKServerGeneralPage::DisplayComponentCatList (HTREEITEM hParent, CATID catid)
	{
	HRESULT hr;
	
	// Make sure COM is initialized:
	hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);

	if (SUCCEEDED (hr))
		{
		ICatInformation *pCat = NULL;

		// Get component category manager:
		hr = CoCreateInstance (CLSID_StdComponentCategoriesMgr, 
			NULL,
			CLSCTX_SERVER, 
			IID_ICatInformation,
			(void **)&pCat);

		// If succeeded, enumerate registered components:
		if (SUCCEEDED (hr))
			{
			IEnumCLSID *pEnum = NULL;

			CATID arrcatid [1];
			arrcatid [0] = catid;

			// Enumerate registered components based on clsid:
			hr = pCat->EnumClassesOfCategories (
				sizeof (arrcatid) / sizeof (CATID),	// number of catids in the array that follows
				arrcatid,							// catid array
				0, 
				NULL,
				&pEnum);							// clsid enumerator for registered components under this category

			// If succeeded, process results:
			if (SUCCEEDED (hr))
				{
				GUID guid;
				ULONG fetched;

				// Loop over enumerated components.  Call enemerator's next
				// member function to reference next component and get its
				// guid:
				while ((hr = pEnum->Next (1, &guid, &fetched)) == S_OK)
					{
					// Get the ProgID from the guid:
					WCHAR *wszProgID;
					hr = ProgIDFromCLSID (guid, &wszProgID);

					// If succeeded, add component to list:
					if (SUCCEEDED (hr))
						{
						// ProgID string will be in UNICODE format.  Convert to 
						// ANSI format if this is and ANSI build.  Insert component
						// into list:
#ifdef _UNICODE
						m_pServerList->InsertItem (wszProgID, ILI_COMPONENT, ILI_COMPONENT, hParent);
#else
						TCHAR szProgID [DEFBUFFSIZE];

						_wcstombsz (szProgID, wszProgID, sizeof (szProgID) / sizeof (TCHAR));
						m_pServerList->InsertItem (szProgID, ILI_COMPONENT, ILI_COMPONENT, hParent);
#endif
						// It is up to us to free the Prog ID string memory:
						CoTaskMemFree (wszProgID);
						}
					}

				// Release our enumerator:
				pEnum->Release ();
				}

			// release our category mamager
			pCat->Release ();
			}

		// Uninitialize COM:
		CoUninitialize ();
		}
	}

// **************************************************************************
// DisplayGeneralOPCServers ()
//
// Description:
//	Construct a list of installed general OPC servers and insert into tree
//	control.
//
// Parameters:
//  HTREEITEM	hParent		Handle of parent tree control item.
//
// Returns:
//  void
// **************************************************************************
void CKServerGeneralPage::DisplayGeneralOPCServers (HTREEITEM hParent)
	{
	HKEY hKey = HKEY_CLASSES_ROOT;		// search under this key
	TCHAR szKey [DEFBUFFSIZE];			// allocate key buffer
	DWORD dwLength = DEFBUFFSIZE;

	// Search the registry for installed OPC Servers:
	for (DWORD dwIndex = 0; 
		 RegEnumKey (hKey, dwIndex, szKey, dwLength) == ERROR_SUCCESS; 
		 ++dwIndex)
		{
		HKEY hSubKey;

		// Open the registry key:
		if (RegOpenKey (hKey, szKey, &hSubKey) == ERROR_SUCCESS)
			{
			// Search for OPC subkey:
			if (RegQueryValue (hSubKey, _T("OPC"), NULL, NULL) == ERROR_SUCCESS)
				{
				// Display the prog ID for this server:
				m_pServerList->InsertItem (szKey, ILI_COMPONENT, ILI_COMPONENT, hParent);
				}

			// Close the registry key:
			RegCloseKey (hSubKey);
			}

		// Re-initialize length:
		dwLength = DEFBUFFSIZE;
		}
	}

int CKServerGeneralPage::DoModal() 
{
	//m_pServer为NULL代表增加服务器
	int nRC =CDialog::DoModal();
	if (m_pServer)
	nRC = IDCANCEL;

	// If the user selects OK, and the server is a new connection, add server
	// and attempt a connection:
	if (nRC == IDOK)
		{
		// Create a new server is server pointer is currently NULL:
		if (m_pServer == NULL)
			{
			try
				{
				m_pServer = new CKServer;
				}
			
			catch (...)
				{
				// Memory allocation error.  Reset return code to IDCANCEL
				// so no changes will be applied:
				ASSERT (FALSE);

				m_pServer = NULL;
				nRC = IDCANCEL;
				}
			}

		// Attempt to connect to a server:
		if (m_pServer)
			{
			m_pServer->Connect (
				m_strProgID,
				m_strRemoteMachine);
			}
		}
	return nRC;
}

⌨️ 快捷键说明

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