?? frmmain.frm
字號:
Caption = "-"
End
Begin VB.Menu mnuListViewDelete
Caption = "&Delete"
End
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' The main form frmMain is the central user interface focal point
' for this example application. While all of the actual OPC
' interface code is contained in the three class modules
' OPCServerClass, OPCGroupClass, and OPCItemClass, the use
' of these classes is demostrated primarily in this module.
' Think of this module as being the basis for your own OPC
' application. While your application may not have the same
' user interface requirements, the mechanisms for adding server
' connections, groups, and items is the same. One thing to
' keep in mind is that while this example applicaiton
' demonstrates the various OPC elements driven by User input
' your application can do all of these steps programtically,
' or via configuration files, or database contents.
'
' The primary bodies of code in this form deal with managing
' user interaction with the tvTreeView and lvListView controls
' on this form. That management includes maintaining the
' OPC server, group, and item objects stored in these views.
' As you add server connections, groups and items, both the
' class modules and the TreeView and ListView controls are kept in
' sync with these item. Simply put the TreeView and ListView
' contain a collection of objects but the OPC class modules
' keep their own list so you don't need to have any user interface
' elements for your own OPC application.
'
' As part of managing the user interface, the frmMain module
' also handles determining what OPC server, group or item has been
' selected by the user. Once determined the three variables in
' Module1.bas, SelectedOPCServer, SelectedOPCGroup,
' and SelectedOPCItem will be set accordingly. These variables
' are important in other parts of this example application. While
' not used in the three class modules, these variables are used in
' the other VB forms of this application to determine what object
' the user has selected for modification or use.
'
' One important element of how the user interface interface for
' this example is the use of the key string used on the Node
' items for the tvTreeView and on the ListItems for the lvListView.
' These key have been given very specific names with a number. In the
' TreeView there are two types of keys by name "Server X" and
' "Group X Y". For the server key the Key always contains the word
' "Server" plus some number. For group items the Key always
' contains the word "Group" plus a group number and the server
' number. For this example it is crucial that these not be changed
' as all object selection is based on using these key to identify
' selected object. I give more detail below but it worth mentioning
' the now due to its importance.
'
' Only this form contains a menu bar. If you have already run the
' application you have seen that there is very little in the menus.
' If you look at the menu bar in the menu editor you will see that
' there a great deal of hidden(popup) menus. These popup menus are
' invoked by right clicking in either the tvTreeView or the lvListView.
' These right click context menus are used as the primary method of user
' interaction within the application. Normally you would mimic these
' menus on the menu bar and I would have as well but you then must
' also take care of enabling and disabling them as needed. I felt that
' the extra code would have clouded the application so I provide
' only the context menus.
'
' At the end of this module is some code generated by the VB app
' wizard, I have placed this code at the end of this module.
' It deals with the resizing of the this applications client
' space but has nothing to do with OPC connectivity.
Option Explicit
Option Base 1
' These variables are used in the standard code generated by
' VB to handle moving the splitter bar on the this form.
Dim mbMoving As Boolean
Const sglSplitLimit = 500
Dim LastTopItem As Integer
' On form load we need to get the last size of the application we
' also need to add the heading to the lvListView control.
'
Private Sub Form_Load()
Me.Left = GetSetting(App.Title, "Settings", "MainLeft", 1000)
Me.Top = GetSetting(App.Title, "Settings", "MainTop", 1000)
Me.Width = GetSetting(App.Title, "Settings", "MainWidth", 7500)
Me.Height = GetSetting(App.Title, "Settings", "MainHeight", 6500)
' Add the three OPC heading to the lvListView control
' The ItemID is the fully qualified Item identification string
' for an OPC item. The Value heading is the current value
' of the item. The Status heading is Quality value of the
' OPC item.
lvListView.ColumnHeaders.Add , , "ItemID", lvListView.Width / 3
lvListView.ColumnHeaders.Add , , "Value", lvListView.Width / 3
lvListView.ColumnHeaders.Add , , "Status", lvListView.Width / 3
lvListView.View = lvwReport ' Place list view text report mode
' This variable is used to determine when the user is attempting
' to scroll the OPC Item window. When the window is scrolled the
' currently selected OPC Item must be deselected to allow the
' scroll beyond the selected item. If the selection isn't cleared
' the ListView control won't allow the list to be scrolled. Normally
' you don't notice this because the list view isn't normally being
' updated continuously.
LastTopItem = -1
End Sub
' The unload forms sub handles cleaning up for the application.
' From an OPC standpoint it releases each OPCServerClass object
' contained in the OPCServers collection. Due to the design of
' the OPCServerClass object it in turn releases any OPC groups
' and items those groups may contain.
'
' This is a good catch all for your application but normally
' you should not rely on this call to cleanup your OPC connections.
' The best practice is to specifically release the OPC items from
' your groups then release the groups from your servers and finally
' disconnect the OPC servers from your application. This will be
' demonstated in the various delete functions for each of the
' objects.
'
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
' When exiting your application you should ensure that all
' groups and items are removed from the OPCServer objects
' then remove the OPCServer objects. The OPCServerClass
' contains a cleanup section in its terminate routine that
' will remove all groups and items from the class when the
' object is deleted. As we remove OPCServerClass object from
' the OPCServers collection this terminate function is called
' automatically removing We still need to remove the
' OPCServerGroups collection that contains the
' OPCGroupClasss objects ourselves.
If OPCServers.Count <> 0 Then
Dim a As Integer
a = OPCServers.Count
For i = 1 To OPCServers.Count
With OPCServers
' Make sure we disconnect from server before we remove it
Set Module1.SelectedOPCServer = .Item(a)
Module1.SelectedOPCServer.DisconnectOPCServer
.Remove (a)
a = a - 1
End With
Next i
End If
' Close all sub forms
For i = Forms.Count - 1 To 1 Step -1
Unload Forms(i)
Next
' Save the current size of the application for the next load but
' only if the application is not in a minimized state.
If Me.WindowState <> vbMinimized Then
SaveSetting App.Title, "Settings", "MainLeft", Me.Left
SaveSetting App.Title, "Settings", "MainTop", Me.Top
SaveSetting App.Title, "Settings", "MainWidth", Me.Width
SaveSetting App.Title, "Settings", "MainHeight", Me.Height
End If
End Sub
Private Sub tvTreeView_Collapse(ByVal Node As ComctlLib.Node)
' When a server is selected we set the Module1.SelectedOPCGroup
' to nothing and clear the lvListView control.
Set Module1.SelectedOPCGroup = Nothing
' Clear the current view of tags if any.
lvListView.ListItems.Clear
End Sub
' This sub is the primary focal point for user interaction with the
' OPC server and goup objects. Depending on where you click within the
' tree view you will be presented with differing context menus that will
' allow you to add new servers, add new groups, view the properties of
' these objects or delete them. Clicking within this view also allows
' you to select different objects and change the data you see in the
' lvListView control.
'
Private Sub tvTreeView_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
' The SelectedNode var is used to hold the Node item returned from
' the HitTest
Dim SelectedNode As Node
' When the selected node is group node this var will be used to
' hold the parent node of the selected group, ie. what server the group
' belongs to.
Dim NodParent As Node
' Once a group and server are determined the NewGroupSelection is used
' to hold the selected group. This is then testing against the
' Module1.SelectedOPCGroup variable. If they differ then a new
' lvListView list will potentially be build by the GetNewItemList
' function. This allows you to select different groups and see
' the items for these groups in the lvListView control.
Dim NewGroupSelection As OPCGroupClass
' Used to temporary provide access to the group collection of
' the OPCServerClass object pointed to by Module1.SelectedOPCServer.
Dim OPCServerGroupsCls As Collection
' Determine if the user has selected a node form the tree view.
Set SelectedNode = tvTreeView.HitTest(X, Y)
' Right button actions select server and group object and
' display the various context menus.
If (Button = vbRightButton) Then
' In all cases the "New Server Connection" menu selection
' is available.
mnuTreeViewNewServer.Visible = True
mnuTreeViewNewServer.Enabled = True
' If the user has selected a node as indicated by
' SelectedNode not being nothing then determine what type
' of node was selected. This is where the Key name I mention
' in the start of this module really comes into play. By
' knowing that all server nodes have the word "Server" as part
' of their Key and all group nodes have the word "Group" as part
' of their Key we can easily determine what type of node the user
' has selected.
If Not SelectedNode Is Nothing Then
' Check for a server node
If InStr(SelectedNode.Key, "Server") Then
' When we get to the AddSelectedOPCServer function
' you will see that I also use this same KEY within
' in the OPCServers collection to key the server.
' This make selection of the server from the collection
' as easy as what you see in the next three lines.
' In your application you can of course use any keying
' methodology you desire as long as their unique and
' you manage them.
With OPCServers
Set Module1.SelectedOPCServer = .Item(SelectedNode.Key)
End With
' When a server is selected we set the Module1.SelectedOPCGroup
' to nothing and clear the lvListView control.
Set Module1.SelectedOPCGroup = Nothing
lvListView.ListItems.Clear
' Now enable all of the options that the user can use when
' a server connection has been selected.
mnuTreeViewNewGroup.Visible = True ' Add group allowed
mnuTreeViewNewGroup.Enabled = True
mnuTreeViewDelete.Visible = True ' Deleted the Server
mnuTreeViewDelete.Enabled = True
mnuTreeViewProperties.Visible = True 'Display Server Properties
mnuTreeViewProperties.Enabled = True
mnuTreeViewNewItem.Visible = False ' New Items can't be added at this time
mnuTreeViewNewItem.Enabled = False
' If a server hasn't neen selected then it must be a group
' node. We test the node key just to be sure.
ElseIf InStr(SelectedNode.Key, "Group") Then
' Get a reference to the parent of group node.
Set NodParent = SelectedNode.Parent
' Set the Selected Server to this parent easy stuff as
' long as the keys are managed and not lost.
With OPCServers
Set Module1.SelectedOPCServer = .Item(NodParent.Key)
End With
' Now that we know the server connection we need to access
' the OPCGroupClass collection contained in that
' OPCServerClass object.
Set OPCServerGroupsCls = Module1.SelectedOPCServer.GetOPCServerGroupCollection
' The NewGroupSelection is simply used to test the newly
' selected group against a group that may already be selected.
' You'll notice right away that this use of the SelectedNode.Key
' is a little different from the simple use when selecting
' an OPC server. You'll see why in the AddOPCGroupMain function
' below but here is the short answer. When a node is added to the
' tree view control the Node.Key must be unique regardless
' of parent/child relationships or not. As you'll see in the
' AddOPCGroupMain function the OPCServerClass.AddOPCGroup
' function returns a group key. That key is specific
' to a single group and the server.
Set NewGroupSelection = OPCServerGroupsCls.Item(SelectedNode.Key)
' Now that we have the OPCGroupClass object the user
' has selected we test to see if it is the currently
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -