using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using ArtCamSdk;


namespace Sample_CS
{
	/// <summary>
	/// Form1 の概要の説明です。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.MainMenu mainMenu1;

		private byte[] m_pCapture;
		private Bitmap m_Bitmap = null;
		private int m_PreviewMode = -1;
		private int m_SelectDevice = -1;
		private CArtCam m_CArtCam = new CArtCam();
		private string m_RecName;
		private bool m_SaveFlg = false;
		private bool m_StopFlg = false;

		private int  m_ViewScale = 100;

		private ARTCAM_CAMERATYPE m_CurrentCameraType;
		private int m_DllType  = -1;
		private int m_DllCount = 0;
		private int m_DllSata  = -1;
		private int m_SataType = -1;

		private System.Windows.Forms.MenuItem menuFile;
		private System.Windows.Forms.MenuItem menuSave;
		private System.Windows.Forms.MenuItem menuExit;
		private System.Windows.Forms.MenuItem menuView;
		private System.Windows.Forms.MenuItem menuPreview;
		private System.Windows.Forms.MenuItem menuCallback;
		private System.Windows.Forms.MenuItem menuSnapshot;
		private System.Windows.Forms.MenuItem menuCapture;
		private System.Windows.Forms.MenuItem menuTrigger;
		private System.Windows.Forms.MenuItem menuSet;
		private System.Windows.Forms.MenuItem menuCamera;
		private System.Windows.Forms.MenuItem menuFilter;
		private System.Windows.Forms.MenuItem menuAnalog;
		private System.Windows.Forms.MenuItem menuDevice;
		private System.Windows.Forms.MenuItem menuDevice0;
		private System.Windows.Forms.MenuItem menuDevice1;
		private System.Windows.Forms.MenuItem menuDevice2;
		private System.Windows.Forms.MenuItem menuDevice3;
		private System.Windows.Forms.MenuItem menuDevice4;
		private System.Windows.Forms.MenuItem menuDevice5;
		private System.Windows.Forms.MenuItem menuDevice6;
		private System.Windows.Forms.MenuItem menuDevice7;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuRec;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private System.Windows.Forms.Timer timerRec;
		private System.Windows.Forms.MenuItem menuStop;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuDLL;
		private System.Windows.Forms.MenuItem menuDllReload;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.PictureBox ImageBox;
		private System.Windows.Forms.Panel ImagePanel;
		private System.Windows.Forms.StatusBarPanel sbPanelSdkStatus;
		private System.Windows.Forms.StatusBarPanel sbPanelFps;
		private System.Windows.Forms.StatusBarPanel sbPanelScale;
		private System.Windows.Forms.StatusBarPanel sbPanelExp;
		private MenuItem menuItem6;
		private MenuItem MenuUserCorrection;
		
		private System.Windows.Forms.Timer timer1;


		[DllImport("user32.dll")]
		public static extern IntPtr GetWindow (IntPtr hWnd, int uCmd);

		[DllImport("user32.dll")]
		public static extern int SetWindowPos (IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);


