Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Text
Imports System.IO
Imports System.Runtime.InteropServices.Marshal
Imports Sample_VB.ArtCamSdk

Public Class Form1
    Inherits System.Windows.Forms.Form

	Dim m_DllType As Integer = -1
	Dim m_DllCount As Integer = 0
	Dim m_DllSata As Integer = -1
	Dim m_SataType As Integer = -1


#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    ' Windows フォーム デザイナを使って変更してください。  
    ' コード エディタは使用しないでください。
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents MenuFile As System.Windows.Forms.MenuItem
    Friend WithEvents MenuSave As System.Windows.Forms.MenuItem
    Friend WithEvents MenuExit As System.Windows.Forms.MenuItem
    Friend WithEvents MenuView As System.Windows.Forms.MenuItem
    Friend WithEvents MenuPreview As System.Windows.Forms.MenuItem
    Friend WithEvents MenuCallback As System.Windows.Forms.MenuItem
    Friend WithEvents MenuSnapshot As System.Windows.Forms.MenuItem
    Friend WithEvents MenuCapture As System.Windows.Forms.MenuItem
    Friend WithEvents MenuTrigger As System.Windows.Forms.MenuItem
    Friend WithEvents MenuSet As System.Windows.Forms.MenuItem
    Friend WithEvents MenuCamera1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuAnalog1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuCamera2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuFilter1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuFilter2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuAnalog2 As System.Windows.Forms.MenuItem
	Friend WithEvents MenuDLL As System.Windows.Forms.MenuItem
	Friend WithEvents menuDllReload As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.components = New System.ComponentModel.Container
		Me.MainMenu1 = New System.Windows.Forms.MainMenu
		Me.MenuFile = New System.Windows.Forms.MenuItem
		Me.MenuSave = New System.Windows.Forms.MenuItem
		Me.MenuExit = New System.Windows.Forms.MenuItem
		Me.MenuView = New System.Windows.Forms.MenuItem
		Me.MenuPreview = New System.Windows.Forms.MenuItem
		Me.MenuCallback = New System.Windows.Forms.MenuItem
		Me.MenuSnapshot = New System.Windows.Forms.MenuItem
		Me.MenuCapture = New System.Windows.Forms.MenuItem
		Me.MenuTrigger = New System.Windows.Forms.MenuItem
		Me.MenuSet = New System.Windows.Forms.MenuItem
		Me.MenuCamera1 = New System.Windows.Forms.MenuItem
		Me.MenuCamera2 = New System.Windows.Forms.MenuItem
		Me.MenuFilter1 = New System.Windows.Forms.MenuItem
		Me.MenuFilter2 = New System.Windows.Forms.MenuItem
		Me.MenuAnalog1 = New System.Windows.Forms.MenuItem
		Me.MenuAnalog2 = New System.Windows.Forms.MenuItem
		Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
		Me.MenuDLL = New System.Windows.Forms.MenuItem
		Me.menuDllReload = New System.Windows.Forms.MenuItem
		'
		'MainMenu1
		'
		Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuFile, Me.MenuView, Me.MenuSet, Me.MenuDLL})
		'
		'MenuFile
		'
		Me.MenuFile.Index = 0
		Me.MenuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuSave, Me.MenuExit})
		Me.MenuFile.Text = "ファイル(&F)"
		'
		'MenuSave
		'
		Me.MenuSave.Index = 0
		Me.MenuSave.Text = "保存(&S)"
		'
		'MenuExit
		'
		Me.MenuExit.Index = 1
		Me.MenuExit.Text = "終了(&X)"
		'
		'MenuView
		'
		Me.MenuView.Index = 1
		Me.MenuView.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuPreview, Me.MenuCallback, Me.MenuSnapshot, Me.MenuCapture, Me.MenuTrigger})
		Me.MenuView.Text = "表示(&V)"
		'
		'MenuPreview
		'
		Me.MenuPreview.Index = 0
		Me.MenuPreview.Text = "プレビュー(&P)"
		'
		'MenuCallback
		'
		Me.MenuCallback.Index = 1
		Me.MenuCallback.Text = "コールバック(&B)"
		'
		'MenuSnapshot
		'
		Me.MenuSnapshot.Index = 2
		Me.MenuSnapshot.Text = "スナップショット(&S)"
		'
		'MenuCapture
		'
		Me.MenuCapture.Index = 3
		Me.MenuCapture.Text = "キャプチャ(&C)"
		'
		'MenuTrigger
		'
		Me.MenuTrigger.Index = 4
		Me.MenuTrigger.Text = "トリガー(&T)"
		'
		'MenuSet
		'
		Me.MenuSet.Index = 2
		Me.MenuSet.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuCamera1, Me.MenuCamera2, Me.MenuFilter1, Me.MenuFilter2, Me.MenuAnalog1, Me.MenuAnalog2})
		Me.MenuSet.Text = "設定(&S)"
		'
		'MenuCamera1
		'
		Me.MenuCamera1.Index = 0
		Me.MenuCamera1.Text = "カメラ設定1(&C)"
		'
		'MenuCamera2
		'
		Me.MenuCamera2.Index = 1
		Me.MenuCamera2.Text = "カメラ設定2(&C)"
		'
		'MenuFilter1
		'
		Me.MenuFilter1.Index = 2
		Me.MenuFilter1.Text = "フィルター設定1(&F)"
		'
		'MenuFilter2
		'
		Me.MenuFilter2.Index = 3
		Me.MenuFilter2.Text = "フィルター設定2(&F)"
		'
		'MenuAnalog1
		'
		Me.MenuAnalog1.Index = 4
		Me.MenuAnalog1.Text = "アナログ設定1(&A)"
		'
		'MenuAnalog2
		'
		Me.MenuAnalog2.Index = 5
		Me.MenuAnalog2.Text = "アナログ設定2(&A)"
		'
		'Timer1
		'
		Me.Timer1.Enabled = True
		'
		'MenuDLL
		'
		Me.MenuDLL.Index = 3
		Me.MenuDLL.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.menuDllReload})
		Me.MenuDLL.Text = "DLL(&L)"
		'
		'menuDllReload
		'
		Me.menuDllReload.Index = 0
		Me.menuDllReload.Text = "Reload"
		'
		'Form1
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
		Me.ClientSize = New System.Drawing.Size(640, 481)
		Me.Menu = Me.MainMenu1
		Me.Name = "Form1"
		Me.Text = "VB Sample"

	End Sub

