38.5.3.2 VB .NET 2003程序示例 - 检索WinGP状态以及更改设置示例(专用API)

示例程序位置:(GP-Pro EX光盘)\WinGP\SDK\Pro-SDK\DotNet\RtCtrlSmpl

'导入System.Runtime.InteropServices.
Imports System.Runtime.InteropServices
 
Public Class Form1
 Inherits System.Windows.Forms.Form
 
 Dim ghWinGP As Int32 = 0 ' API句柄。
 
#Region ”由Windows窗体设计器生成的代码“
 
 Public Sub New()
 MyBase.New()
 
 '此调用对于Windows窗体设计器是必需的。
 InitializeComponent()
 
  '初始化API。
'在调用InitializeComponent()后,运行初始化。
 Dim nResult As Integer = InitRuntimeAPI()
 
 ' 在此处获取句柄(API)。
   ghWinGP = GetRuntimeHandle(9800)
 If ghWinGP = 0 Then
 MsgBox("(API) Failed to get handle")
 End If
 
 End Sub
 
 窗体重写dispose并执行在组建列表上的post处理。
 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
 If disposing Then
 If Not (components Is Nothing) Then
 components.Dispose()
 End If
 End If
 CleanupRuntimeAPI()
 MyBase.Dispose(disposing)
End Sub
 
-删减(此处省略由Windows窗体设计器设计的代码)-
#End Region
 '5 获取启动状态。
 Private Sub Bt_GetStartState_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Bt_GetStartState.Click
 
 Me.Cursor = Cursors.WaitCursor '将光标变成沙漏状。
 
 Try
 
 '获取状态(API)。
   Dim Status As Int32
 Dim RetVal As Int32 = GetRuntimeStartState(ghWinGP, Status)
 
 '有错误吗?
 If RetVal <> API_ERROR.E_SUCCESS Then
 MsgBox("Err(" + Str(RetVal).Trim() + "):GetRuntimeStartState()")
 End If
 
 '显示状态。
   Select Case Status
Case 0
Me.StartState.Text = "Starting"
Case 1
Me.StartState.Text = "Online"
 Case 2
 Me.StartState.Text = "Offline Mode"
Case 3
Me.StartState.Text = "Transfer Mode"
Case 4
Me.StartState.Text = "Exiting"
 Case 5
 Me.StartState.Text = "Not executing"
End Select
 
Catch ex As Exception
 
MsgBox(ex.Message)
 
 End Try
 
Me.Cursor = Cursors.Default '将光标恢复原状。
 
 End Sub
 
 Private Sub GetScreenState_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles BT_GetScreenState.Click
 
Me.Cursor = Cursors.WaitCursor '将光标变成沙漏状。
 
  Try
 
   '获取状态。
   Dim Status As Int32
   Dim RetVal As Int32 = GetScreenState(ghWinGP, Status)
 
   '有错误吗?
If RetVal <> API_ERROR.E_SUCCESS Then
    MsgBox("Err(" + Str(RetVal).Trim() + "):GetScreenState()")
   End If
 
   '显示状态。
      Select Case Status
 Case 0, 1, 2
 Me.ScreenState.SelectedIndex = Status
End Select
 
  Catch ex As Exception
 
