VERSION 5.00
Begin VB.Form frmMain 
   Caption         =   "In-Sight Sample"
   ClientHeight    =   5760
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   5100
   LinkTopic       =   "Form1"
   ScaleHeight     =   5760
   ScaleWidth      =   5100
   StartUpPosition =   3  'Windows ‚ÌŠù’è’l
   Begin VB.CommandButton cmdGetBitmap 
      Caption         =   "View"
      Height          =   375
      Left            =   3720
      TabIndex        =   4
      Top             =   5280
      Width           =   1095
   End
   Begin VB.Frame fraConnect 
      Caption         =   "Connect"
      Height          =   1335
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   4815
      Begin VB.CommandButton cmdDisconnect 
         Caption         =   "Disconnect"
         Height          =   375
         Left            =   3360
         TabIndex        =   3
         Top             =   840
         Width           =   1215
      End
      Begin VB.TextBox txtConnectOption 
         Height          =   375
         Left            =   240
         TabIndex        =   2
         Text            =   "conn=eth:10.6.235.70"
         Top             =   360
         Width           =   4335
      End
      Begin VB.CommandButton cmdConnect 
         Caption         =   "Connect"
         Height          =   375
         Left            =   2040
         TabIndex        =   1
         Top             =   840
         Width           =   1200
      End
   End
   Begin VB.Image imgCamera 
      Height          =   3600
      Left            =   120
      Stretch         =   -1  'True
      Top             =   1560
      Width           =   4800
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Dim caoEng As CaoEngine
Dim caoCtrl As CaoController
Dim caoVal As CaoVariable

Private Sub Form_Load()
    Set caoEng = New CaoEngine
    
    cmdConnect.Enabled = True
    cmdDisconnect.Enabled = False
    cmdGetBitmap.Enabled = False
End Sub


Private Sub cmdConnect_Click()
    On Error GoTo ErrorProc

    ' Connect In-sight
    Set caoCtrl = caoEng.Workspaces(0).AddController("Cognex", "caoProv.Cognex.In-Sight", "", txtConnectOption.Text)
    
    ' In-Sight camera trigger
    caoCtrl.Execute "SetEvent", 8
    
    Sleep 100
    
    ' Create BITMAP
    Set caoVal = caoCtrl.AddVariable("@BITMAP")
    
    cmdConnect.Enabled = False
    cmdDisconnect.Enabled = True
    cmdGetBitmap.Enabled = True
    Exit Sub
ErrorProc:
    cmdDisconnect_Click
    MsgBox "0x" & Hex(Err.Number) & " : " & Err.Description
End Sub


Private Sub cmdDisconnect_Click()
    If Not (caoCtrl Is Nothing) Then
    
        If Not (caoVal Is Nothing) Then
            caoCtrl.Variables.Remove caoVal.Index
            Set caoVal = Nothing
        End If
    
        caoEng.Workspaces(0).Controllers.Remove caoCtrl.Index
        Set caoCtrl = Nothing
    End If
    
    cmdConnect.Enabled = True
    cmdDisconnect.Enabled = False
    cmdGetBitmap.Enabled = False
End Sub

Private Sub cmdGetBitmap_Click()
    On Error GoTo ErrorProc
    
    Dim bmp() As Byte
    Dim hBitmap As Long
    
    ' In-Sight camera trigger
    caoCtrl.Execute "SetEvent", 8
    
    ' get bitmap
    bmp = caoVal.Value
    
    ' show imagewindow
    imgCamera.Picture = GetPicture(bmp, hBitmap)
    
    Exit Sub
ErrorProc:
    MsgBox "0x" & Hex(Err.Number) & " : " & Err.Description
End Sub
