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

📄 browsetreeconverttotreelist.cs

📁 利用OpcNet控件(包含源码)进行OPC通讯的开发样例
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using HaiGrang.Package.OpcNetApiChs.Opc;
using HaiGrang.Package.OpcNetApiChs.Da;
using HaiGrang.Package.OpcNetApiChs.DaNet;

namespace BrowseTreeConvertToTreeList
{
	/// <summary>
	/// BrowseTreeConvertToTreeList 的摘要说明。
	/// </summary>

	public class BrowseTreeConvertToTreeListForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button BrowseTree;
		private System.Windows.Forms.Label BrowseTreeName;
		private System.Windows.Forms.Button OpcServerDisConnect;
		private System.Windows.Forms.Label OpcServerHostInfo;
		private System.Windows.Forms.Label bOpcServerName;
		private System.Windows.Forms.Button bOpcServerConnect;
		private System.Windows.Forms.ListView BrowseTreeView;
		private System.Windows.Forms.TreeView tBranche;
		private System.ComponentModel.IContainer components;

		public BrowseTreeConvertToTreeListForm()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>

		protected override void Dispose( bool disposing )
		{
			if(tOpcServer != null)
			{
				tOpcServer.Disconnect() ;
			}
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.BrowseTree = new System.Windows.Forms.Button();
			this.BrowseTreeName = new System.Windows.Forms.Label();
			this.OpcServerDisConnect = new System.Windows.Forms.Button();
			this.OpcServerHostInfo = new System.Windows.Forms.Label();
			this.bOpcServerName = new System.Windows.Forms.Label();
			this.bOpcServerConnect = new System.Windows.Forms.Button();
			this.BrowseTreeView = new System.Windows.Forms.ListView();
			this.tBranche = new System.Windows.Forms.TreeView();
			this.SuspendLayout();
			// 
			// BrowseTree
			// 
			this.BrowseTree.Location = new System.Drawing.Point(64, 344);
			this.BrowseTree.Name = "BrowseTree";
			this.BrowseTree.Size = new System.Drawing.Size(216, 32);
			this.BrowseTree.TabIndex = 46;
			this.BrowseTree.Text = "Browse Tree";
			this.BrowseTree.Click += new System.EventHandler(this.BrowseTree_Click);
			// 
			// BrowseTreeName
			// 
			this.BrowseTreeName.BackColor = System.Drawing.Color.Yellow;
			this.BrowseTreeName.Location = new System.Drawing.Point(64, 128);
			this.BrowseTreeName.Name = "BrowseTreeName";
			this.BrowseTreeName.Size = new System.Drawing.Size(216, 32);
			this.BrowseTreeName.TabIndex = 45;
			this.BrowseTreeName.Text = "Browse Tree Name";
			this.BrowseTreeName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// OpcServerDisConnect
			// 
			this.OpcServerDisConnect.Location = new System.Drawing.Point(64, 416);
			this.OpcServerDisConnect.Name = "OpcServerDisConnect";
			this.OpcServerDisConnect.Size = new System.Drawing.Size(216, 32);
			this.OpcServerDisConnect.TabIndex = 44;
			this.OpcServerDisConnect.Text = "Opc Server DisConnect";
			this.OpcServerDisConnect.Click += new System.EventHandler(this.OpcServerDisConnect_Click);
			// 
			// OpcServerHostInfo
			// 
			this.OpcServerHostInfo.BackColor = System.Drawing.Color.Yellow;
			this.OpcServerHostInfo.Location = new System.Drawing.Point(64, 200);
			this.OpcServerHostInfo.Name = "OpcServerHostInfo";
			this.OpcServerHostInfo.Size = new System.Drawing.Size(216, 32);
			this.OpcServerHostInfo.TabIndex = 43;
			this.OpcServerHostInfo.Text = "Opc Server HostInfo";
			this.OpcServerHostInfo.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// bOpcServerName
			// 
			this.bOpcServerName.BackColor = System.Drawing.Color.Yellow;
			this.bOpcServerName.Location = new System.Drawing.Point(64, 56);
			this.bOpcServerName.Name = "bOpcServerName";
			this.bOpcServerName.Size = new System.Drawing.Size(216, 32);
			this.bOpcServerName.TabIndex = 42;
			this.bOpcServerName.Text = "Opc Server Name";
			this.bOpcServerName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// bOpcServerConnect
			// 
			this.bOpcServerConnect.Location = new System.Drawing.Point(64, 272);
			this.bOpcServerConnect.Name = "bOpcServerConnect";
			this.bOpcServerConnect.Size = new System.Drawing.Size(216, 32);
			this.bOpcServerConnect.TabIndex = 41;
			this.bOpcServerConnect.Text = "Opc Server Connect";
			this.bOpcServerConnect.Click += new System.EventHandler(this.bOpcServerConnect_Click);
			// 
			// BrowseTreeView
			// 
			this.BrowseTreeView.Location = new System.Drawing.Point(336, 56);
			this.BrowseTreeView.Name = "BrowseTreeView";
			this.BrowseTreeView.Size = new System.Drawing.Size(216, 392);
			this.BrowseTreeView.TabIndex = 48;
			// 
			// tBranche
			// 
			this.tBranche.ImageIndex = -1;
			this.tBranche.Location = new System.Drawing.Point(608, 56);
			this.tBranche.Name = "tBranche";
			this.tBranche.SelectedImageIndex = -1;
			this.tBranche.Size = new System.Drawing.Size(216, 392);
			this.tBranche.TabIndex = 49;
			// 
			// BrowseTreeConvertToTreeListForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(1016, 741);
			this.Controls.Add(this.tBranche);
			this.Controls.Add(this.BrowseTreeView);
			this.Controls.Add(this.BrowseTree);
			this.Controls.Add(this.BrowseTreeName);
			this.Controls.Add(this.OpcServerDisConnect);
			this.Controls.Add(this.OpcServerHostInfo);
			this.Controls.Add(this.bOpcServerName);
			this.Controls.Add(this.bOpcServerConnect);
			this.Name = "BrowseTreeConvertToTreeListForm";
			this.Text = "BrowseTreeConvertToTreeList";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.BrowseTreeConvertToTreeListForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		
		[STAThread]
		static void Main() 
		{
			Application.Run(new BrowseTreeConvertToTreeListForm());
		}

		private void BrowseTreeConvertToTreeListForm_Load(object sender, System.EventArgs e)
		{
		
		}

		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@                      Opc Connect Server                       @@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

		private void bOpcServerConnect_Click(object sender, System.EventArgs e)
		{
			OpcServerConnect() ;
		}

		private string tMachineString = "" ;
		private string tKEPServerExUrlString = "KEPware.KEPServerEx.V4" ;

		OpcServer tOpcServer = new OpcServer() ;

		private void OpcServerConnect()
		{
			tOpcServer.Connect( tMachineString, tKEPServerExUrlString );
			bOpcServerName.Text = tOpcServer.ServerName.ToString() ;
			OpcServerHostInfo.Text = tOpcServer.HostInfo.ToString() ;
		}

		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@                    Opc DisConnect Server                      @@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

		private void OpcServerDisConnect_Click(object sender, System.EventArgs e)
		{
			tOpcServer.Disconnect() ;
			bOpcServerName.Text = "Opc Server Disconnect" ;
			OpcServerHostInfo.Text = "Opc Server Disconnect" ;
		}

		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@                    Opc DisConnect Server                      @@@@@@@@@@@@@@@@@@
		// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

		private void BrowseTree_Click(object sender, System.EventArgs e)
		{
			OpcBrowseTree();
		}

		private System.Windows.Forms.ColumnHeader tColumnHeader; 
 
		private void OpcBrowseTree()
		{
			BrowseTree tOpcServerTree =  tOpcServer.AddBrowseTree(); 
			tBranche.ImageList   = tOpcServerTree.ImageList; 
			tBranche.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(tvBranches_AfterSelect); 
			BrowseTreeView.View  = System.Windows.Forms.View.SmallIcon; 
			tColumnHeader = new System.Windows.Forms.ColumnHeader();
			BrowseTreeView.Columns.AddRange
				( new System.Windows.Forms.ColumnHeader[] { tColumnHeader } ); 
			tColumnHeader.Width    = BrowseTreeView.Size.Width; 
			BrowseTreeView.SmallImageList = tOpcServerTree.ImageList; 
 
			tBranche.Nodes.Clear();  
			int rtc = tOpcServerTree.CreateTree(); 
			TreeNode branches = tOpcServerTree.ConvertToTreeList( tKEPServerExUrlString ); 
			tBranche.Nodes.Add( branches ); 
		}

		private void tvBranches_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) 
		{ 
			tlNodeInfo tNodes = (tlNodeInfo)e.Node.Tag; 
			BrowseTreeView.Items.Clear() ; 
			BrowseTreeView.Items.AddRange( tNodes.itemNames );      
		} 
















	}
}

⌨️ 快捷键说明

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