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 MenuCamera As System.Windows.Forms.MenuItem
    Friend WithEvents MenuFilter As System.Windows.Forms.MenuItem
    Friend WithEvents MenuAnalog As System.Windows.Forms.MenuItem
    Friend WithEvents MenuDevice As System.Windows.Forms.MenuItem
    Friend WithEvents MenuDevice0 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuDevice1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuDevice2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuDevice3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuDevice4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuDevice5 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuDevice6 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuDevice7 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.MenuCamera = New System.Windows.Forms.MenuItem
		Me.MenuFilter = New System.Windows.Forms.MenuItem
		Me.MenuAnalog = New System.Windows.Forms.MenuItem
		Me.MenuDevice = New System.Windows.Forms.MenuItem
		Me.MenuDevice0 = New System.Windows.Forms.MenuItem
		Me.MenuDevice1 = New System.Windows.Forms.MenuItem
		Me.MenuDevice2 = New System.Windows.Forms.MenuItem
		Me.MenuDevice3 = New System.Windows.Forms.MenuItem
		Me.MenuDevice4 = New System.Windows.Forms.MenuItem
		Me.MenuDevice5 = New System.Windows.Forms.MenuItem
		Me.MenuDevice6 = New System.Windows.Forms.MenuItem
		Me.MenuDevice7 = 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, Me.MenuDevice})
		'
		'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.MenuCamera, Me.MenuFilter, Me.MenuAnalog})
		Me.MenuSet.Text = "設定(&S)"
		'
		'MenuCamera
		'
		Me.MenuCamera.Index = 0
		Me.MenuCamera.Text = "カメラ設定(&C)"
		'
		'MenuFilter
		'
		Me.MenuFilter.Index = 1
		Me.MenuFilter.Text = "フィルター設定(&F)"
		'
		'MenuAnalog
		'
		Me.MenuAnalog.Index = 2
		Me.MenuAnalog.Text = "アナログ設定(&A)"
		'
		'MenuDevice
		'
		Me.MenuDevice.Index = 4
		Me.MenuDevice.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuDevice0, Me.MenuDevice1, Me.MenuDevice2, Me.MenuDevice3, Me.MenuDevice4, Me.MenuDevice5, Me.MenuDevice6, Me.MenuDevice7})
		Me.MenuDevice.Text = "デバイス(&D)"
		'
		'MenuDevice0
		'
		Me.MenuDevice0.Index = 0
		Me.MenuDevice0.Text = "0"
		'
		'MenuDevice1
		'
		Me.MenuDevice1.Index = 1
		Me.MenuDevice1.Text = "1"
		'
		'MenuDevice2
		'
		Me.MenuDevice2.Index = 2
		Me.MenuDevice2.Text = "2"
		'
		'MenuDevice3
		'
		Me.MenuDevice3.Index = 3
		Me.MenuDevice3.Text = "3"
		'
		'MenuDevice4
		'
		Me.MenuDevice4.Index = 4
		Me.MenuDevice4.Text = "4"
		'
		'MenuDevice5
		'
		Me.MenuDevice5.Index = 5
		Me.MenuDevice5.Text = "5"
		'
		'MenuDevice6
		'
		Me.MenuDevice6.Index = 6
		Me.MenuDevice6.Text = "6"
		'
		'MenuDevice7
		'
		Me.MenuDevice7.Index = 7
		Me.MenuDevice7.Text = "7"
		'
		'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_pCapture() As Byte
    Dim m_Bitmap As Bitmap
    Dim m_PreviewMode As Integer = 0
    Dim m_ArtCam As CArtCam = New CArtCam()

    ' フォーム開始
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 描画をダブルバッファを使用して描画（ちらつき防止）
        SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)

		OnDllReload()
    End Sub


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

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

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

        m_ArtCam.SaveImage("image.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.IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

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

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

        m_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.IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

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

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

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

        m_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.IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

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

        ' ビットマップ作成
        CreateBitmap()
        m_ArtCam.SnapShot(m_pCapture, getSize(), 1)
        Invalidate()

        m_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.IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

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

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

        m_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.IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

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

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

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

        m_PreviewMode = 4
    End Sub


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

        ElseIf 3 = m_PreviewMode Then
            m_ArtCam.SnapShot(m_pCapture, getSize(), 1)
            Invalidate()
        End If
    End Sub


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

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

                ' パレット変更
                Dim pal As ColorPalette = m_Bitmap.Palette
                Dim cpe As Color() = m_Bitmap.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.Palette = pal

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

        ' キャプチャ用の配列
        ReDim m_pCapture(getSize())
    End Sub


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

        If m_ArtCam.SetCameraDlg(Me.Handle()) Then
            Select Case m_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 MenuFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFilter.Click
        If Not m_ArtCam.IsInit Then
            MessageBox.Show("有効なデバイスを選択してください")
            Exit Sub
        End If

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


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

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


    ' デバイス変更
    Private Sub DeviceChange(ByVal Number As Integer)
        If Not Not m_ArtCam.IsInit Then
            m_ArtCam.Close()
        End If

		' デバイスが接続されているかチェックする場合は
		' GetDeviceName を使えば簡単に分かる
		' ただし、名前を取得する領域が無い場合もエラーになるので配列長は最低32は用意する事
		Dim Temp As StringBuilder = New StringBuilder(256)
'		If 0 = m_ArtCam.GetDeviceName(Number, Temp, 256) Then
'			m_PreviewMode = -1
'			m_SelectDevice = -1
'			m_StopFlg = False
'			MenuStop.Text = "一時停止"
'			Exit Sub
'		End If


		' これでデバイスの番号を変更してからカメラを表示するとデバイスが変更される
		' 注：この関数単体ではデバイスは変更されません。
		' 　　この関数を呼び出した後にデバイスを初期化する事でデバイスが変更されます
		m_ArtCam.SetDeviceNumber(Number)

		Dim i As Integer
		For i = 0 To 7
			MenuDevice.MenuItems(i).Checked = False
		Next i
		MenuDevice.MenuItems(Number).Checked = True
     End Sub

    Private Sub MenuDevice0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDevice0.Click
        DeviceChange(0)
    End Sub

    Private Sub MenuDevice1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDevice1.Click
        DeviceChange(1)
    End Sub

    Private Sub MenuDevice2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDevice2.Click
        DeviceChange(2)
    End Sub

    Private Sub MenuDevice3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDevice3.Click
        DeviceChange(3)
    End Sub

    Private Sub MenuDevice4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDevice4.Click
        DeviceChange(4)
    End Sub

    Private Sub MenuDevice5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDevice5.Click
        DeviceChange(5)
    End Sub

    Private Sub MenuDevice6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDevice6.Click
        DeviceChange(6)
    End Sub

    Private Sub MenuDevice7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDevice7.Click
        DeviceChange(7)
    End Sub

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

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

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

    Private Function LockBitmap() As BitmapData
        Select Case m_ArtCam.GetColorMode()
            Case 8, 16
                Return m_Bitmap.LockBits(New Rectangle(0, 0, getWidth, getHeight), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed)

            Case 24, 32, 48, 64
                Return m_Bitmap.LockBits(New Rectangle(0, 0, getWidth, getHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
        End Select

        Return Nothing
    End Function


    Private Sub DrawImage(ByVal g As Graphics)
        If Nothing Is m_Bitmap Then
            Return
        End If

        Dim pBitmapData As BitmapData = LockBitmap()
        If Nothing Is pBitmapData Then
            Return
        End If


        ' データをピクセルに変換
        Dim pDest As IntPtr = pBitmapData.Scan0()

        Dim i As Integer = 0
        If 16 = m_ArtCam.GetColorMode() Then
            Dim Size As Integer = getSize() / 2
            For i = 0 To Size - 1
                Dim Data1 As Byte = m_pCapture(i * 2)
                Dim Data2 As Byte = m_pCapture(i * 2 + 1)

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

                WriteByte(pDest, i, CType(dData, Byte))
            Next

        Else
            Dim Size As Integer = getSize()
            For i = 0 To Size - 1
                WriteByte(pDest, i, m_pCapture(i))
            Next
        End If


        m_Bitmap.UnlockBits(pBitmapData)

        ' 映像表示
        g.DrawImage(m_Bitmap, New Rectangle(0, 0, getWidth, getHeight))

        ' ライン描画
        Dim Pos As Point = PointToClient(Cursor.Position)
        Dim RedPen As Pen = New Pen(Color.Red, 1)
        g.DrawLine(RedPen, Pos.X, 0, Pos.X, getHeight())
        g.DrawLine(RedPen, 0, Pos.Y, getWidth(), Pos.Y)
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)

        ' ダブルバッファで描画（ちらつき防止）
        ' OnPaint内での描画はオフスクリーンバッファに書き込まれ
        ' OnPaint内の処理が全て完了した時点で画面上に描画されます
        DrawImage(e.Graphics)
    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.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.LoadLibrary(szDllName)
		If (False = res) Then
			MessageBox.Show("DLLが見つかりません\n起動した後でDLLを移動等した可能性があります")
			Return
		End If
		' Initialize は最初に呼びます
		' ここでウィンドウハンドルを設定するとWM_ERRORを取得する事が出来る
		If (False = m_ArtCam.Initialize(Me.Handle)) Then
			MessageBox.Show("ＳＤＫの初期化に失敗しました")
			Return
		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.GetDllType()) Then
			m_ArtCam.SetCameraType(SataType)
			m_ArtCam.SetDeviceNumber(0)
		Else
			menuDLL.MenuItems(DllType+1).Checked = true
		End If

		' 利用可能なデバイスをチェックする
		For i = 0 To 7
			Dim Temp As StringBuilder = New StringBuilder(256)
			If (0 <> m_ArtCam.GetDeviceName(i, Temp, 256)) Then

				MenuDevice.MenuItems(i).Text = Temp.ToString()
				MenuDevice.MenuItems(i).Enabled = True
			Else
				MenuDevice.MenuItems(i).Enabled = False
			End If
		Next
		DeviceChange(0)
	End Sub
End Class