		public Form1()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );

			Release();
			m_CArtCam.FreeLibrary();

		}

		#region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
			this.menuFile = new System.Windows.Forms.MenuItem();
			this.menuSave = new System.Windows.Forms.MenuItem();
			this.menuRec = new System.Windows.Forms.MenuItem();
			this.menuExit = new System.Windows.Forms.MenuItem();
			this.menuView = new System.Windows.Forms.MenuItem();
			this.menuPreview = new System.Windows.Forms.MenuItem();
			this.menuCallback = new System.Windows.Forms.MenuItem();
			this.menuSnapshot = new System.Windows.Forms.MenuItem();
			this.menuCapture = new System.Windows.Forms.MenuItem();
			this.menuTrigger = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuStop = new System.Windows.Forms.MenuItem();
			this.menuSet = new System.Windows.Forms.MenuItem();
			this.menuCamera = new System.Windows.Forms.MenuItem();
			this.menuFilter = new System.Windows.Forms.MenuItem();
			this.menuAnalog = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.MenuUserCorrection = new System.Windows.Forms.MenuItem();
			this.menuDLL = new System.Windows.Forms.MenuItem();
			this.menuDllReload = new System.Windows.Forms.MenuItem();
			this.menuDevice = new System.Windows.Forms.MenuItem();
			this.menuDevice0 = new System.Windows.Forms.MenuItem();
			this.menuDevice1 = new System.Windows.Forms.MenuItem();
			this.menuDevice2 = new System.Windows.Forms.MenuItem();
			this.menuDevice3 = new System.Windows.Forms.MenuItem();
			this.menuDevice4 = new System.Windows.Forms.MenuItem();
			this.menuDevice5 = new System.Windows.Forms.MenuItem();
			this.menuDevice6 = new System.Windows.Forms.MenuItem();
			this.menuDevice7 = new System.Windows.Forms.MenuItem();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			this.timerRec = new System.Windows.Forms.Timer(this.components);
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.sbPanelSdkStatus = new System.Windows.Forms.StatusBarPanel();
			this.sbPanelExp = new System.Windows.Forms.StatusBarPanel();
			this.sbPanelFps = new System.Windows.Forms.StatusBarPanel();
			this.sbPanelScale = new System.Windows.Forms.StatusBarPanel();
			this.ImageBox = new System.Windows.Forms.PictureBox();
			this.ImagePanel = new System.Windows.Forms.Panel();
			((System.ComponentModel.ISupportInitialize)(this.sbPanelSdkStatus)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.sbPanelExp)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.sbPanelFps)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.sbPanelScale)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.ImageBox)).BeginInit();
			this.ImagePanel.SuspendLayout();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuFile,
            this.menuView,
            this.menuSet,
            this.menuDLL,
            this.menuDevice});
			// 
			// menuFile
			// 
			this.menuFile.Index = 0;
			this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuSave,
            this.menuRec,
            this.menuExit});
			this.menuFile.Text = "ファイル(&F)";
			this.menuFile.Popup += new System.EventHandler(this.menuFile_Popup);
			// 
			// menuSave
			// 
			this.menuSave.Index = 0;
			this.menuSave.Text = "保存(&S)";
			this.menuSave.Click += new System.EventHandler(this.menuSave_Click);
			// 
			// menuRec
			// 
			this.menuRec.Index = 1;
			this.menuRec.Text = "録画(&R)";
			this.menuRec.Click += new System.EventHandler(this.menuRec_Click);
			// 
			// menuExit
			// 
			this.menuExit.Index = 2;
			this.menuExit.Text = "終了(&X)";
			this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
			// 
			// menuView
			// 
			this.menuView.Index = 1;
			this.menuView.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuPreview,
            this.menuCallback,
            this.menuSnapshot,
            this.menuCapture,
            this.menuTrigger,
            this.menuItem5,
            this.menuStop});
			this.menuView.Text = "表示(&V)";
			this.menuView.Popup += new System.EventHandler(this.menuView_Popup);
			// 
			// menuPreview
			// 
			this.menuPreview.Index = 0;
			this.menuPreview.Text = "プレビュー(&P)";
			this.menuPreview.Click += new System.EventHandler(this.menuPreview_Click);
			// 
			// menuCallback
			// 
			this.menuCallback.Index = 1;
			this.menuCallback.Text = "コールバック(&B)";
			this.menuCallback.Click += new System.EventHandler(this.menuCallback_Click);
			// 
			// menuSnapshot
			// 
			this.menuSnapshot.Index = 2;
			this.menuSnapshot.Text = "スナップショット(&S)";
			this.menuSnapshot.Click += new System.EventHandler(this.menuSnapshot_Click);
			// 
			// menuCapture
			// 
			this.menuCapture.Index = 3;
			this.menuCapture.Text = "キャプチャ(&C)";
			this.menuCapture.Click += new System.EventHandler(this.menuCapture_Click);
			// 
			// menuTrigger
			// 
			this.menuTrigger.Index = 4;
			this.menuTrigger.Text = "トリガー(&T)";
			this.menuTrigger.Click += new System.EventHandler(this.menuTrigger_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 5;
			this.menuItem5.Text = "-";
			// 
			// menuStop
			// 
			this.menuStop.Index = 6;
			this.menuStop.Text = "一時停止";
			this.menuStop.Click += new System.EventHandler(this.menuStop_Click);
			// 
			// menuSet
			// 
			this.menuSet.Index = 2;
			this.menuSet.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuCamera,
            this.menuFilter,
            this.menuAnalog,
            this.menuItem1,
            this.menuItem2,
            this.menuItem3,
            this.menuItem4,
            this.menuItem7,
            this.menuItem6,
            this.MenuUserCorrection});
			this.menuSet.Text = "設定(&S)";
			this.menuSet.Popup += new System.EventHandler(this.menuSet_Popup);
			this.menuSet.Click += new System.EventHandler(this.menuSet_Click);
			// 
			// menuCamera
			// 
			this.menuCamera.Index = 0;
			this.menuCamera.Text = "カメラ設定(&C)";
			this.menuCamera.Click += new System.EventHandler(this.menuCamera_Click);
			// 
			// menuFilter
			// 
			this.menuFilter.Index = 1;
			this.menuFilter.Text = "フィルター設定(&F)";
			this.menuFilter.Click += new System.EventHandler(this.menuFilter_Click);
			// 
			// menuAnalog
			// 
			this.menuAnalog.Index = 2;
			this.menuAnalog.Text = "アナログ設定(&A)";
			this.menuAnalog.Click += new System.EventHandler(this.menuAnalog_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 3;
			this.menuItem1.Text = "-";
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 4;
			this.menuItem2.Text = "ユーザーサイズ設定";
			this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 5;
			this.menuItem3.Text = "ユーザーフィルター設定";
			this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 6;
			this.menuItem4.Text = "ユーザーIO設定";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 7;
			this.menuItem7.Text = "ユーザーモニタ設定";
			this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click_1);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 8;
			this.menuItem6.Text = "-";
			// 
			// MenuUserCorrection
			// 
			this.MenuUserCorrection.Index = 9;
			this.MenuUserCorrection.Text = "TNIRカメラ補正設定";
			this.MenuUserCorrection.Click += new System.EventHandler(this.MenuUserCorrection_Click);
			// 
			// menuDLL
			// 
			this.menuDLL.Index = 3;
			this.menuDLL.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuDllReload});
			this.menuDLL.Text = "DLL(&L)";
			// 
			// menuDllReload
			// 
			this.menuDllReload.Index = 0;
			this.menuDllReload.Text = "Reload";
			this.menuDllReload.Click += new System.EventHandler(this.OnDllReload);
			// 
			// menuDevice
			// 
			this.menuDevice.Index = 4;
			this.menuDevice.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuDevice0,
            this.menuDevice1,
            this.menuDevice2,
            this.menuDevice3,
            this.menuDevice4,
            this.menuDevice5,
            this.menuDevice6,
            this.menuDevice7});
			this.menuDevice.Text = "デバイス(&D)";
			// 
			// menuDevice0
			// 
			this.menuDevice0.Index = 0;
			this.menuDevice0.Text = "0";
			this.menuDevice0.Click += new System.EventHandler(this.menuDevice0_Click);
			// 
			// menuDevice1
			// 
			this.menuDevice1.Index = 1;
			this.menuDevice1.Text = "1";
			this.menuDevice1.Click += new System.EventHandler(this.menuDevice1_Click);
			// 
			// menuDevice2
			// 
			this.menuDevice2.Index = 2;
			this.menuDevice2.Text = "2";
			this.menuDevice2.Click += new System.EventHandler(this.menuDevice2_Click);
			// 
			// menuDevice3
			// 
			this.menuDevice3.Index = 3;
			this.menuDevice3.Text = "3";
			this.menuDevice3.Click += new System.EventHandler(this.menuDevice3_Click);
			// 
			// menuDevice4
			// 
			this.menuDevice4.Index = 4;
			this.menuDevice4.Text = "4";
			this.menuDevice4.Click += new System.EventHandler(this.menuDevice4_Click);
			// 
			// menuDevice5
			// 
			this.menuDevice5.Index = 5;
			this.menuDevice5.Text = "5";
			this.menuDevice5.Click += new System.EventHandler(this.menuDevice5_Click);
			// 
			// menuDevice6
			// 
			this.menuDevice6.Index = 6;
			this.menuDevice6.Text = "6";
			this.menuDevice6.Click += new System.EventHandler(this.menuDevice6_Click);
			// 
			// menuDevice7
			// 
			this.menuDevice7.Index = 7;
			this.menuDevice7.Text = "7";
			this.menuDevice7.Click += new System.EventHandler(this.menuDevice7_Click);
			// 
			// timer1
			// 
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// saveFileDialog1
			// 
			this.saveFileDialog1.FileName = "doc1";
			// 
			// timerRec
			// 
			this.timerRec.Tick += new System.EventHandler(this.timerRec_Tick);
			// 
			// statusBar1
			// 
			this.statusBar1.Dock = System.Windows.Forms.DockStyle.None;
			this.statusBar1.Location = new System.Drawing.Point(0, 456);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
            this.sbPanelSdkStatus,
            this.sbPanelExp,
            this.sbPanelFps,
            this.sbPanelScale});
			this.statusBar1.ShowPanels = true;
			this.statusBar1.Size = new System.Drawing.Size(640, 16);
			this.statusBar1.SizingGrip = false;
			this.statusBar1.TabIndex = 0;
			this.statusBar1.PanelClick += new System.Windows.Forms.StatusBarPanelClickEventHandler(this.statusBar1_PanelClick);
			// 
			// sbPanelSdkStatus
			// 
			this.sbPanelSdkStatus.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
			this.sbPanelSdkStatus.Name = "sbPanelSdkStatus";
			this.sbPanelSdkStatus.Text = "初期化されていません";
			this.sbPanelSdkStatus.Width = 300;
			// 
			// sbPanelExp
			// 
			this.sbPanelExp.Alignment = System.Windows.Forms.HorizontalAlignment.Right;
			this.sbPanelExp.MinWidth = 100;
			this.sbPanelExp.Name = "sbPanelExp";
			this.sbPanelExp.Width = 120;
			// 
			// sbPanelFps
			// 
			this.sbPanelFps.Alignment = System.Windows.Forms.HorizontalAlignment.Right;
			this.sbPanelFps.Name = "sbPanelFps";
			this.sbPanelFps.Text = "0.0fps";
			this.sbPanelFps.Width = 120;
			// 
			// sbPanelScale
			// 
			this.sbPanelScale.Name = "sbPanelScale";
			this.sbPanelScale.Text = "100%";
			// 
			// ImageBox
			// 
			this.ImageBox.Location = new System.Drawing.Point(16, 16);
			this.ImageBox.Name = "ImageBox";
			this.ImageBox.Size = new System.Drawing.Size(40, 40);
			this.ImageBox.TabIndex = 1;
			this.ImageBox.TabStop = false;
			this.ImageBox.Paint += new System.Windows.Forms.PaintEventHandler(this.ImageBox_Paint);
			// 
			// ImagePanel
			// 
			this.ImagePanel.AutoScroll = true;
			this.ImagePanel.BackColor = System.Drawing.SystemColors.Control;
			this.ImagePanel.Controls.Add(this.ImageBox);
			this.ImagePanel.Location = new System.Drawing.Point(48, 32);
			this.ImagePanel.Name = "ImagePanel";
			this.ImagePanel.Size = new System.Drawing.Size(552, 400);
			this.ImagePanel.TabIndex = 2;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.BackColor = System.Drawing.SystemColors.AppWorkspace;
			this.ClientSize = new System.Drawing.Size(640, 481);
			this.Controls.Add(this.ImagePanel);
			this.Controls.Add(this.statusBar1);
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds;
			this.Text = "C# Sample";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.Resize += new System.EventHandler(this.Form1_Resize);
			((System.ComponentModel.ISupportInitialize)(this.sbPanelSdkStatus)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.sbPanelExp)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.sbPanelFps)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.sbPanelScale)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.ImageBox)).EndInit();
			this.ImagePanel.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// アプリケーションのメイン エントリ ポイントです。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		protected override unsafe void WndProc(ref Message m)
		{
			// WM_GRAPHNOTIFY
			if(DLL_MESSAGE.WM_GRAPHNOTIFY == (DLL_MESSAGE)m.Msg)
			{
			}
				// WM_ERROR
			else if(DLL_MESSAGE.WM_ERROR == (DLL_MESSAGE)m.Msg)
			{
				int error = (int)m.LParam;
				switch((ARTCAMSDK_ERROR)error){
					case ARTCAMSDK_ERROR.ARTCAMSDK_NOERROR :		sbPanelSdkStatus.Text = "正常";										break;	
					case ARTCAMSDK_ERROR.ARTCAMSDK_NOT_INITIALIZE:	sbPanelSdkStatus.Text = "初期化されてません";						break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_DISABLEDDEVICE:	sbPanelSdkStatus.Text = "利用不可能なデバイス";						break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_CREATETHREAD:	sbPanelSdkStatus.Text = "画像取り込み用スレッド作成に失敗";			break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_CREATEWINDOW:	sbPanelSdkStatus.Text = "ウィンドウ作成に失敗";						break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_OUTOFMEMORY:		sbPanelSdkStatus.Text = "メモリの確保に失敗";						break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_CAMERASET:		sbPanelSdkStatus.Text = "カメラ（デバイス）の設定でエラー";			break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_CAMERASIZE:		sbPanelSdkStatus.Text = "カメラ（デバイス）のサイズ設定でエラー";	break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_CAPTURE:			sbPanelSdkStatus.Text = "映像取り込みで失敗";						break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_PARAM:			sbPanelSdkStatus.Text = "引数が間違ってます";						break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_DIRECTSHOW:		sbPanelSdkStatus.Text = "DirectShow 初期化エラー";					break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_UNSUPPORTED:		sbPanelSdkStatus.Text = "この機能はサポートされていません";			break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_UNKNOWN:			sbPanelSdkStatus.Text = "不明のエラー";								break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_CAPTURELOST:		sbPanelSdkStatus.Text = "デバイスが消失";							break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_FILENOTFOUND:	sbPanelSdkStatus.Text = "指定ファイルが見つからない";				break;
					case ARTCAMSDK_ERROR.ARTCAMSDK_FPGASET:			sbPanelSdkStatus.Text = "FPGAの設定でエラー";						break;
					default:										sbPanelSdkStatus.Text = "不明のエラー";								break;
				}
			}
				// WM_GRAPHPAINT
			else if(DLL_MESSAGE.WM_GRAPHPAINT == (DLL_MESSAGE)m.Msg)
			{
				GP_INFO* pInfo = (GP_INFO*)m.WParam;
				if(pInfo != null)
				{
					m_SaveFlg = true;
					ImageBox.Invalidate();
					float fps = ((float)pInfo->lFps / 10.0f);
					sbPanelFps.Text = fps.ToString("F1") + "fps";
				}

			}
			else
			{
				base.WndProc(ref m);
			}
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			// 描画をダブルバッファを使用して描画（ちらつき防止）
			this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

			//設定ファイルの読み込み
			if(File.Exists("Sample.xml"))
			{
				System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(int[]));
				System.IO.FileStream fs = new System.IO.FileStream("Sample.xml", System.IO.FileMode.Open);
				int[] Type = new int[2];
				Type = (int[])ser.Deserialize(fs);
				fs.Close();
				m_DllType = Type[0];
				m_SataType= Type[1];
			}
			OnDllReload(sender, e);
			if( -1 != m_DllType){
				OnDllChange((object)0, System.EventArgs.Empty, m_DllType, m_SataType);
			}
		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			//設定ファイルの書き込み
			if(m_DllType != -1)
			{
				int[] Type = {(int)m_DllType, (int)m_SataType};
				System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(int[]));
				System.IO.FileStream fs = new System.IO.FileStream("Sample.xml", System.IO.FileMode.Create);
				ser.Serialize(fs, Type);
				fs.Close();
			}

			Release();
		}

		private void Release()
		{
			m_CArtCam.Release();

			if(m_Bitmap != null)
			{
				m_Bitmap.Dispose();
				m_Bitmap = null;
			}
			
			m_PreviewMode = -1;
			timer1.Enabled = false;
			timerRec.Enabled = false;
			m_SaveFlg = false;
			m_StopFlg = false;
		}


		// 保存
		private void menuSave_Click(object sender, System.EventArgs e)
		{
			saveFileDialog1.Filter = "BITMAPファイル(*.bmp)|*.bmp|JPEGファイル(*.jpg)|*.jpg;*.jpeg|TIFFファイル(*.tif)|*.tif;*.tiff|PNGファイル(*.png)|*.png|RAWファイル(*.raw)|*.raw||";

			if(DialogResult.OK == saveFileDialog1.ShowDialog())
			{
				// 拡張子は自分で設定させる
				FileInfo fileInfo = new FileInfo(saveFileDialog1.FileName);

				string str = fileInfo.FullName.Remove(fileInfo.FullName.Length - fileInfo.Extension.Length, fileInfo.Extension.Length);
				switch(saveFileDialog1.FilterIndex)
				{
					case 1:	str += ".bmp";	m_CArtCam.SaveImage(str, FILETYPE.FILETYPE_BITMAP);		break;
					case 2:	str += ".jpg";	m_CArtCam.SaveImage(str, FILETYPE.FILETYPE_JPEG_HIGH);	break;
					case 3:	str += ".tif";	m_CArtCam.SaveImage(str, FILETYPE.FILETYPE_TIFF);		break;
					case 4:	str += ".png";	m_CArtCam.SaveImage(str, FILETYPE.FILETYPE_PNG);		break;
					case 5:	str += ".raw";	m_CArtCam.SaveImage(str, FILETYPE.FILETYPE_RAW);		break;
				}
			}
		}

		// 録画
		private void menuRec_Click(object sender, System.EventArgs e)
		{
			saveFileDialog1.Filter = "AVIファイル(*.avi)|*.avi||";
			saveFileDialog1.DefaultExt = "avi";

			if(DialogResult.OK == saveFileDialog1.ShowDialog())
			{
				m_RecName = saveFileDialog1.FileName;

				// 一旦停止
				m_CArtCam.Close();

				// 録画中は別ウィンドウに表示してみる
				// hWndがNULLだった場合はSDK側でウィンドウを出すのでサイズはあまり関係ない
				m_CArtCam.SetPreviewWindow(IntPtr.Zero, 0, 0, 0, 0);

				// ５秒間録画
				m_CArtCam.Record(m_RecName, 5000, 1);


				// 録画終了のタイミングはSDKから送られてこないので、ソフト側で止める
				// 念のため１秒多めにタイマーをまわす
				timerRec.Interval = 6000;
				timerRec.Enabled = true;
			}
		}

		// 録画タイマー
		private void timerRec_Tick(object sender, System.EventArgs e)
		{
			timerRec.Enabled = false;
			m_CArtCam.Close();

			if(DialogResult.Yes == MessageBox.Show("録画が完了しました。\n保存したファイルを再生しますか？", "録画完了", MessageBoxButtons.YesNo))
			{
				System.Diagnostics.Process.Start(m_RecName);
			}			
		}

		// 終了
		private void menuExit_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		// プレビュー　全自動で描画する
		private void menuPreview_Click(object sender, System.EventArgs e)
		{
			if(!m_CArtCam.IsInit())
			{
				MessageBox.Show("有効なデバイスを選択してください");
				return;
			}

			ImagePanel.Visible = false;
			timer1.Enabled = false;

			// ここで一旦デバイスを開放する
			m_CArtCam.Close();

			// 表示するウィンドウを設定する
			// hWnd にNULLを設定すると新しいウィンドウを作成して表示します
			m_CArtCam.SetPreviewWindow(this.Handle, 0, 0, this.Width, this.Height);

			m_CArtCam.Preview();

			// メニューをチェックする
			menuPreview.Checked  = true;
			menuCallback.Checked = false;
			menuCapture.Checked  = false;
			menuTrigger.Checked  = false;

			m_PreviewMode = 0;
			m_SaveFlg = true;
			m_StopFlg = false;
			menuStop.Text = "一時停止";

			ImageBox.Invalidate();
		}

		// コールバック　映像のイメージポインタを取得して自前で描画する
		private void menuCallback_Click(object sender, System.EventArgs e)
		{
			if(!m_CArtCam.IsInit())
			{
				MessageBox.Show("有効なデバイスを選択してください");
				return;
			}

			ImagePanel.Visible = true;
			timer1.Enabled = false;

			// ここで一旦デバイスを開放する
			m_CArtCam.Close();

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

			// 取り込み用領域作成
			CreateBitmap();

			ImageBox.SetBounds(0, 0, getWidth(), getHeight());

			// 映像を取り込む
			m_CArtCam.CallBackPreview(Handle, m_pCapture, getSize(), 1);

			// メニューをチェックする
			menuPreview.Checked  = false;
			menuCallback.Checked = true;
			menuCapture.Checked  = false;
			menuTrigger.Checked  = false;

			m_PreviewMode = 1;
			m_StopFlg = false;
			menuStop.Text = "一時停止";

			ImageBox.Invalidate();
		}

		// スナップショット
		private void menuSnapshot_Click(object sender, System.EventArgs e)
		{
			if(!m_CArtCam.IsInit())
			{
				MessageBox.Show("有効なデバイスを選択してください");
				return;
			}

			ImagePanel.Visible = true;
			timer1.Enabled = false;

			// ここで一旦デバイスを開放する
			m_CArtCam.Close();

			// 取り込み用領域作成
			CreateBitmap();

			ImageBox.SetBounds(0, 0, getWidth(), getHeight());

			// 映像を取り込む
			m_CArtCam.SnapShot(m_pCapture, getSize(), 1);


			// メニューをチェックする
			menuPreview.Checked  = false;
			menuCallback.Checked = false;
			menuCapture.Checked  = false;
			menuTrigger.Checked  = false;

			m_PreviewMode = 2;
			m_SaveFlg = true;
			m_StopFlg = false;
			menuStop.Text = "一時停止";

			timer1.Enabled = true;
			timer1.Interval = 100;

			ImageBox.Invalidate();
		}

		// キャプチャー
		private void menuCapture_Click(object sender, System.EventArgs e)
		{
			if(!m_CArtCam.IsInit())
			{
				MessageBox.Show("有効なデバイスを選択してください");
				return;
			}

			ImagePanel.Visible = true;
			timer1.Enabled = false;

			// ここで一旦デバイスを開放する
			m_CArtCam.Close();

			// 取り込み用領域作成
			CreateBitmap();

			ImageBox.SetBounds(0, 0, getWidth(), getHeight());

			// 映像を表示する
			m_CArtCam.Capture();

			// メニューをチェックする
			menuPreview.Checked  = false;
			menuCallback.Checked = false;
			menuCapture.Checked  = true;
			menuTrigger.Checked  = false;

			m_PreviewMode = 3;
			m_StopFlg = false;
			menuStop.Text = "一時停止";

			ImageBox.Invalidate();

			timer1.Enabled = true;
			timer1.Interval = 200;
		}

		// キャプチャータイマー
		private void timer1_Tick(object sender, System.EventArgs e)
		{
			if(m_PreviewMode == 3)
			{
				m_CArtCam.SnapShot(m_pCapture, getSize(), 1);
				m_SaveFlg = true;
			}
			ImageBox.Invalidate();
		}

		// トリガー
		private void menuTrigger_Click(object sender, System.EventArgs e)
		{
			if(!m_CArtCam.IsInit())
			{
				MessageBox.Show("有効なデバイスを選択してください");
				return;
			}

			ImagePanel.Visible = true;
			timer1.Enabled = false;

			// ここで一旦デバイスを開放する
			m_CArtCam.Close();

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

			// 取り込み用領域作成
			CreateBitmap();

			ImageBox.SetBounds(0, 0, getWidth(), getHeight());

			// 映像を取り込む
			m_CArtCam.Trigger(this.Handle, m_pCapture, getSize(), 1);

			// メニューをチェックする
			menuPreview.Checked  = false;
			menuCallback.Checked = false;
			menuCapture.Checked  = false;
			menuTrigger.Checked  = true;

			m_PreviewMode = 4;
			m_StopFlg = false;
			menuStop.Text = "一時停止";

			ImageBox.Invalidate();
		}

		// 一時停止／再開
		private void menuStop_Click(object sender, System.EventArgs e)
		{
			// 動画の時のみ止める
			if(0 == m_PreviewMode || 1 == m_PreviewMode)
			{
				if(m_StopFlg)
				{
					m_CArtCam.StartPreview();
					m_StopFlg = false;

					menuStop.Text = "一時停止";
				}
				else 
				{
					m_CArtCam.StopPreview();
					m_StopFlg = true;

					menuStop.Text = "再開";
				}
			}
		}

		// ビットマップ作成
		private void CreateBitmap()
		{
			// 既にビットマップが作られている場合は一旦開放
			if(null != m_Bitmap)
			{
				m_Bitmap.Dispose();
			}

			switch(getColorMode())
			{
				case 8:
				case 16:
					m_Bitmap = new Bitmap(getWidth(), getHeight(), PixelFormat.Format8bppIndexed);

					// パレット変更
					ColorPalette pal = m_Bitmap.Palette;
					Color[] cpe = m_Bitmap.Palette.Entries;

					for(int i=0; i<256; i++)
					{
						cpe.SetValue(Color.FromArgb(i, i, i), i);
						pal.Entries[i] = cpe[i];
					}
					m_Bitmap.Palette = pal;
				break;

				case 24:	m_Bitmap = new Bitmap(getWidth(), getHeight(), PixelFormat.Format24bppRgb);			break;
				case 32:	m_Bitmap = new Bitmap(getWidth(), getHeight(), PixelFormat.Format24bppRgb);			break;
				case 48:	m_Bitmap = new Bitmap(getWidth(), getHeight(), PixelFormat.Format24bppRgb);			break;
				case 64:	m_Bitmap = new Bitmap(getWidth(), getHeight(), PixelFormat.Format24bppRgb);			break;
			}

			// キャプチャ用の配列
			m_pCapture = new Byte[getSize()];
		}


		// カメラ設定
		private void menuCamera_Click(object sender, System.EventArgs e)
		{
			if(!m_CArtCam.IsInit())
			{
				MessageBox.Show("有効なデバイスを選択してください");
				return;
			}

			if(0 != m_CArtCam.SetCameraDlg(this.Handle))
			{
				switch(m_PreviewMode)
				{
					case 0: menuPreview_Click(sender, e); break;
					case 1: menuCallback_Click(sender, e); break;
					case 3: menuCapture_Click(sender, e); break;
					case 4: menuTrigger_Click(sender, e); break;
				}
			}
		}

		// フィルター設定
		private void menuFilter_Click(object sender, System.EventArgs e)
		{
			if(!m_CArtCam.IsInit())
			{
				MessageBox.Show("有効なデバイスを選択してください");
				return;
			}

			m_CArtCam.SetImageDlg(this.Handle);
		}

		// アナログ設定
		private void menuAnalog_Click(object sender, System.EventArgs e)
		{
			if(!m_CArtCam.IsInit())
			{
				MessageBox.Show("有効なデバイスを選択してください");
				return;
			}

			m_CArtCam.SetAnalogDlg(this.Handle);
		}

		private void OnDllReload(object sender, System.EventArgs e)
		{
			// DLLリストを削除
			for (int i=0; i<m_DllCount; i++){
				menuDLL.MenuItems.RemoveAt(1);
			}
			m_DllCount = 0;
			m_DllSata  = -1;

			// DLL検索
			String szDirPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
			String[] files = Directory.GetFiles(szDirPath, "Art*.dll");
			foreach (String szFileName in files)
			{
				CArtCam ArtCam = new CArtCam();
				if (ArtCam.LoadLibrary(szFileName)){

					// バージョンを取得して表示させます
					long ver = ArtCam.GetDllVersion() & 0xFFFF;
					String szMenu = String.Format("{0}\tVersion {1:D4}", Path.GetFileNameWithoutExtension(szFileName), ver);

					// メニューに追加
					m_DllCount++;
					if(ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_SATA == (ARTCAM_CAMERATYPE)(ArtCam.GetDllVersion() >> 16)){
						
						MenuItem menuSata = new MenuItem();
						menuSata.Text = szMenu;
						String[] CameraName = {
											"LVDS",
											"300MI",
											"500MI",
											"MV413",
											"800MI",
											"036MI",
											"150P3",
											"267KY",
											"274KY",
											"625KY",
											"130MI",
											"200MI",
										};
						for(int i=0; i<CameraName.Length; i++){
							MenuItem mi = new MenuItem();
							mi.Text = CameraName[i];
							mi.Click += new System.EventHandler(OnMenuDLLSelect);
							menuSata.MenuItems.Add(i, mi);
						}
						menuDLL.MenuItems.Add(m_DllCount, menuSata);
						m_DllSata = m_DllCount-1;
					}else{
						MenuItem mi = new MenuItem();
						mi.Text = szMenu;
						mi.Click += new System.EventHandler(OnMenuDLLSelect);
						menuDLL.MenuItems.Add(m_DllCount, mi);
					}
				}
			}
		}

		// DLL選択用メニューハンドラ
		private void OnMenuDLLSelect(object sender, System.EventArgs e)
		{
			int id = menuDLL.MenuItems.IndexOf((MenuItem)sender);
			if (id > -1){
				OnDllChange(sender, e, id-1, -1);
			}else{
				int type = ((MenuItem)sender).Index;
				if((int)ARTCAM_CAMERATYPE_SATA.ARTCAM_CAMERATYPE_SATA_LVDS <= type && type <= (int)ARTCAM_CAMERATYPE_SATA.ARTCAM_CAMERATYPE_SATA_200MI){
					OnDllChange(sender, e, m_DllSata, type);
				}
			}
		}
		// デバイス変更
		private void DeviceChange(object sender, System.EventArgs e, int Number)
		{
			if(m_CArtCam.IsInit())
			{
				m_CArtCam.Close();
			}


			// デバイスが接続されているかチェックする場合は
			// GetDeviceName を使えば簡単に分かる
			// ただし、名前を取得する領域が無い場合もエラーになるので配列長は最低32は用意する事
			StringBuilder Temp = new StringBuilder( 256 );
			if(0 == m_CArtCam.GetDeviceName(Number, Temp, 256))
			{
				m_PreviewMode = -1;
				m_SelectDevice = -1;
				m_StopFlg = false;
				menuStop.Text = "一時停止";
				return;
			}


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


			for(int i=0; i<8; i++)
			{
				menuDevice.MenuItems[i].Checked = false;
			}
			menuDevice.MenuItems[Number].Checked = true;


			switch(m_PreviewMode)
			{
				case 0:	menuPreview_Click(sender, e); break;
				case 1:	menuCallback_Click(sender, e); break;
				case 3:	menuCapture_Click(sender, e); break;
				case 4:	menuTrigger_Click(sender, e); break;
			}
		}

		private void menuDevice0_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 0); }
		private void menuDevice1_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 1); }
		private void menuDevice2_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 2); }
		private void menuDevice3_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 3); }
		private void menuDevice4_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 4); }
		private void menuDevice5_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 5); }
		private void menuDevice6_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 6); }
		private void menuDevice7_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 7); }
		private void menuDevice8_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 8); }
		private void menuDevice9_Click(object sender, System.EventArgs e){ DeviceChange(sender, e, 9); }

		private int getSize()
		{
			return ((getWidth() * (getColorMode() / 8) + 3) & ~3) * getHeight();
		}

		private int getWidth()
		{
			int[] Size = { 1, 2, 4, 8 };
			return m_CArtCam.Width() / Size[(int)(getSubSample())];
		}

		private int getHeight()
		{
			int[] Size = { 1, 2, 4, 8 };
			return m_CArtCam.Height() / Size[(int)getSubSample()];
		}

		private int getColorMode()
		{
			return ((m_CArtCam.GetColorMode() + 7) & ~7);
		}

		private int getSubSample()
		{
			return ((int)m_CArtCam.GetSubSample() & 0x03);
		}

		private BitmapData LockBitmap()
		{
			switch(getColorMode())
			{
				case 8:
				case 16:
					return m_Bitmap.LockBits(new Rectangle(0,0,getWidth(),getHeight()), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

				case 24:
				case 32:
				case 48:
				case 64:
					return m_Bitmap.LockBits(new Rectangle(0,0,getWidth(),getHeight()), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
			}

			return null;
		}

		private void DrawImage(Graphics g)
		{
			if(null == m_Bitmap)
			{
				return ;
			}

			BitmapData pBitmapData = LockBitmap();
			if(null == pBitmapData)
			{
				return ;
			}


			unsafe
			{
				byte* pdest = (byte*)pBitmapData.Scan0.ToPointer();

				// 16ビット転送の場合は８ビットに変換して表示する
				if(getColorMode() == 16)
				{
					int size = getWidth() * getHeight();
					switch(m_CArtCam.GetColorMode())
					{
						case 10: for(int i=0; i<size; i++) pdest[i] = (byte)((m_pCapture[i*2+1] << 6) | (m_pCapture[i*2] >> 2));	break;
						case 12: for(int i=0; i<size; i++) pdest[i] = (byte)((m_pCapture[i*2+1] << 4) | (m_pCapture[i*2] >> 4));	break;
						case 14: for(int i=0; i<size; i++) pdest[i] = (byte)((m_pCapture[i*2+1] << 2) | (m_pCapture[i*2] >> 6));	break;
						case 16: for(int i=0; i<size; i++) pdest[i] = (byte)((m_pCapture[i*2+1] << 0) | (m_pCapture[i*2] >> 8));	break;
					}
				}
				else if(getColorMode() == 32)
				{
					int size = getWidth() * getHeight();
					for(int i=0; i<size; i++)
					{
						pdest[i*3]   = m_pCapture[i*4];
						pdest[i*3+1] = m_pCapture[i*4+1];
						pdest[i*3+2] = m_pCapture[i*4+2];
					}
				}
				// 重いので注意（基本的に16(10)ビットのカラーを扱う際はＣ系列の言語を使用してください）
				else if(getColorMode() == 48 || getColorMode() == 64)
				{
					int bpp = getColorMode() / 8;
					int size = getWidth() * getHeight();
					switch(m_CArtCam.GetColorMode())
					{
						case 42: case 58:
							for(int i=0; i<size; i++)
							{
								pdest[i*3]   = (byte)((m_pCapture[i*bpp+1] << 6) | (m_pCapture[i*bpp]   >> 2));
								pdest[i*3+1] = (byte)((m_pCapture[i*bpp+3] << 6) | (m_pCapture[i*bpp+2] >> 2));
								pdest[i*3+2] = (byte)((m_pCapture[i*bpp+5] << 6) | (m_pCapture[i*bpp+4] >> 2));
							}
							break;
						case 44: case 60:
							for(int i=0; i<size; i++)
							{
								pdest[i*3]   = (byte)((m_pCapture[i*bpp+1] << 4) | (m_pCapture[i*bpp]   >> 4));
								pdest[i*3+1] = (byte)((m_pCapture[i*bpp+3] << 4) | (m_pCapture[i*bpp+2] >> 4));
								pdest[i*3+2] = (byte)((m_pCapture[i*bpp+5] << 4) | (m_pCapture[i*bpp+4] >> 4));
							}
							break;
						case 46: case 62:
							for(int i=0; i<size; i++)
							{
								pdest[i*3]   = (byte)((m_pCapture[i*bpp+1] << 2) | (m_pCapture[i*bpp]   >> 6));
								pdest[i*3+1] = (byte)((m_pCapture[i*bpp+3] << 2) | (m_pCapture[i*bpp+2] >> 6));
								pdest[i*3+2] = (byte)((m_pCapture[i*bpp+5] << 2) | (m_pCapture[i*bpp+4] >> 6));
							}
							break;
						case 48: case 64:
							for(int i=0; i<size; i++)
							{
								pdest[i*3]   = (byte)((m_pCapture[i*bpp+1] << 0) | (m_pCapture[i*bpp]   >> 8));
								pdest[i*3+1] = (byte)((m_pCapture[i*bpp+3] << 0) | (m_pCapture[i*bpp+2] >> 8));
								pdest[i*3+2] = (byte)((m_pCapture[i*bpp+5] << 0) | (m_pCapture[i*bpp+4] >> 8));
							}
							break;
					}
				}
				// それ以外はそのままコピー
				else
				{
					int size = getSize();
					for(int i=0; i<size; i++)
					{
						pdest[i] = m_pCapture[i];
					}
				}
			}

			m_Bitmap.UnlockBits(pBitmapData);


			// 映像表示
			int iWidth = m_Bitmap.Width;
			int iHeight= m_Bitmap.Height;
			g.DrawImage(m_Bitmap, new Rectangle(0, 0, getWidth() * m_ViewScale / 100, getHeight() * m_ViewScale / 100));

			// ライン描画
			Point MousePos = this.PointToClient(Cursor.Position);
			if( (MousePos.X < 0) || (MousePos.Y < 0) || (MousePos.X >= iWidth) || (MousePos.Y >= iHeight)) return;

			Pen pen = new Pen(Color.Red, 1);
			Point Pos = new Point(MousePos.X - ImagePanel.AutoScrollPosition.X, MousePos.Y - ImagePanel.AutoScrollPosition.Y);
			g.DrawLine(pen, Pos.X, 0, Pos.X, getHeight() * m_ViewScale / 100);
			g.DrawLine(pen, 0, Pos.Y, getWidth() * m_ViewScale / 100, Pos.Y);


			//露光時間をミリ秒単位で表示(v1300以降)
			if (1300 <= (m_CArtCam.GetDllVersion() & 0xFFFF))
			{
				int Exposure = m_CArtCam.GetRealExposureTime();	// (マイクロ秒)
				float fExposure = (float)Exposure / 1000.0f;
				sbPanelExp.Text = "Exp: " + fExposure.ToString() + "msec";
			}
			else
			{
				sbPanelExp.Text = "";
			}
		}

		private void ImageBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			DrawImage(e.Graphics);
		}

		private void Form1_Resize(object sender, System.EventArgs e)
		{
			ImagePanel.SetBounds(0,0,this.ClientRectangle.Right,this.ClientRectangle.Bottom - 16);
			statusBar1.SetBounds(0,this.ClientRectangle.Bottom - 16, this.ClientRectangle.Right, 16);
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
		}

		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			m_CArtCam.Close();
			timer1.Enabled = false;


			Form modalDialog = new Form2(m_CArtCam);
			DialogResult result = modalDialog.ShowDialog();

			switch(m_PreviewMode)
			{
				case 0: menuPreview_Click(sender, e); break;
				case 1: menuCallback_Click(sender, e); break;
				case 3: menuCapture_Click(sender, e); break;
				case 4: menuTrigger_Click(sender, e); break;
			}
		}

		private void menuItem3_Click(object sender, System.EventArgs e)
		{
			Form modalDialog = new Form3(m_CArtCam, m_PreviewMode);
			DialogResult result = modalDialog.ShowDialog();
		}

		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			Form modalDialog = new Form4(m_CArtCam);
			DialogResult result = modalDialog.ShowDialog();	
		}




		private void menuFile_Popup(object sender, System.EventArgs e)
		{
			// 保存が可能か判定（一度でも映像が取り込まれていれば可能）
			if(m_SaveFlg)
			{
				menuSave.Enabled = true;
			} 
			else 
			{
				menuSave.Enabled = false;
			}

			// DirectShowカメラかチェック
			// 録画機能があるのはDirectShowカメラのみ
			if (ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_DS == m_CurrentCameraType)
			{
				menuRec.Enabled = true;
			} 
			else 
			{
				menuRec.Enabled = false;
			}
		}

		private void menuView_Popup(object sender, System.EventArgs e)
		{
			// デバイスに接続してない場合は再生出来なくする
			if(-1 == m_SelectDevice)
			{
				menuPreview.Enabled = false;
				menuCallback.Enabled = false;
				menuSnapshot.Enabled = false;
				menuCapture.Enabled = false;
				menuTrigger.Enabled = false;
				menuStop.Enabled = false;
			}
			else 
			{
				menuPreview.Enabled = true;
				menuCallback.Enabled = true;
				menuSnapshot.Enabled = true;
				menuCapture.Enabled = true;
				menuStop.Enabled = false;

				// トリガーモードは使えないカメラもあるので注意
				switch (m_CurrentCameraType)
				{
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_DS:
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_USTC:	
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_CNV:	
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_200SH:
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_098:	
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_150P:
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_500P:		
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_130MI_MOUT:
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_150P3_MOUT:
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_1000MI_HD2:
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_USB3_500MI:
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_USB3_900MI:
					case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_USB3_267KY:
						menuTrigger.Enabled = false;
						break;

					default:
						menuTrigger.Enabled = true;
						break;
				}
			}
		}

		private void menuSet_Popup(object sender, System.EventArgs e)
		{
			// DirectShowカメラは機種によって使用できるかどうか強く依存されます
			// 特にSetAnalogDlgは使えない機種が多数あると思われますので注意してください。
			menuCamera.Enabled = true;
			menuFilter.Enabled = true;

			switch (m_CurrentCameraType)
			{
				//case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_DS:		// DS
				case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_USTC:		// UST
				case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_CNV:		// CNV
					menuAnalog.Enabled = false;
					break;

				case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_150P3_MOUT:
				case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_130MI_MOUT:
				case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_1000MI_HD2:
					menuItem7.Enabled = true;
					break;

				case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_008TNIR:
				case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_031TNIR:
				case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_0016TNIR:
					MenuUserCorrection.Enabled = true;
					break;


				default:
					menuAnalog.Enabled = true;
					//menuItem7.Enabled = false;
					MenuUserCorrection.Enabled = false;
					break;
			}


		}

		private void menuItem7_Click_1(object sender, System.EventArgs e)
		{
			Form modalDialog;
			switch(m_CurrentCameraType){
			case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_130MI_MOUT:
			case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_150P3_MOUT:	
				modalDialog = new Form5(m_CArtCam);	
				modalDialog.ShowDialog(); 
			break;
			case ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_1000MI_HD2:
				modalDialog = new Form7(m_CArtCam);	
				modalDialog.ShowDialog();
			break;
			}
		}

		private void OnDllChange(object sender, System.EventArgs e, int DllType, int SataType)
		{
			Release();
			m_CArtCam.FreeLibrary();

			m_CurrentCameraType = 0;

			if (0 == m_DllCount) return;
			String stMenu = menuDLL.MenuItems[(int)DllType + 1].Text;
			String[] stArray = stMenu.Split('\t');
			String szDllName = String.Format("{0}.dll", stArray[0]);
			bool res = m_CArtCam.LoadLibrary(szDllName);
			if (!res){
				MessageBox.Show("DLLが見つかりません\n起動した後でDLLを移動等した可能性があります");
				return;
			}else{
				m_CurrentCameraType = (ARTCAM_CAMERATYPE)(m_CArtCam.GetDllVersion() >> 16);
			}
			// Initialize は最初に呼びます
			// ここでウィンドウハンドルを設定するとWM_ERRORを取得する事が出来る
			if (!m_CArtCam.Initialize(this.Handle)){
				MessageBox.Show("ＳＤＫの初期化に失敗しました");
				return;
			}
			m_DllType = DllType;
			m_SataType = SataType;

			// メニューをチェックする
			for (int i = 0; i < m_DllCount; i++){
				menuDLL.MenuItems[(int)i + 1].Checked = false;
			}
			// SATAカメラは更にカメラのタイプを指定する
			if(-1 != SataType && ARTCAM_CAMERATYPE.ARTCAM_CAMERATYPE_SATA == m_CurrentCameraType){
				m_CArtCam.SetCameraType(SataType);
				m_CArtCam.SetDeviceNumber(0);
			}else{
				menuDLL.MenuItems[(int)DllType+1].Checked = true;
			}


			// 利用可能なデバイスをチェックする
			for(int i=0; i<8; i++)
			{
				StringBuilder Temp = new StringBuilder( 256 );
				if(0 != m_CArtCam.GetDeviceName(i, Temp, 256))
				{
					menuDevice.MenuItems[i].Text = Temp.ToString();
					menuDevice.MenuItems[i].Enabled = true;
				}
				else
				{
					menuDevice.MenuItems[i].Enabled = false;
				}
			}

			DeviceChange(sender, e, 0);

			ImageBox.SetBounds(0, 0, getWidth(), getHeight());
		}

		private void statusBar1_PanelClick(object sender, System.Windows.Forms.StatusBarPanelClickEventArgs e)
		{
			if(e.StatusBarPanel == sbPanelScale)
			{
				if(e.Button.Equals(System.Windows.Forms.MouseButtons.Left))
				{
					switch(m_ViewScale)
					{
						case  25:	m_ViewScale =  50;	break;
						case  50:	m_ViewScale = 100;	break;
						case 100:	m_ViewScale = 150;	break;
						case 150:	m_ViewScale = 200;	break;
						case 200:	m_ViewScale = 400;	break;
						default :	m_ViewScale = 400;	break;
					}
				}
				else
				{
					switch(m_ViewScale)
					{
						case  50:	m_ViewScale =  25;	break;
						case 100:	m_ViewScale =  50;	break;
						case 150:	m_ViewScale = 100;	break;
						case 200:	m_ViewScale = 150;	break;
						case 400:	m_ViewScale = 200;	break;
						default :	m_ViewScale =  25;	break;
					}
				}
			}
			ImagePanel.AutoScrollPosition = new Point(0, 0);
			ImageBox.SetBounds(0, 0, getWidth() * m_ViewScale / 100, getHeight() * m_ViewScale / 100, BoundsSpecified.All);
			sbPanelScale.Text = m_ViewScale.ToString() + "%";
		}

		private void MenuUserCorrection_Click(object sender, EventArgs e)
		{
			Form modalDialog = new Form6(m_CArtCam);
			DialogResult result = modalDialog.ShowDialog();
		}

		private void menuSet_Click(object sender, EventArgs e)
		{

		}
	}
}