MsgBox(ex.Message)
 
 End Try
 
 Me.Cursor = Cursors.Default '将光标恢复原状。
 
 End Sub
 
 Private Sub SetScreenState_Click (ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles BT_SetScreenState.Click
 
  Me.Cursor = Cursors.WaitCursor '将光标变成沙漏状。
 
Try
 
'获取值。
   Dim State As Int32 = Me.ScreenState.SelectedIndex
Dim PosX As Int32 = Val(Me.PosX.Text)
 Dim PosY As Int32 = Val(Me.PosY.Text)
 Dim Width As Int32 = Val(Me.TX_Width.Text)
 Dim Height As Int32 = Val(Me.TX_Height.Text)
 
 '画面状态设置。
   Dim RetVal As Int32 = SetScreenState(ghWinGP, State, PosX, PosY, Width, Height)
 
'有错误吗?
If RetVal <> API_ERROR.E_SUCCESS Then
 MsgBox("Err(" + Str(RetVal).Trim() + "):SetScreenState()")
 End If
 
Catch ex As Exception
 
 MsgBox(ex.Message)
 
End Try
 
Me.Cursor = Cursors.Default '将光标恢复原状。
 
 End Sub
 
 Private Sub GetDispScreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles GetDispScreen.Click
 
Me.Cursor = Cursors.WaitCursor '将光标变成沙漏状。
 
Dim CurScrNo As Int32 '当前显示画面的画面号。
 
  Try
 
   '获取状态。
   Dim RetVal As Int32 = GetDisplayScreenNumber(ghWinGP, CurScrNo)
 
 '有错误吗?
 If RetVal <> API_ERROR.E_SUCCESS Then
MsgBox("Err(" + Str(RetVal).Trim() + "):GetDisplayScreenNumber()")
 End If
 
Catch ex As Exception
 
MsgBox(ex.Message)
 
End Try
 
Try
 
 '获取画面数。
   Dim ScreenCount As Int32 = 0
 Dim RetVal As Int32 = GetEnumScreenNumberCount(ghWinGP, ScreenCount)
 
'有错误吗?
 If RetVal <> API_ERROR.E_SUCCESS Then
MsgBox("Err(" + Str(RetVal).Trim() + "):GetEnumScreenNumberCount()")
 End If
 
'获取画面号。
   If ScreenCount > 0 Then
 
    '获取画面号。
    Dim ScreenNumber(ScreenCount - 1) As Int32
 RetVal = EnumScreenNumber(ghWinGP, ScreenCount, ScreenNumber(0))
 
 '有错误吗?
 If RetVal <> API_ERROR.E_SUCCESS Then
 MsgBox("Err(" + Str(RetVal).Trim() + "):EnumScreenNumber()")
End If
 
 ----- 显示状态-----
 
'删除全部。
    Me.CB_DispScreen.Items.Clear()
 
 ‘设置您获取的画面号。
    Dim idx As Int32
    For idx = 0 To ScreenNumber.Length - 1
     Me.CB_DispScreen.Items. Add(ScreenNumber(idx))
    Next
 
    '显示当前显示画面的画面号。
    For idx = 0 To ScreenNumber.Length - 1
 If CurScrNo = Val(Me.CB_DispScreen.Items(idx)) Then
 Me.CB_DispScreen.SelectedIndex = idx
 Exit For
 End If
Next
 
End If
 
 Catch ex As Exception
 
 MsgBox(ex.Message)
 
End Try
 
Me.Cursor = Cursors.Default '将光标恢复原状。
 
 End Sub
 
 Private Sub SetDispScreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles SetDispScreen.Click
 
  Me.Cursor = Cursors.WaitCursor '将光标变成沙漏状。
 
Try
 
'获取画面号。
   Dim Screen As Int32
 Screen = Val(Me.CB_DispScreen.Text)
 
 '更改画面号。
   Dim RetVal As Int32 = SetDisplayScreenNumber(ghWinGP, Screen)
 
 '有错误吗?
 If RetVal <> API_ERROR.E_SUCCESS Then
MsgBox("Err(" + Str(RetVal).Trim() + "):SetDisplayScreenNumber()")
 End If
 
'再次获取画面号并将它与设定值作比较,从而确定是否成功更改了画面号。
 Dim NowScrNo As Long
RetVal = GetDisplayScreenNumber(ghWinGP, NowScrNo)
If RetVal = API_ERROR.E_SUCCESS Then
 If NowScrNo = Screen Then
 'MsgBox("Screen Change Successful No=" + Str(NowScrNo))
 End If
 End If
 
 Catch ex As Exception
 
 MsgBox(ex.Message)
 
 End Try
 
Me.Cursor = Cursors.Default '将光标恢复原状。
 
 End Sub
 
 Private Sub GetProjectInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles GetProjectInfo.Click
 
  Me.Cursor = Cursors.WaitCursor '将光标变成沙漏状。
 
Try
 
 '要获取的参数范围。
   Dim ProjectFileName As New System.Text.StringBuilder(PROJECTINFO_SIZE.e_FileName)
 Dim ProjectComment As New System.Text.StringBuilder(PROJECTINFO_SIZE.e_Comment)
 Dim ProjectFastTime As New System.Text.StringBuilder(PROJECTINFO_SIZE.e_FastTime)
 Dim ProjectLastTime As New System.Text.StringBuilder(PROJECTINFO_SIZE.e_LastTime)
 Dim ProjectIDownload As New System.Text.StringBuilder(PROJECTINFO_SIZE.e_IDownload)
 Dim HMIEditorVersion As New
System.Text.StringBuilder(PROJECTINFO_SIZE.e_HMIEditorVersion)
Dim ControlEditorVersion As New
System.Text.StringBuilder(PROJECTINFO_SIZE.e_ControlEditorVersion)
 Dim MakingPerson As New System.Text.StringBuilder(PROJECTINFO_SIZE.e_MakingPerson)
 
 '获取工程信息。
   Dim RetVal As Int32
 RetVal = GetProjctInformation(ghWinGP, _
 ProjectFileName, _
ProjectComment, _
 ProjectFastTime, _
ProjectLastTime, _
ProjectIDownload, _
HMIEditorVersion, _
ControlEditorVersion, _
 MakingPerson)
 
 '有错误吗?
 If RetVal <> API_ERROR.E_SUCCESS Then
 MsgBox("Err(" + Str(RetVal).Trim() + "):GetProjctInformation()")
 End If
 
 '显示获取的信息。
   Me.Prj_File.Text = ProjectFileName.ToString()
 Me.Prj_Comment.Text = ProjectComment.ToString()
 Me.Prj_Date.Text = ProjectFastTime.ToString()
 Me.Prj_LastDate.Text = ProjectLastTime.ToString()
 Me.Prj_HMI.Text = HMIEditorVersion.ToString()
 Me.Prj_Person.Text = MakingPerson.ToString
 
Catch ex As Exception
 
MsgBox(ex.Message)
 
 End Try
 
Me.Cursor = Cursors.Default '将光标恢复原状。
 
 End Sub
 
 '13结束。
'退出时使用确认对话框。
'如果您选择对话框中的“不退出”,则WinGP不会结束。
'即便这样,返回值也是API_ERROR.E_SUCCESS。
 
 Private Sub StopWinGP_Q_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles StopWinGP_Q.Click
 
 Me.Cursor = Cursors.WaitCursor '将光标变成沙漏状
 
 Try
 
 '退出(API)。
   Dim RetVal As Int32 = StopRuntime(ghWinGP, 1)
 
 '有错误吗?
 If RetVal <> API_ERROR.E_SUCCESS Then
 MsgBox("Err(" + Str(RetVal).Trim() + "):StopRuntime()")
 End If
 
 Catch ex As Exception
 
 MsgBox(ex.Message)
 
 End Try
 
 Me.Cursor = Cursors.Default '将光标恢复原状。
 
 End Sub
 
End Class