using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CtimerwCs;

namespace Interval
{
	/// <summary>
	/// Interval ̊Tv̐łB
	/// </summary>
	public class Interval : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		/// <summary>
		/// KvȃfUCiϐłB
		/// </summary>
		private System.ComponentModel.Container components = null;

		Ctimerw timerw = new Ctimerw();

		public uint		interval_count;
		public short	TimerId;
		private System.Windows.Forms.Label Label_ProductName;
		private System.Windows.Forms.TextBox Text_DeviceName;
		private System.Windows.Forms.TextBox Text_Interval;
		private System.Windows.Forms.Label Label_MsgCount;
		private System.Windows.Forms.TextBox Text_Ret;
		private System.Windows.Forms.Button Button_TimInit;
		private System.Windows.Forms.Button Button_TimStartTimer;
		private System.Windows.Forms.Button Button_TimStopTimer;
		private System.Windows.Forms.Button Button_TimExit;
		private System.Windows.Forms.Button Button_Exit;
		
		public Interval()
		{
			//
			// Windows tH[ fUCi T|[gɕKvłB
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent ĂяǒɁARXgN^ R[hǉĂB
			//
		}

		/// <summary>
		/// gpĂ郊\[XɌ㏈s܂B
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// fUCi T|[gɕKvȃ\bhłB̃\bh̓e
		/// R[h GfB^ŕύXȂłB
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.Label_ProductName = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.Text_DeviceName = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.Text_Interval = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.Label_MsgCount = new System.Windows.Forms.Label();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.Text_Ret = new System.Windows.Forms.TextBox();
			this.Button_TimInit = new System.Windows.Forms.Button();
			this.Button_TimStartTimer = new System.Windows.Forms.Button();
			this.Button_TimStopTimer = new System.Windows.Forms.Button();
			this.Button_TimExit = new System.Windows.Forms.Button();
			this.Button_Exit = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Label_ProductName,
																					this.label4,
																					this.Text_DeviceName,
																					this.label3});
			this.groupBox1.Location = new System.Drawing.Point(8, 6);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(216, 56);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "";
			// 
			// Label_ProductName
			// 
			this.Label_ProductName.Location = new System.Drawing.Point(72, 37);
			this.Label_ProductName.Name = "Label_ProductName";
			this.Label_ProductName.Size = new System.Drawing.Size(128, 11);
			this.Label_ProductName.TabIndex = 3;
			this.Label_ProductName.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 37);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 2;
			this.label4.Text = "iF";
			// 
			// Text_DeviceName
			// 
			this.Text_DeviceName.Location = new System.Drawing.Point(72, 13);
			this.Text_DeviceName.Name = "Text_DeviceName";
			this.Text_DeviceName.Size = new System.Drawing.Size(136, 19);
			this.Text_DeviceName.TabIndex = 1;
			this.Text_DeviceName.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 16);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 16);
			this.label3.TabIndex = 0;
			this.label3.Text = "foCXF";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Text_Interval,
																					this.label2});
			this.groupBox2.Location = new System.Drawing.Point(8, 65);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(216, 43);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "C^[o";
			// 
			// Text_Interval
			// 
			this.Text_Interval.Location = new System.Drawing.Point(16, 16);
			this.Text_Interval.Name = "Text_Interval";
			this.Text_Interval.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.Text_Interval.Size = new System.Drawing.Size(144, 19);
			this.Text_Interval.TabIndex = 1;
			this.Text_Interval.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(168, 20);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(32, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "[us]";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Label_MsgCount});
			this.groupBox3.Location = new System.Drawing.Point(8, 112);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(216, 37);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "ʒm";
			// 
			// Label_MsgCount
			// 
			this.Label_MsgCount.Location = new System.Drawing.Point(16, 17);
			this.Label_MsgCount.Name = "Label_MsgCount";
			this.Label_MsgCount.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.Label_MsgCount.Size = new System.Drawing.Size(140, 10);
			this.Label_MsgCount.TabIndex = 0;
			this.Label_MsgCount.Text = "0";
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Text_Ret});
			this.groupBox4.Location = new System.Drawing.Point(8, 153);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(328, 46);
			this.groupBox4.TabIndex = 3;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "߂l";
			// 
			// Text_Ret
			// 
			this.Text_Ret.Location = new System.Drawing.Point(8, 16);
			this.Text_Ret.Name = "Text_Ret";
			this.Text_Ret.Size = new System.Drawing.Size(312, 19);
			this.Text_Ret.TabIndex = 0;
			this.Text_Ret.Text = "";
			// 
			// Button_TimInit
			// 
			this.Button_TimInit.Location = new System.Drawing.Point(240, 8);
			this.Button_TimInit.Name = "Button_TimInit";
			this.Button_TimInit.Size = new System.Drawing.Size(92, 21);
			this.Button_TimInit.TabIndex = 4;
			this.Button_TimInit.Text = "TimInit";
			this.Button_TimInit.Click += new System.EventHandler(this.Button_TimInit_Click);
			// 
			// Button_TimStartTimer
			// 
			this.Button_TimStartTimer.Location = new System.Drawing.Point(240, 38);
			this.Button_TimStartTimer.Name = "Button_TimStartTimer";
			this.Button_TimStartTimer.Size = new System.Drawing.Size(92, 21);
			this.Button_TimStartTimer.TabIndex = 5;
			this.Button_TimStartTimer.Text = "TimStartTimer";
			this.Button_TimStartTimer.Click += new System.EventHandler(this.Button_TimStartTimer_Click);
			// 
			// Button_TimStopTimer
			// 
			this.Button_TimStopTimer.Location = new System.Drawing.Point(240, 68);
			this.Button_TimStopTimer.Name = "Button_TimStopTimer";
			this.Button_TimStopTimer.Size = new System.Drawing.Size(92, 21);
			this.Button_TimStopTimer.TabIndex = 6;
			this.Button_TimStopTimer.Text = "TimStopTimer";
			this.Button_TimStopTimer.Click += new System.EventHandler(this.Button_TimStopTimer_Click);
			// 
			// Button_TimExit
			// 
			this.Button_TimExit.Location = new System.Drawing.Point(240, 98);
			this.Button_TimExit.Name = "Button_TimExit";
			this.Button_TimExit.Size = new System.Drawing.Size(92, 21);
			this.Button_TimExit.TabIndex = 7;
			this.Button_TimExit.Text = "TimExit";
			this.Button_TimExit.Click += new System.EventHandler(this.Button_TimExit_Click);
			// 
			// Button_Exit
			// 
			this.Button_Exit.Location = new System.Drawing.Point(240, 128);
			this.Button_Exit.Name = "Button_Exit";
			this.Button_Exit.Size = new System.Drawing.Size(92, 21);
			this.Button_Exit.TabIndex = 8;
			this.Button_Exit.Text = "Exit";
			this.Button_Exit.Click += new System.EventHandler(this.Button_Exit_Click);
			// 
			// Interval
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(342, 206);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.Button_Exit,
																		  this.Button_TimExit,
																		  this.Button_TimStopTimer,
																		  this.Button_TimStartTimer,
																		  this.Button_TimInit,
																		  this.groupBox4,
																		  this.groupBox3,
																		  this.groupBox2,
																		  this.groupBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Interval";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "C^[o^C}[";
			this.Load += new System.EventHandler(this.Interval_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// AvP[ṼC Gg |CgłB
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Interval());
		}

		private void Interval_Load(object sender, System.EventArgs e)
		{
			Text_Interval.Text = "1000";
			Button_TimStopTimer.Enabled = false;
		}

		//==============================================================================
		// TimInit
		//==============================================================================
		private void Button_TimInit_Click(object sender, System.EventArgs e)
		{
			string	DeviceName;
			string	ProductName; 
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// foCX擾
			//-----------------------------------
			DeviceName = Text_DeviceName.Text;
			//-----------------------------------
			// 
			//-----------------------------------
			ret = timerw.Init(ref DeviceName, out ProductName);
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimInit ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS){
				Text_DeviceName.Text = DeviceName;
				Label_ProductName.Text = ProductName;
			}
		}

		//==============================================================================
		// TimStartTimer
		//==============================================================================
		private void Button_TimStartTimer_Click(object sender, System.EventArgs e)
		{
			string	ErrorString;
			uint	Microseconds;
			int		ret;

			//-----------------------------------
			// C^[oJEg
			//-----------------------------------
			interval_count	= 0;
			//-----------------------------------
			// C^[oݒl擾
			//-----------------------------------
			Microseconds	= System.Convert.ToUInt32(Text_Interval.Text);
			//-----------------------------------
			// C^[o^C}Jn
			//-----------------------------------
			ret = timerw.StartTimer(out TimerId, Microseconds, this.Handle.ToInt32());
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimStartTimer ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS){
				//-----------------------------------
				// ^C}[~{^L
				//-----------------------------------
				Button_TimStopTimer.Enabled = true;
				//-----------------------------------
				// ^C}[Jn{^
				//-----------------------------------
				Button_TimStartTimer.Enabled = false;
			}
		}

		//==============================================================================
		// TimStopTimer
		//==============================================================================
		private void Button_TimStopTimer_Click(object sender, System.EventArgs e)
		{
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// C^[o^C}~
			//-----------------------------------
			ret = timerw.StopTimer(TimerId);
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimStopTimer ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			if (ret == (int)CtimerwConst.TIM_ERR_SUCCESS){
				//-----------------------------------
				// ^C}[~{^
				//-----------------------------------
				Button_TimStopTimer.Enabled = false;
				//-----------------------------------
				// ^C}[Jn{^L
				//-----------------------------------
				Button_TimStartTimer.Enabled = true;
			}
		}

		//==============================================================================
		// TimExit
		//==============================================================================
		private void Button_TimExit_Click(object sender, System.EventArgs e)
		{
			string	ErrorString;
			int		ret;

			//-----------------------------------
			// I
			//-----------------------------------
			ret = timerw.Exit();
			//-----------------------------------
			// G[
			//-----------------------------------
			timerw.GetErrorString(ret, out ErrorString);
			Text_Ret.Text = "TimExit ret = " + System.Convert.ToString(ret) + " " + ErrorString;
			//-----------------------------------
			// ^C}[~{^
			//-----------------------------------
			Button_TimStopTimer.Enabled = false;
			//-----------------------------------
			// ^C}[Jn{^L
			//-----------------------------------
			Button_TimStartTimer.Enabled = true;
		}

		//==============================================================================
		// Exit
		//==============================================================================
		private void Button_Exit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		//===================================================
		// Cxgʒm
		//===================================================
		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
			//-----------------------------------
			// C^[o^C}bZ[W
			//-----------------------------------
			case (int)CtimerwConst.TIMM_INTERVAL:
				interval_count++;
				Label_MsgCount.Text = interval_count.ToString();
				break;
			}
			base.WndProc(ref m);
		}
	}
}