#End Region


    Dim m_ArtCam() As CArtCam = {New CArtCam(), New CArtCam()}
    Dim m_Bitmap() As Bitmap = {Nothing, Nothing}
    Dim m_BitmapData() As BitmapData = {Nothing, Nothing}
    Dim m_LockFlg() As Boolean = {False, False}
    Dim PreviewMode As Integer = 0


    ' フォーム開始
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
		OnDllReload()
    End Sub


    ' フォーム終了
    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Release()
    End Sub


    ' 保存
    Private Sub MenuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuSave.Click
        If Not m_ArtCam(0).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        m_ArtCam(0).SaveImage("Image1.jpg", FILETYPE.FILETYPE_JPEG_NOMAL)
        m_ArtCam(1).SaveImage("Image2.jpg", FILETYPE.FILETYPE_JPEG_NOMAL)
    End Sub


    ' 終了
    Private Sub MenuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuExit.Click
        Close()
    End Sub


    ' プレビュー　全自動で描画する
    Private Sub MenuPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPreview.Click
        If Not m_ArtCam(0).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        ' 映像の描画を停止（映像を描画してない場合に呼んでも問題ない）
        m_ArtCam(0).Close()
        m_ArtCam(1).Close()

        m_ArtCam(0).SetPreviewWindow(Me.Handle(), 0, 0, getWidth(0), getHeight(0))
        m_ArtCam(1).SetPreviewWindow(Me.Handle(), 0, 0, getWidth(1), getHeight(1))
        m_ArtCam(0).Preview()
        m_ArtCam(1).Preview()

        PreviewMode = 1
    End Sub


    ' コールバック　映像のイメージポインタを取得して自前で描画する
    Private Sub MenuCallback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuCallback.Click
        If Not m_ArtCam(0).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        ' 映像の描画を停止（映像を描画してない場合に呼んでも問題ない）
        m_ArtCam(0).Close()
        m_ArtCam(1).Close()

        ' ビットマップ作成
        CreateBitmap(0)
        CreateBitmap(1)

        ' 自分で描画する場合はウィンドウサイズを全て０にします
        ' CallBackPreview を使用する場合でもウィンドウサイズを設定すれば自動表示は行えます。
        m_ArtCam(0).SetPreviewWindow(Me.Handle(), 0, 0, 0, 0)
        m_ArtCam(1).SetPreviewWindow(Me.Handle(), 0, 0, 0, 0)
        m_ArtCam(0).CallBackPreview(Me.Handle(), m_BitmapData(0).Scan0, getSize(0), 1)
        m_ArtCam(1).CallBackPreview(Me.Handle(), m_BitmapData(1).Scan0, getSize(1), 1)

        PreviewMode = 2
    End Sub


    ' スナップショット
    Private Sub MenuSnapshot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuSnapshot.Click
        If Not m_ArtCam(0).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        ' 映像の描画を停止（映像を描画してない場合に呼んでも問題ない）
        m_ArtCam(0).Close()
        m_ArtCam(1).Close()

        ' ビットマップ作成
        CreateBitmap(0)
        CreateBitmap(1)
        m_ArtCam(0).SnapShot(m_BitmapData(0).Scan0, getSize(0), 1)
        m_ArtCam(1).SnapShot(m_BitmapData(1).Scan0, getSize(1), 1)
        UnLockBitmap(0)
        UnLockBitmap(1)

        ' 映像表示
        ' スナップショットのみ１０ビット表示を対応させる
        ' この方法ではビット演算の処理速度が遅すぎる為、
        ' 動画中では処理が間に合わずメモリアクセス違反を起こしてしまう為
        If 16 = m_ArtCam(0).GetColorMode() Then
            DrawImage16Bit(0)
            DrawImage16Bit(1)
        Else
            DrawImage(0)
            DrawImage(1)
        End If

        PreviewMode = 0
    End Sub


    ' キャプチャーモード
    Private Sub MenuCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuCapture.Click
        If Not m_ArtCam(0).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        ' ここで一旦デバイスを開放する
        m_ArtCam(0).Close()
        m_ArtCam(1).Close()

        ' ビットマップ作成
        CreateBitmap(0)
        CreateBitmap(1)
        m_ArtCam(0).Capture()
        m_ArtCam(1).Capture()

        PreviewMode = 3
    End Sub


    ' トリガーモード
    Private Sub MenuTrigger_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuTrigger.Click
        If Not m_ArtCam(0).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        ' ここで一旦デバイスを開放する
        m_ArtCam(0).Close()
        m_ArtCam(1).Close()

        ' ビットマップ作成
        CreateBitmap(0)
        CreateBitmap(1)

        ' 自分で描画する場合はウィンドウサイズを全て０にします
        ' CallBackPreview を使用する場合でもウィンドウサイズを設定すれば自動表示は行えます。
        m_ArtCam(0).SetPreviewWindow(Me.Handle, 0, 0, 0, 0)
        m_ArtCam(1).SetPreviewWindow(Me.Handle, 0, 0, 0, 0)
        m_ArtCam(0).Trigger(Me.Handle, m_BitmapData(0).Scan0, getSize(0), 1)
        m_ArtCam(1).Trigger(Me.Handle, m_BitmapData(1).Scan0, getSize(1), 1)

        PreviewMode = 4
    End Sub


    ' VBではコールバックメッセージを扱うと、不具合が出る可能性があるので描画のタイミングはタイマーで行う
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If 2 = PreviewMode Or 4 = PreviewMode Then
            UnLockBitmap(0)
            UnLockBitmap(1)

            ' 映像表示
            DrawImage(0)
            DrawImage(1)

        ElseIf 3 = PreviewMode Then
            LockBitmap(0)
            m_ArtCam(0).SnapShot(m_BitmapData(0).Scan0, getSize(0), 1)
            UnLockBitmap(0)

            LockBitmap(1)
            m_ArtCam(1).SnapShot(m_BitmapData(1).Scan0, getSize(1), 1)
            UnLockBitmap(1)

            ' 映像表示
            DrawImage(0)
            DrawImage(1)
        End If
    End Sub


    ' ビットマップ作成
    Private Sub CreateBitmap(ByVal n As Integer)
        ' ロックをしている場合は解除
        UnLockBitmap(n)

        ' 既にビットマップが作られている場合は一旦開放
        If Not IsNothing(m_Bitmap(n)) Then
            m_Bitmap(n).Dispose()
        End If

        ' オブジェクト作成
        Select Case m_ArtCam(n).GetColorMode()
            Case 8 : m_Bitmap(n) = New Bitmap(getWidth(n), getHeight(n), PixelFormat.Format8bppIndexed)

                ' パレット変更
                Dim pal As ColorPalette = m_Bitmap(n).Palette
                Dim cpe As Color() = m_Bitmap(n).Palette.Entries

                Dim i As Integer = 0
                For i = 0 To 255
                    cpe.SetValue(Color.FromArgb(i, i, i), i)
                    pal.Entries(i) = cpe(i)
                Next
                m_Bitmap(n).Palette = pal

            Case 16 : m_Bitmap(n) = New Bitmap(getWidth(n), getHeight(n), PixelFormat.Format16bppGrayScale)
            Case 24 : m_Bitmap(n) = New Bitmap(getWidth(n), getHeight(n), PixelFormat.Format24bppRgb)
            Case 32 : m_Bitmap(n) = New Bitmap(getWidth(n), getHeight(n), PixelFormat.Format32bppRgb)
            Case 48 : m_Bitmap(n) = New Bitmap(getWidth(n), getHeight(n), PixelFormat.Format48bppRgb)
            Case 64 : m_Bitmap(n) = New Bitmap(getWidth(n), getHeight(n), PixelFormat.Format64bppArgb)
        End Select

        LockBitmap(n)
    End Sub

    Private Function getSize(ByVal n As Integer) As Integer
        Return ((getWidth(n) * (m_ArtCam(n).GetColorMode() / 8) + 3) And Not (3)) * getHeight(n)
    End Function

    Private Function getWidth(ByVal n As Integer) As Integer
        Dim Size() As Integer = {1, 2, 4, 8}
        Return m_ArtCam(n).Width() / Size(m_ArtCam(n).GetSubSample())
    End Function

    Private Function getHeight(ByVal n As Integer) As Integer
        Dim Size() As Integer = {1, 2, 4, 8}
        Return m_ArtCam(n).Height() / Size(m_ArtCam(n).GetSubSample())
    End Function

    Private Sub LockBitmap(ByVal n As Integer)
        If m_LockFlg(n) Then
            Exit Sub
        End If

        Select Case m_ArtCam(0).GetColorMode()
            Case 8 : m_BitmapData(n) = m_Bitmap(n).LockBits(New Rectangle(0, 0, getWidth(n), getHeight(n)), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed)
            Case 16 : m_BitmapData(n) = m_Bitmap(n).LockBits(New Rectangle(0, 0, getWidth(n), getHeight(n)), ImageLockMode.ReadWrite, PixelFormat.Format16bppGrayScale)
            Case 24 : m_BitmapData(n) = m_Bitmap(n).LockBits(New Rectangle(0, 0, getWidth(n), getHeight(n)), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
            Case 32 : m_BitmapData(n) = m_Bitmap(n).LockBits(New Rectangle(0, 0, getWidth(n), getHeight(n)), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb)
            Case 48 : m_BitmapData(n) = m_Bitmap(n).LockBits(New Rectangle(0, 0, getWidth(n), getHeight(n)), ImageLockMode.ReadWrite, PixelFormat.Format48bppRgb)
            Case 64 : m_BitmapData(n) = m_Bitmap(n).LockBits(New Rectangle(0, 0, getWidth(n), getHeight(n)), ImageLockMode.ReadWrite, PixelFormat.Format64bppArgb)
        End Select

        m_LockFlg(n) = True
    End Sub

    Private Sub UnLockBitmap(ByVal n As Integer)
        If m_LockFlg(n) Then
            m_Bitmap(n).UnlockBits(m_BitmapData(n))
            m_LockFlg(n) = False
        End If

    End Sub

    Private Sub DrawImage(ByVal n As Integer)
        Dim g As Graphics = MyBase.CreateGraphics()
        g.DrawImage(m_Bitmap(n), New Rectangle(getWidth(n) * n, 0, getWidth(n), getHeight(n)))
        g.Dispose()
    End Sub

    Private Sub DrawImage16Bit(ByVal n As Integer)
        ' 
        Dim DrawBmp As Bitmap = New Bitmap(getWidth(n), getHeight(n), PixelFormat.Format8bppIndexed)

        ' パレット変更
        Dim pal As ColorPalette = DrawBmp.Palette
        Dim cpe As Color() = DrawBmp.Palette.Entries

        Dim i As Integer = 0
        For i = 0 To 255
            cpe.SetValue(Color.FromArgb(i, i, i), i)
            pal.Entries(i) = cpe(i)
        Next
        DrawBmp.Palette = pal

        Dim DrawBmpData As BitmapData = DrawBmp.LockBits(New Rectangle(0, 0, getWidth(n), getHeight(n)), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed)

        Dim pSrc As IntPtr = m_BitmapData(n).Scan0()
        Dim pDest As IntPtr = DrawBmpData.Scan0()

        ' カラー変換
        Dim Size As Integer = getWidth(n) * getHeight(n) - 1
        For i = 0 To Size
            Dim Data1 As Byte = ReadByte(pSrc, i * 2)
            Dim Data2 As Byte = ReadByte(pSrc, i * 2 + 1)

            ' .NET2002 ではシフト演算がありません
            ' .NET2003 以降をお使いの方は下記のコードで動きます
            'Dim Data3 As Byte = (Data2 << 6) | (Data1 >> 2)

            Dim dData As Object = (Data1 / 4) + (Data2 * 64)
            If 255 < dData Then
                dData = 255
            End If

            Dim Data3 As Byte = CType(dData, Byte)

            WriteByte(pDest, i, Data3)
        Next

        DrawBmp.UnlockBits(DrawBmpData)
        UnLockBitmap(n)

        ' 映像表示
        Dim g As Graphics = MyBase.CreateGraphics()
        g.DrawImage(DrawBmp, New Rectangle(getWidth(n) * n, 0, getWidth(n), getHeight(n)))
        g.Dispose()
    End Sub

    ' カメラ開放
    Private Sub Release()
        m_ArtCam(0).Release()
        m_ArtCam(1).Release()
        UnLockBitmap(0)
        UnLockBitmap(1)
        PreviewMode = 0
    End Sub


    ' カメラ設定
    Private Sub MenuCamera_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuCamera1.Click
        If Not m_ArtCam(0).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        If m_ArtCam(0).SetCameraDlg(Me.Handle()) Then
            Select Case PreviewMode
                Case 1
                    MenuPreview_Click(sender, e)
                Case 2
                    MenuCallback_Click(sender, e)
                Case 3
                    MenuCapture_Click(sender, e)
                Case 4
                    MenuTrigger_Click(sender, e)
            End Select
        End If
    End Sub

    Private Sub MenuCamera2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuCamera2.Click
        If Not m_ArtCam(1).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        If m_ArtCam(1).SetCameraDlg(Me.Handle()) Then
            Select Case PreviewMode
                Case 1
                    MenuPreview_Click(sender, e)
                Case 2
                    MenuCallback_Click(sender, e)
                Case 3
                    MenuCapture_Click(sender, e)
                Case 4
                    MenuTrigger_Click(sender, e)
            End Select
        End If
    End Sub


    ' イメージ設定
    Private Sub MenuFilter1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFilter1.Click
        If Not m_ArtCam(0).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        m_ArtCam(0).SetImageDlg(Me.Handle())
    End Sub

    Private Sub MenuFilter2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFilter2.Click
        If Not m_ArtCam(1).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        m_ArtCam(1).SetImageDlg(Me.Handle())
    End Sub


    ' アナログ設定
    Private Sub MenuAnalog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuAnalog1.Click
        If Not m_ArtCam(0).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        m_ArtCam(0).SetAnalogDlg(Me.Handle())
    End Sub

    Private Sub MenuAnalog2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuAnalog2.Click
        If Not m_ArtCam(1).IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

        m_ArtCam(1).SetAnalogDlg(Me.Handle())
    End Sub

	Private Sub OnDllReload()
		' DLLリストを削除
		If 1 < MenuDll.MenuItems.Count Then
			Dim i As Integer
			For i = 0 To m_DllCount-1
				MenuDll.MenuItems.RemoveAt(1)
			Next
		End If
		m_DllCount = 0
		m_DllSata  = -1

		' DLL検索
		Dim szDirPath As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
		Dim files As String() = Directory.GetFiles(szDirPath, "Art*.dll")
		For Each szFileName As String In files
			Dim ArtCam As CArtCam = New CArtCam()
			If (ArtCam.LoadLibrary(szFileName)) Then

				' バージョンを取得して表示させます
				Dim ver As Long = ArtCam.GetDllVersion() And &HFFFF
				Dim szMenu As String = String.Format("{0}{1}Version {2:D4}", Path.GetFileNameWithoutExtension(szFileName), ControlChars.Tab, ver)

				' メニューに追加
				m_DllCount += 1
				If (ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_SATA = ArtCam.GetDllVersion() >> 16) Then
					Dim menuSata As MenuItem = New MenuItem()
					menuSata.Text = szMenu
					Dim CameraName() As String = new String() { _
										"LVDS", _
										"300MI", _
										"500MI", _
										"MV413", _
										"800MI", _
										"036MI", _
										"150P3", _
										"267KY", _
										"274KY", _
										"625KY", _
										"130MI", _
										"200MI" _
									}
					Dim i As Integer
					For i=0 To CameraName.Length -1
						Dim mi As MenuItem = New MenuItem()
						mi.Text = CameraName(i)
						AddHandler mi.Click, AddressOf OnMenuDLLSelect
						menuSata.MenuItems.Add(i, mi)
					Next i
					menuDLL.MenuItems.Add(m_DllCount, menuSata)
					m_DllSata = m_DllCount-1
				Else
					Dim mi As MenuItem = New MenuItem()
					mi.Text = szMenu
					AddHandler mi.Click, AddressOf OnMenuDLLSelect
					MenuDll.MenuItems.Add(m_DllCount, mi)
				End If
			End If
		Next

	End Sub
	' DLL選択用メニューハンドラ
	Private Sub OnMenuDLLSelect(ByVal sender As System.Object, ByVal e As System.EventArgs)
		Dim id As Integer =  menuDLL.MenuItems.IndexOf(CType(sender, MenuItem))
		If (id > -1) Then
			OnDllChange(sender, e, id-1, -1)
		Else
			Dim type As Integer = CType(sender, MenuItem).Index
			If (ARTCAM_CAMERATYPE_SATA.ARTCAM_CAMERATYPE_SATA_LVDS <= type And type <= ARTCAM_CAMERATYPE_SATA.ARTCAM_CAMERATYPE_SATA_200MI) Then
				OnDllChange(sender, e, m_DllSata, type)
			End If
		End If
	End Sub

	Private Sub menuDllReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuDllReload.Click
		OnDllReload()

	End Sub
	

	Private Sub OnDllChange(ByVal sender As System.Object, ByVal e As System.EventArgs, DllType as Integer, SataType As Integer)
		Release()
		m_ArtCam(0).FreeLibrary()
		m_ArtCam(1).FreeLibrary()

		If 0 = m_DllCount Then
			Return
		End If
		Dim stMenu As String = MenuDll.MenuItems(DllType + 1).Text
		Dim stArray As String() = stMenu.Split(ControlChars.Tab)
		Dim szDllName As String = String.Format("{0}.dll", stArray(0))
		Dim res As Boolean = m_ArtCam(0).LoadLibrary(szDllName)
		If (False = res) Then
			MessageBox.Show("DLLが見つかりません\n起動した後でDLLを移動等した可能性があります")
			Return
		Else
			m_ArtCam(1).LoadLibrary(szDllName)
		End If
		' Initialize は最初に呼びます
		' ここでウィンドウハンドルを設定するとWM_ERRORを取得する事が出来る
		If (False = m_ArtCam(0).Initialize(Me.Handle)) Then
			MessageBox.Show("ＳＤＫの初期化に失敗しました")
			Return
		Else
			m_ArtCam(1).Initialize(Me.Handle)
		End If
		m_DllType = DllType
		m_SataType = SataType

		' メニューをチェックする
		Dim i As Integer
		For i = 0 To m_DllCount - 1
			MenuDll.MenuItems(i + 1).Checked = False
		Next
		' SATAカメラは更にカメラのタイプを指定する
		If (-1 <> SataType And ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_SATA = m_ArtCam(0).GetDllVersion()>>16) Then
			m_ArtCam(0).SetCameraType(SataType)
			m_ArtCam(1).SetCameraType(SataType)
		End If

		' デバイスが接続されているかどうかは、デバイス名を取得すれば分かる
		Dim Temp As StringBuilder = new StringBuilder( 256 )
		If(0 = m_ArtCam(0).GetDeviceName(0, Temp, 256)) Then
			m_ArtCam(0).FreeLibrary()
		End If
		if(0 = m_ArtCam(1).GetDeviceName(1, Temp, 256)) Then
			' デバイスが検出できなかった
			' デバイスが１つしか検出できなかった際に２つのクラスからアクセスがあると
			' １つのデバイスに二重アクセスをしてしまうので注意
			' クラスを解放するか、関数をコールしないようにしてください

			' とりあえずクラスを開放してしまえば関数をコールしても安全に失敗する。
			m_ArtCam(1).FreeLibrary()
		End If


		m_ArtCam(0).SetDeviceNumber(0)
		m_ArtCam(1).SetDeviceNumber(1)

		' ２カメラを同時に表示する際は必ずハーフクロックにしてください
		m_ArtCam(0).SetHalfClock(1)
		m_ArtCam(1).SetHalfClock(1)


		' メニューをチェックする
		For i = 0 To m_DllCount-1
			menuDLL.MenuItems(i + 1).Checked = false
		Next
		If Not (ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_SATA = m_ArtCam(0).GetDllVersion()>>16) Then
			menuDLL.MenuItems(DllType+1).Checked = true
		End If



	End Sub
	
End Class




